Skip to content

Commit

Permalink
Forbid redundant_pattern_matching triggering in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-700 committed Sep 20, 2020
1 parent d88b9b7 commit ca35e81
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ impl_lint_pass!(Matches => [

impl<'tcx> LateLintPass<'tcx> for Matches {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if in_external_macro(cx.sess(), expr.span) {
if in_external_macro(cx.sess(), expr.span) || in_macro(expr.span) {
return;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/redundant_pattern_matching.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn main() {

issue5504();
issue5697();
issue6065();

let _ = if gen_opt().is_some() {
1
Expand Down Expand Up @@ -128,6 +129,17 @@ fn issue5504() {
while m!().is_some() {}
}

fn issue6065() {
macro_rules! if_let_in_macro {
($pat:pat, $x:expr) => {
if let Some($pat) = $x {}
};
}

// shouldn't be linted
if_let_in_macro!(_, Some(42));
}

// None of these should be linted because none of the suggested methods
// are `const fn` without toggling a feature.
const fn issue5697() {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/redundant_pattern_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ fn main() {

issue5504();
issue5697();
issue6065();

let _ = if let Some(_) = gen_opt() {
1
Expand Down Expand Up @@ -149,6 +150,17 @@ fn issue5504() {
while let Some(_) = m!() {}
}

fn issue6065() {
macro_rules! if_let_in_macro {
($pat:pat, $x:expr) => {
if let Some($pat) = $x {}
};
}

// shouldn't be linted
if_let_in_macro!(_, Some(42));
}

// None of these should be linted because none of the suggested methods
// are `const fn` without toggling a feature.
const fn issue5697() {
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/redundant_pattern_matching.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -149,49 +149,49 @@ LL | let x = if let Some(_) = opt { true } else { false };
| -------^^^^^^^------ help: try this: `if opt.is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:102:20
--> $DIR/redundant_pattern_matching.rs:103:20
|
LL | let _ = if let Some(_) = gen_opt() {
| -------^^^^^^^------------ help: try this: `if gen_opt().is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching.rs:104:19
--> $DIR/redundant_pattern_matching.rs:105:19
|
LL | } else if let None = gen_opt() {
| -------^^^^------------ help: try this: `if gen_opt().is_none()`

error: redundant pattern matching, consider using `is_ok()`
--> $DIR/redundant_pattern_matching.rs:106:19
--> $DIR/redundant_pattern_matching.rs:107:19
|
LL | } else if let Ok(_) = gen_res() {
| -------^^^^^------------ help: try this: `if gen_res().is_ok()`

error: redundant pattern matching, consider using `is_err()`
--> $DIR/redundant_pattern_matching.rs:108:19
--> $DIR/redundant_pattern_matching.rs:109:19
|
LL | } else if let Err(_) = gen_res() {
| -------^^^^^^------------ help: try this: `if gen_res().is_err()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:141:19
--> $DIR/redundant_pattern_matching.rs:142:19
|
LL | while let Some(_) = r#try!(result_opt()) {}
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:142:16
--> $DIR/redundant_pattern_matching.rs:143:16
|
LL | if let Some(_) = r#try!(result_opt()) {}
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:148:12
--> $DIR/redundant_pattern_matching.rs:149:12
|
LL | if let Some(_) = m!() {}
| -------^^^^^^^------- help: try this: `if m!().is_some()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching.rs:149:15
--> $DIR/redundant_pattern_matching.rs:150:15
|
LL | while let Some(_) = m!() {}
| ----------^^^^^^^------- help: try this: `while m!().is_some()`
Expand Down

0 comments on commit ca35e81

Please sign in to comment.