about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksei Latyshev <alex_700_95@mail.ru>2020-09-20 12:38:23 +0300
committerAleksei Latyshev <alex_700_95@mail.ru>2020-09-21 20:49:42 +0300
commitd4f158fa5cd5f4ae1cc690b37b0a8850cb013b69 (patch)
tree3f4e96dc95842f84fc8a44db076423fedfcce797
parent78fbb04edbcd6c7065d87ffcdb02314d1c8a3e6d (diff)
downloadrust-d4f158fa5cd5f4ae1cc690b37b0a8850cb013b69.tar.gz
rust-d4f158fa5cd5f4ae1cc690b37b0a8850cb013b69.zip
Forbid redundant_pattern_matching triggering in macros
- remove ice-2636 test
-rw-r--r--clippy_lints/src/matches.rs2
-rw-r--r--tests/ui/crashes/ice-2636.rs22
-rw-r--r--tests/ui/crashes/ice-2636.stderr17
-rw-r--r--tests/ui/redundant_pattern_matching.fixed12
-rw-r--r--tests/ui/redundant_pattern_matching.rs12
-rw-r--r--tests/ui/redundant_pattern_matching.stderr24
6 files changed, 37 insertions, 52 deletions
diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs
index 6f47687c410..b1a4e06d4c3 100644
--- a/clippy_lints/src/matches.rs
+++ b/clippy_lints/src/matches.rs
@@ -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;
         }
 
diff --git a/tests/ui/crashes/ice-2636.rs b/tests/ui/crashes/ice-2636.rs
deleted file mode 100644
index e0b58157590..00000000000
--- a/tests/ui/crashes/ice-2636.rs
+++ /dev/null
@@ -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);
-}
diff --git a/tests/ui/crashes/ice-2636.stderr b/tests/ui/crashes/ice-2636.stderr
deleted file mode 100644
index 53799b4fbf1..00000000000
--- a/tests/ui/crashes/ice-2636.stderr
+++ /dev/null
@@ -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
-
diff --git a/tests/ui/redundant_pattern_matching.fixed b/tests/ui/redundant_pattern_matching.fixed
index 17d908336d5..fe8f62503b7 100644
--- a/tests/ui/redundant_pattern_matching.fixed
+++ b/tests/ui/redundant_pattern_matching.fixed
@@ -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.
diff --git a/tests/ui/redundant_pattern_matching.rs b/tests/ui/redundant_pattern_matching.rs
index d57fbb14ae4..09426a6e590 100644
--- a/tests/ui/redundant_pattern_matching.rs
+++ b/tests/ui/redundant_pattern_matching.rs
@@ -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.
diff --git a/tests/ui/redundant_pattern_matching.stderr b/tests/ui/redundant_pattern_matching.stderr
index 955900f3e6c..3473ceea00e 100644
--- a/tests/ui/redundant_pattern_matching.stderr
+++ b/tests/ui/redundant_pattern_matching.stderr
@@ -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,