Skip to content

Commit bfef48f

Browse files
committed
Auto merge of #4721 - phansch:fix_try_err_in_ext_macro, r=flip1995
Don't emit try_err lint in external macros changelog: Fix [`try_err`] false positive in external macros Closes #4709
2 parents 37ea436 + 52f5290 commit bfef48f

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

clippy_lints/src/try_err.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::{match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc::hir::*;
4-
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4+
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass};
55
use rustc::ty::Ty;
66
use rustc::{declare_lint_pass, declare_tool_lint};
77
use rustc_errors::Applicability;
@@ -54,6 +54,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
5454
// val,
5555
// };
5656
if_chain! {
57+
if !in_external_macro(cx.tcx.sess, expr.span);
5758
if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.kind;
5859
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.kind;
5960
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;

tests/ui/auxiliary/macro_rules.rs

+15
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,18 @@ macro_rules! must_use_unit {
1616
fn foo() {}
1717
};
1818
}
19+
20+
#[macro_export]
21+
macro_rules! try_err {
22+
() => {
23+
pub fn try_err_fn() -> Result<i32, i32> {
24+
let err: i32 = 1;
25+
// To avoid warnings during rustfix
26+
if true {
27+
Err(err)?
28+
} else {
29+
Ok(2)
30+
}
31+
}
32+
};
33+
}

tests/ui/try_err.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// run-rustfix
2+
// aux-build:macro_rules.rs
23

34
#![deny(clippy::try_err)]
45

6+
#[macro_use]
7+
extern crate macro_rules;
8+
59
// Tests that a simple case works
610
// Should flag `Err(err)?`
711
pub fn basic_test() -> Result<i32, i32> {
@@ -77,6 +81,9 @@ fn main() {
7781
negative_test().unwrap();
7882
closure_matches_test().unwrap();
7983
closure_into_test().unwrap();
84+
85+
// We don't want to lint in external macros
86+
try_err!();
8087
}
8188

8289
macro_rules! bar {

tests/ui/try_err.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// run-rustfix
2+
// aux-build:macro_rules.rs
23

34
#![deny(clippy::try_err)]
45

6+
#[macro_use]
7+
extern crate macro_rules;
8+
59
// Tests that a simple case works
610
// Should flag `Err(err)?`
711
pub fn basic_test() -> Result<i32, i32> {
@@ -77,6 +81,9 @@ fn main() {
7781
negative_test().unwrap();
7882
closure_matches_test().unwrap();
7983
closure_into_test().unwrap();
84+
85+
// We don't want to lint in external macros
86+
try_err!();
8087
}
8188

8289
macro_rules! bar {

tests/ui/try_err.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
error: returning an `Err(_)` with the `?` operator
2-
--> $DIR/try_err.rs:11:9
2+
--> $DIR/try_err.rs:15:9
33
|
44
LL | Err(err)?;
55
| ^^^^^^^^^ help: try this: `return Err(err)`
66
|
77
note: lint level defined here
8-
--> $DIR/try_err.rs:3:9
8+
--> $DIR/try_err.rs:4:9
99
|
1010
LL | #![deny(clippy::try_err)]
1111
| ^^^^^^^^^^^^^^^
1212

1313
error: returning an `Err(_)` with the `?` operator
14-
--> $DIR/try_err.rs:21:9
14+
--> $DIR/try_err.rs:25:9
1515
|
1616
LL | Err(err)?;
1717
| ^^^^^^^^^ help: try this: `return Err(err.into())`
1818

1919
error: returning an `Err(_)` with the `?` operator
20-
--> $DIR/try_err.rs:41:17
20+
--> $DIR/try_err.rs:45:17
2121
|
2222
LL | Err(err)?;
2323
| ^^^^^^^^^ help: try this: `return Err(err)`
2424

2525
error: returning an `Err(_)` with the `?` operator
26-
--> $DIR/try_err.rs:60:17
26+
--> $DIR/try_err.rs:64:17
2727
|
2828
LL | Err(err)?;
2929
| ^^^^^^^^^ help: try this: `return Err(err.into())`
3030

3131
error: returning an `Err(_)` with the `?` operator
32-
--> $DIR/try_err.rs:96:9
32+
--> $DIR/try_err.rs:103:9
3333
|
3434
LL | Err(foo!())?;
3535
| ^^^^^^^^^^^^ help: try this: `return Err(foo!())`

0 commit comments

Comments
 (0)