Auto merge of #6069 - alex-700:redundant-pattern-matching-in-macro, r=Manishearth
Forbid redundant_pattern_matching triggering in macros fixes #6065 changelog: forbid redundant_pattern_matching triggering in macros
This commit is contained in:
commit
29b12f22fc
@ -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;
|
||||
}
|
||||
|
||||
|
@ -1,22 +0,0 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
enum Foo {
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
}
|
||||
|
||||
macro_rules! test_hash {
|
||||
($foo:expr, $($t:ident => $ord:expr),+ ) => {
|
||||
use self::Foo::*;
|
||||
match $foo {
|
||||
$ ( & $t => $ord,
|
||||
)*
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Foo::A;
|
||||
test_hash!(&a, A => 0, B => 1, C => 2);
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
error: you don't need to add `&` to both the expression and the patterns
|
||||
--> $DIR/ice-2636.rs:12:9
|
||||
|
|
||||
LL | / match $foo {
|
||||
LL | | $ ( & $t => $ord,
|
||||
LL | | )*
|
||||
LL | | };
|
||||
| |_________^
|
||||
...
|
||||
LL | test_hash!(&a, A => 0, B => 1, C => 2);
|
||||
| --------------------------------------- in this macro invocation
|
||||
|
|
||||
= note: `-D clippy::match-ref-pats` implied by `-D warnings`
|
||||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: aborting due to previous error
|
||||
|
@ -42,6 +42,7 @@ fn main() {
|
||||
|
||||
issue5504();
|
||||
issue6067();
|
||||
issue6065();
|
||||
|
||||
let _ = if gen_res().is_ok() {
|
||||
1
|
||||
@ -79,6 +80,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));
|
||||
}
|
||||
|
||||
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
|
||||
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
|
||||
// so the following should be linted.
|
||||
|
@ -54,6 +54,7 @@ fn main() {
|
||||
|
||||
issue5504();
|
||||
issue6067();
|
||||
issue6065();
|
||||
|
||||
let _ = if let Ok(_) = gen_res() {
|
||||
1
|
||||
@ -91,6 +92,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));
|
||||
}
|
||||
|
||||
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
|
||||
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
|
||||
// so the following should be linted.
|
||||
|
@ -73,67 +73,67 @@ LL | let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false };
|
||||
| -------^^^^^--------------------- help: try this: `if Ok::<usize, ()>(4).is_ok()`
|
||||
|
||||
error: redundant pattern matching, consider using `is_ok()`
|
||||
--> $DIR/redundant_pattern_matching.rs:58:20
|
||||
--> $DIR/redundant_pattern_matching.rs:59:20
|
||||
|
|
||||
LL | let _ = 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:60:19
|
||||
--> $DIR/redundant_pattern_matching.rs:61: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:83:19
|
||||
--> $DIR/redundant_pattern_matching.rs:84: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:84:16
|
||||
--> $DIR/redundant_pattern_matching.rs:85: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:90:12
|
||||
--> $DIR/redundant_pattern_matching.rs:91: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:91:15
|
||||
--> $DIR/redundant_pattern_matching.rs:92:15
|
||||
|
|
||||
LL | while let Some(_) = m!() {}
|
||||
| ----------^^^^^^^------- help: try this: `while m!().is_some()`
|
||||
|
||||
error: redundant pattern matching, consider using `is_ok()`
|
||||
--> $DIR/redundant_pattern_matching.rs:98:12
|
||||
--> $DIR/redundant_pattern_matching.rs:110:12
|
||||
|
|
||||
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
|
||||
| -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()`
|
||||
|
||||
error: redundant pattern matching, consider using `is_err()`
|
||||
--> $DIR/redundant_pattern_matching.rs:100:12
|
||||
--> $DIR/redundant_pattern_matching.rs:112:12
|
||||
|
|
||||
LL | if let Err(_) = Err::<i32, i32>(42) {}
|
||||
| -------^^^^^^---------------------- help: try this: `if Err::<i32, i32>(42).is_err()`
|
||||
|
||||
error: redundant pattern matching, consider using `is_ok()`
|
||||
--> $DIR/redundant_pattern_matching.rs:102:15
|
||||
--> $DIR/redundant_pattern_matching.rs:114:15
|
||||
|
|
||||
LL | while let Ok(_) = Ok::<i32, i32>(10) {}
|
||||
| ----------^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_ok()`
|
||||
|
||||
error: redundant pattern matching, consider using `is_err()`
|
||||
--> $DIR/redundant_pattern_matching.rs:104:15
|
||||
--> $DIR/redundant_pattern_matching.rs:116:15
|
||||
|
|
||||
LL | while let Err(_) = Ok::<i32, i32>(10) {}
|
||||
| ----------^^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_err()`
|
||||
|
||||
error: redundant pattern matching, consider using `is_ok()`
|
||||
--> $DIR/redundant_pattern_matching.rs:106:5
|
||||
--> $DIR/redundant_pattern_matching.rs:118:5
|
||||
|
|
||||
LL | / match Ok::<i32, i32>(42) {
|
||||
LL | | Ok(_) => true,
|
||||
@ -142,7 +142,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`
|
||||
|
||||
error: redundant pattern matching, consider using `is_err()`
|
||||
--> $DIR/redundant_pattern_matching.rs:111:5
|
||||
--> $DIR/redundant_pattern_matching.rs:123:5
|
||||
|
|
||||
LL | / match Err::<i32, i32>(42) {
|
||||
LL | | Ok(_) => false,
|
||||
|
Loading…
Reference in New Issue
Block a user