Skip to content

Commit 0d2e2b5

Browse files
committed
Auto merge of #6935 - matthiaskrgr:qmark_marco, r=flip1995
needless_question_mark: don't lint if Some(..) is inside a macro def and the ? is not. The suggestion would fail to apply. Fixes #6921 changelog: needless_question_mark: don't lint if Some(..) is inside a macro def and the ? is not.
2 parents 36aee1c + b42ec5e commit 0d2e2b5

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

clippy_lints/src/needless_question_mark.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet;
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{is_ok_ctor, is_some_ctor, meets_msrv};
4+
use clippy_utils::{differing_macro_contexts, is_ok_ctor, is_some_ctor, meets_msrv};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::{Body, Expr, ExprKind, LangItem, MatchSource, QPath};
@@ -173,6 +173,11 @@ fn is_some_or_ok_call<'a>(
173173
// question mark operator
174174
let inner_expr = &args[0];
175175

176+
// if the inner expr is inside macro but the outer one is not, do not lint (#6921)
177+
if differing_macro_contexts(expr.span, inner_expr.span) {
178+
return None;
179+
}
180+
176181
let inner_ty = cx.typeck_results().expr_ty(inner_expr);
177182
let outer_ty = cx.typeck_results().expr_ty(expr);
178183

tests/ui/needless_question_mark.fixed

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,28 @@ mod question_mark_both {
167167
needless_question_mark_result();
168168
}
169169
}
170+
171+
// #6921 if a macro wraps an expr in Some( ) and the ? is in the macro use,
172+
// the suggestion fails to apply; do not lint
173+
macro_rules! some_in_macro {
174+
($expr:expr) => {
175+
|| -> _ { Some($expr) }()
176+
};
177+
}
178+
179+
pub fn test1() {
180+
let x = Some(3);
181+
let _x = some_in_macro!(x?);
182+
}
183+
184+
// this one is ok because both the ? and the Some are both inside the macro def
185+
macro_rules! some_and_qmark_in_macro {
186+
($expr:expr) => {
187+
|| -> Option<_> { Some($expr) }()
188+
};
189+
}
190+
191+
pub fn test2() {
192+
let x = Some(3);
193+
let _x = some_and_qmark_in_macro!(x?);
194+
}

tests/ui/needless_question_mark.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,28 @@ mod question_mark_both {
167167
needless_question_mark_result();
168168
}
169169
}
170+
171+
// #6921 if a macro wraps an expr in Some( ) and the ? is in the macro use,
172+
// the suggestion fails to apply; do not lint
173+
macro_rules! some_in_macro {
174+
($expr:expr) => {
175+
|| -> _ { Some($expr) }()
176+
};
177+
}
178+
179+
pub fn test1() {
180+
let x = Some(3);
181+
let _x = some_in_macro!(x?);
182+
}
183+
184+
// this one is ok because both the ? and the Some are both inside the macro def
185+
macro_rules! some_and_qmark_in_macro {
186+
($expr:expr) => {
187+
|| -> Option<_> { Some(Some($expr)?) }()
188+
};
189+
}
190+
191+
pub fn test2() {
192+
let x = Some(3);
193+
let _x = some_and_qmark_in_macro!(x?);
194+
}

tests/ui/needless_question_mark.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,16 @@ error: question mark operator is useless here
8484
LL | Ok(to.magic?) // should be triggered
8585
| ^^^^^^^^^^^^^ help: try: `to.magic`
8686

87-
error: aborting due to 14 previous errors
87+
error: question mark operator is useless here
88+
--> $DIR/needless_question_mark.rs:187:27
89+
|
90+
LL | || -> Option<_> { Some(Some($expr)?) }()
91+
| ^^^^^^^^^^^^^^^^^^ help: try: `Some($expr)`
92+
...
93+
LL | let _x = some_and_qmark_in_macro!(x?);
94+
| ---------------------------- in this macro invocation
95+
|
96+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
97+
98+
error: aborting due to 15 previous errors
8899

0 commit comments

Comments
 (0)