Skip to content

Commit 83e75f9

Browse files
committed
Fix incorrect suggestion for try_err lint when Err arg is itself a macro
1 parent f83762b commit 83e75f9

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

clippy_lints/src/try_err.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
in_macro, is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet, snippet_with_macro_callsite,
3-
span_lint_and_sugg,
2+
differing_macro_contexts, in_macro, is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet,
3+
snippet_with_macro_callsite, span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
@@ -91,8 +91,11 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
9191
};
9292

9393
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
94+
let differing_contexts = differing_macro_contexts(expr.span, err_arg.span);
9495

95-
let origin_snippet = if err_arg.span.from_expansion() && !in_macro(expr.span) {
96+
let origin_snippet = if in_macro(expr.span) && in_macro(err_arg.span) && differing_contexts {
97+
snippet(cx, err_arg.span.ctxt().outer_expn_data().call_site, "_")
98+
} else if err_arg.span.from_expansion() && !in_macro(expr.span) {
9699
snippet_with_macro_callsite(cx, err_arg.span, "_")
97100
} else {
98101
snippet(cx, err_arg.span, "_")

tests/ui/try_err.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,26 @@ macro_rules! try_validation {
8888
}};
8989
}
9090

91+
macro_rules! ret_one {
92+
() => {
93+
1
94+
};
95+
}
96+
97+
macro_rules! try_validation_in_macro {
98+
($e: expr) => {{
99+
match $e {
100+
Ok(_) => 0,
101+
Err(_) => return Err(ret_one!()),
102+
}
103+
}};
104+
}
105+
91106
fn calling_macro() -> Result<i32, i32> {
107+
// macro
92108
try_validation!(Ok::<_, i32>(5));
109+
// `Err` arg is another macro
110+
try_validation_in_macro!(Ok::<_, i32>(5));
93111
Ok(5)
94112
}
95113

tests/ui/try_err.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,26 @@ macro_rules! try_validation {
8888
}};
8989
}
9090

91+
macro_rules! ret_one {
92+
() => {
93+
1
94+
};
95+
}
96+
97+
macro_rules! try_validation_in_macro {
98+
($e: expr) => {{
99+
match $e {
100+
Ok(_) => 0,
101+
Err(_) => Err(ret_one!())?,
102+
}
103+
}};
104+
}
105+
91106
fn calling_macro() -> Result<i32, i32> {
107+
// macro
92108
try_validation!(Ok::<_, i32>(5));
109+
// `Err` arg is another macro
110+
try_validation_in_macro!(Ok::<_, i32>(5));
93111
Ok(5)
94112
}
95113

tests/ui/try_err.stderr

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,39 @@ LL | try_validation!(Ok::<_, i32>(5));
4040
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
4141

4242
error: returning an `Err(_)` with the `?` operator
43-
--> $DIR/try_err.rs:122:9
43+
--> $DIR/try_err.rs:101:23
44+
|
45+
LL | Err(_) => Err(ret_one!())?,
46+
| ^^^^^^^^^^^^^^^^ help: try this: `return Err(ret_one!())`
47+
...
48+
LL | try_validation_in_macro!(Ok::<_, i32>(5));
49+
| ------------------------------------------ in this macro invocation
50+
|
51+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
52+
53+
error: returning an `Err(_)` with the `?` operator
54+
--> $DIR/try_err.rs:140:9
4455
|
4556
LL | Err(foo!())?;
4657
| ^^^^^^^^^^^^ help: try this: `return Err(foo!())`
4758

4859
error: returning an `Err(_)` with the `?` operator
49-
--> $DIR/try_err.rs:129:9
60+
--> $DIR/try_err.rs:147:9
5061
|
5162
LL | Err(io::ErrorKind::WriteZero)?
5263
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Err(io::ErrorKind::WriteZero.into()))`
5364

5465
error: returning an `Err(_)` with the `?` operator
55-
--> $DIR/try_err.rs:131:9
66+
--> $DIR/try_err.rs:149:9
5667
|
5768
LL | Err(io::Error::new(io::ErrorKind::InvalidInput, "error"))?
5869
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Err(io::Error::new(io::ErrorKind::InvalidInput, "error")))`
5970

6071
error: returning an `Err(_)` with the `?` operator
61-
--> $DIR/try_err.rs:139:9
72+
--> $DIR/try_err.rs:157:9
6273
|
6374
LL | Err(io::ErrorKind::NotFound)?
6475
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `return Poll::Ready(Some(Err(io::ErrorKind::NotFound.into())))`
6576

66-
error: aborting due to 9 previous errors
77+
error: aborting due to 10 previous errors
6778

0 commit comments

Comments
 (0)