Add more descriptive help info for needless_question_mark

This commit is contained in:
Dharma Saputra Wijaya 2021-11-25 09:10:50 +08:00 committed by dswij
parent 81f37a8150
commit c0bad8bcab
2 changed files with 32 additions and 25 deletions

View File

@ -93,10 +93,16 @@ impl LateLintPass<'_> for NeedlessQuestionMark {
} }
fn check(cx: &LateContext<'_>, expr: &Expr<'_>) { fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
let inner_expr = if_chain! { if_chain! {
if let ExprKind::Call(path, [arg]) = &expr.kind; if let ExprKind::Call(path, [arg]) = &expr.kind;
if let ExprKind::Path(ref qpath) = &path.kind; if let ExprKind::Path(ref qpath) = &path.kind;
if is_lang_ctor(cx, qpath, OptionSome) || is_lang_ctor(cx, qpath, ResultOk); let sugg_remove = if is_lang_ctor(cx, qpath, OptionSome) {
"Some()"
} else if is_lang_ctor(cx, qpath, ResultOk) {
"Ok()"
} else {
return;
};
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &arg.kind; if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &arg.kind;
if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind; if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind;
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, _)) = &called.kind; if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, _)) = &called.kind;
@ -104,15 +110,16 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
let expr_ty = cx.typeck_results().expr_ty(expr); let expr_ty = cx.typeck_results().expr_ty(expr);
let inner_ty = cx.typeck_results().expr_ty(inner_expr); let inner_ty = cx.typeck_results().expr_ty(inner_expr);
if TyS::same_type(expr_ty, inner_ty); if TyS::same_type(expr_ty, inner_ty);
then { inner_expr } else { return; } then {
};
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
NEEDLESS_QUESTION_MARK, NEEDLESS_QUESTION_MARK,
expr.span, expr.span,
"question mark operator is useless here", "question mark operator is useless here",
"try", &format!("try removing question mark and `{}`", sugg_remove),
format!("{}", snippet(cx, inner_expr.span, r#""...""#)), format!("{}", snippet(cx, inner_expr.span, r#""...""#)),
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
}
}

View File

@ -2,7 +2,7 @@ error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:23:12 --> $DIR/needless_question_mark.rs:23:12
| |
LL | return Some(to.magic?); LL | return Some(to.magic?);
| ^^^^^^^^^^^^^^^ help: try: `to.magic` | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
| |
= note: `-D clippy::needless-question-mark` implied by `-D warnings` = note: `-D clippy::needless-question-mark` implied by `-D warnings`
@ -10,67 +10,67 @@ error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:31:12 --> $DIR/needless_question_mark.rs:31:12
| |
LL | return Some(to.magic?) LL | return Some(to.magic?)
| ^^^^^^^^^^^^^^^ help: try: `to.magic` | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:36:5 --> $DIR/needless_question_mark.rs:36:5
| |
LL | Some(to.magic?) LL | Some(to.magic?)
| ^^^^^^^^^^^^^^^ help: try: `to.magic` | ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:41:21 --> $DIR/needless_question_mark.rs:41:21
| |
LL | to.and_then(|t| Some(t.magic?)) LL | to.and_then(|t| Some(t.magic?))
| ^^^^^^^^^^^^^^ help: try: `t.magic` | ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:50:9 --> $DIR/needless_question_mark.rs:50:9
| |
LL | Some(t.magic?) LL | Some(t.magic?)
| ^^^^^^^^^^^^^^ help: try: `t.magic` | ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:55:12 --> $DIR/needless_question_mark.rs:55:12
| |
LL | return Ok(tr.magic?); LL | return Ok(tr.magic?);
| ^^^^^^^^^^^^^ help: try: `tr.magic` | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:62:12 --> $DIR/needless_question_mark.rs:62:12
| |
LL | return Ok(tr.magic?) LL | return Ok(tr.magic?)
| ^^^^^^^^^^^^^ help: try: `tr.magic` | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:66:5 --> $DIR/needless_question_mark.rs:66:5
| |
LL | Ok(tr.magic?) LL | Ok(tr.magic?)
| ^^^^^^^^^^^^^ help: try: `tr.magic` | ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:70:21 --> $DIR/needless_question_mark.rs:70:21
| |
LL | tr.and_then(|t| Ok(t.magic?)) LL | tr.and_then(|t| Ok(t.magic?))
| ^^^^^^^^^^^^ help: try: `t.magic` | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:78:9 --> $DIR/needless_question_mark.rs:78:9
| |
LL | Ok(t.magic?) LL | Ok(t.magic?)
| ^^^^^^^^^^^^ help: try: `t.magic` | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:85:16 --> $DIR/needless_question_mark.rs:85:16
| |
LL | return Ok(t.magic?); LL | return Ok(t.magic?);
| ^^^^^^^^^^^^ help: try: `t.magic` | ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic`
error: question mark operator is useless here error: question mark operator is useless here
--> $DIR/needless_question_mark.rs:120:27 --> $DIR/needless_question_mark.rs:120:27
| |
LL | || -> Option<_> { Some(Some($expr)?) }() LL | || -> Option<_> { Some(Some($expr)?) }()
| ^^^^^^^^^^^^^^^^^^ help: try: `Some($expr)` | ^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `Some($expr)`
... ...
LL | let _x = some_and_qmark_in_macro!(x?); LL | let _x = some_and_qmark_in_macro!(x?);
| ---------------------------- in this macro invocation | ---------------------------- in this macro invocation