Skip to content

Commit 35b0f24

Browse files
committed
Auto merge of rust-lang#8028 - dswij:8016, r=flip1995
Add more descriptive help info for `needless_question_mark` closes rust-lang#8016 changelog: [`needless_question_mark`] help suggestion now explains what should be changed
2 parents 81f37a8 + c0bad8b commit 35b0f24

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

clippy_lints/src/needless_question_mark.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,33 @@ impl LateLintPass<'_> for NeedlessQuestionMark {
9393
}
9494

9595
fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
96-
let inner_expr = if_chain! {
96+
if_chain! {
9797
if let ExprKind::Call(path, [arg]) = &expr.kind;
9898
if let ExprKind::Path(ref qpath) = &path.kind;
99-
if is_lang_ctor(cx, qpath, OptionSome) || is_lang_ctor(cx, qpath, ResultOk);
99+
let sugg_remove = if is_lang_ctor(cx, qpath, OptionSome) {
100+
"Some()"
101+
} else if is_lang_ctor(cx, qpath, ResultOk) {
102+
"Ok()"
103+
} else {
104+
return;
105+
};
100106
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &arg.kind;
101107
if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind;
102108
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, _)) = &called.kind;
103109
if expr.span.ctxt() == inner_expr.span.ctxt();
104110
let expr_ty = cx.typeck_results().expr_ty(expr);
105111
let inner_ty = cx.typeck_results().expr_ty(inner_expr);
106112
if TyS::same_type(expr_ty, inner_ty);
107-
then { inner_expr } else { return; }
108-
};
109-
span_lint_and_sugg(
110-
cx,
111-
NEEDLESS_QUESTION_MARK,
112-
expr.span,
113-
"question mark operator is useless here",
114-
"try",
115-
format!("{}", snippet(cx, inner_expr.span, r#""...""#)),
116-
Applicability::MachineApplicable,
117-
);
113+
then {
114+
span_lint_and_sugg(
115+
cx,
116+
NEEDLESS_QUESTION_MARK,
117+
expr.span,
118+
"question mark operator is useless here",
119+
&format!("try removing question mark and `{}`", sugg_remove),
120+
format!("{}", snippet(cx, inner_expr.span, r#""...""#)),
121+
Applicability::MachineApplicable,
122+
);
123+
}
124+
}
118125
}

tests/ui/needless_question_mark.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,75 @@ error: question mark operator is useless here
22
--> $DIR/needless_question_mark.rs:23:12
33
|
44
LL | return Some(to.magic?);
5-
| ^^^^^^^^^^^^^^^ help: try: `to.magic`
5+
| ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
66
|
77
= note: `-D clippy::needless-question-mark` implied by `-D warnings`
88

99
error: question mark operator is useless here
1010
--> $DIR/needless_question_mark.rs:31:12
1111
|
1212
LL | return Some(to.magic?)
13-
| ^^^^^^^^^^^^^^^ help: try: `to.magic`
13+
| ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
1414

1515
error: question mark operator is useless here
1616
--> $DIR/needless_question_mark.rs:36:5
1717
|
1818
LL | Some(to.magic?)
19-
| ^^^^^^^^^^^^^^^ help: try: `to.magic`
19+
| ^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `to.magic`
2020

2121
error: question mark operator is useless here
2222
--> $DIR/needless_question_mark.rs:41:21
2323
|
2424
LL | to.and_then(|t| Some(t.magic?))
25-
| ^^^^^^^^^^^^^^ help: try: `t.magic`
25+
| ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic`
2626

2727
error: question mark operator is useless here
2828
--> $DIR/needless_question_mark.rs:50:9
2929
|
3030
LL | Some(t.magic?)
31-
| ^^^^^^^^^^^^^^ help: try: `t.magic`
31+
| ^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `t.magic`
3232

3333
error: question mark operator is useless here
3434
--> $DIR/needless_question_mark.rs:55:12
3535
|
3636
LL | return Ok(tr.magic?);
37-
| ^^^^^^^^^^^^^ help: try: `tr.magic`
37+
| ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic`
3838

3939
error: question mark operator is useless here
4040
--> $DIR/needless_question_mark.rs:62:12
4141
|
4242
LL | return Ok(tr.magic?)
43-
| ^^^^^^^^^^^^^ help: try: `tr.magic`
43+
| ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic`
4444

4545
error: question mark operator is useless here
4646
--> $DIR/needless_question_mark.rs:66:5
4747
|
4848
LL | Ok(tr.magic?)
49-
| ^^^^^^^^^^^^^ help: try: `tr.magic`
49+
| ^^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `tr.magic`
5050

5151
error: question mark operator is useless here
5252
--> $DIR/needless_question_mark.rs:70:21
5353
|
5454
LL | tr.and_then(|t| Ok(t.magic?))
55-
| ^^^^^^^^^^^^ help: try: `t.magic`
55+
| ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic`
5656

5757
error: question mark operator is useless here
5858
--> $DIR/needless_question_mark.rs:78:9
5959
|
6060
LL | Ok(t.magic?)
61-
| ^^^^^^^^^^^^ help: try: `t.magic`
61+
| ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic`
6262

6363
error: question mark operator is useless here
6464
--> $DIR/needless_question_mark.rs:85:16
6565
|
6666
LL | return Ok(t.magic?);
67-
| ^^^^^^^^^^^^ help: try: `t.magic`
67+
| ^^^^^^^^^^^^ help: try removing question mark and `Ok()`: `t.magic`
6868

6969
error: question mark operator is useless here
7070
--> $DIR/needless_question_mark.rs:120:27
7171
|
7272
LL | || -> Option<_> { Some(Some($expr)?) }()
73-
| ^^^^^^^^^^^^^^^^^^ help: try: `Some($expr)`
73+
| ^^^^^^^^^^^^^^^^^^ help: try removing question mark and `Some()`: `Some($expr)`
7474
...
7575
LL | let _x = some_and_qmark_in_macro!(x?);
7676
| ---------------------------- in this macro invocation

0 commit comments

Comments
 (0)