about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-09-05 16:15:36 +0000
committerbors <bors@rust-lang.org>2019-09-05 16:15:36 +0000
commit313b41fac5f3e1133aa942d510de41e383830c81 (patch)
tree742e221ed03f228b8a02be742c6a07d1de67e9db
parent5f28fda13efeb85097fe082af4b4827a58b0ecf6 (diff)
parent0b3f4527641197510efe9bbe034c07c1bcf41d61 (diff)
downloadrust-313b41fac5f3e1133aa942d510de41e383830c81.tar.gz
rust-313b41fac5f3e1133aa942d510de41e383830c81.zip
Auto merge of #4489 - JohnTitor:fix-redundant-pattern-false-positive, r=flip1995
Fix `redundant_pattern` false positive

Fix #4428

changelog: Fix `redundant_pattern` false positive
-rw-r--r--clippy_lints/src/lib.rs4
-rw-r--r--clippy_lints/src/misc.rs39
-rw-r--r--clippy_lints/src/misc_early.rs42
-rw-r--r--src/lintlist/mod.rs2
-rw-r--r--tests/ui/patterns.fixed21
-rw-r--r--tests/ui/patterns.rs7
-rw-r--r--tests/ui/patterns.stderr4
7 files changed, 74 insertions, 45 deletions
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index a9294da59b6..9d0a91c5318 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -815,7 +815,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
         misc::CMP_OWNED,
         misc::FLOAT_CMP,
         misc::MODULO_ONE,
-        misc::REDUNDANT_PATTERN,
         misc::SHORT_CIRCUIT_STATEMENT,
         misc::TOPLEVEL_REF_ARG,
         misc::ZERO_PTR,
@@ -824,6 +823,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
         misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
         misc_early::MIXED_CASE_HEX_LITERALS,
         misc_early::REDUNDANT_CLOSURE_CALL,
+        misc_early::REDUNDANT_PATTERN,
         misc_early::UNNEEDED_FIELD_PATTERN,
         misc_early::ZERO_PREFIXED_LITERAL,
         mut_reference::UNNECESSARY_MUT_PASSED,
@@ -967,13 +967,13 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
         methods::STRING_EXTEND_CHARS,
         methods::UNNECESSARY_FOLD,
         methods::WRONG_SELF_CONVENTION,
-        misc::REDUNDANT_PATTERN,
         misc::TOPLEVEL_REF_ARG,
         misc::ZERO_PTR,
         misc_early::BUILTIN_TYPE_SHADOW,
         misc_early::DOUBLE_NEG,
         misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
         misc_early::MIXED_CASE_HEX_LITERALS,
+        misc_early::REDUNDANT_PATTERN,
         misc_early::UNNEEDED_FIELD_PATTERN,
         mut_reference::UNNECESSARY_MUT_PASSED,
         neg_multiply::NEG_MULTIPLY,
diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs
index c6bcb845c7a..a761f80b793 100644
--- a/clippy_lints/src/misc.rs
+++ b/clippy_lints/src/misc.rs
@@ -137,28 +137,6 @@ declare_clippy_lint! {
 }
 
 declare_clippy_lint! {
-    /// **What it does:** Checks for patterns in the form `name @ _`.
-    ///
-    /// **Why is this bad?** It's almost always more readable to just use direct
-    /// bindings.
-    ///
-    /// **Known problems:** None.
-    ///
-    /// **Example:**
-    /// ```rust
-    /// # let v = Some("abc");
-    ///
-    /// match v {
-    ///     Some(x) => (),
-    ///     y @ _ => (), // easier written as `y`,
-    /// }
-    /// ```
-    pub REDUNDANT_PATTERN,
-    style,
-    "using `name @ _` in a pattern"
-}
-
-declare_clippy_lint! {
     /// **What it does:** Checks for the use of bindings with a single leading
     /// underscore.
     ///
@@ -247,7 +225,6 @@ declare_lint_pass!(MiscLints => [
     FLOAT_CMP,
     CMP_OWNED,
     MODULO_ONE,
-    REDUNDANT_PATTERN,
     USED_UNDERSCORE_BINDING,
     SHORT_CIRCUIT_STATEMENT,
     ZERO_PTR,
@@ -459,22 +436,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
             );
         }
     }
-
-    fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
-        if let PatKind::Binding(.., ident, Some(ref right)) = pat.node {
-            if let PatKind::Wild = right.node {
-                span_lint(
-                    cx,
-                    REDUNDANT_PATTERN,
-                    pat.span,
-                    &format!(
-                        "the `{} @ _` pattern can be written as just `{}`",
-                        ident.name, ident.name
-                    ),
-                );
-            }
-        }
-    }
 }
 
 fn check_nan(cx: &LateContext<'_, '_>, path: &Path, expr: &Expr) {
diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs
index b90307af8ff..47446edac54 100644
--- a/clippy_lints/src/misc_early.rs
+++ b/clippy_lints/src/misc_early.rs
@@ -173,6 +173,28 @@ declare_clippy_lint! {
     "shadowing a builtin type"
 }
 
+declare_clippy_lint! {
+    /// **What it does:** Checks for patterns in the form `name @ _`.
+    ///
+    /// **Why is this bad?** It's almost always more readable to just use direct
+    /// bindings.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// # let v = Some("abc");
+    ///
+    /// match v {
+    ///     Some(x) => (),
+    ///     y @ _ => (), // easier written as `y`,
+    /// }
+    /// ```
+    pub REDUNDANT_PATTERN,
+    style,
+    "using `name @ _` in a pattern"
+}
+
 declare_lint_pass!(MiscEarlyLints => [
     UNNEEDED_FIELD_PATTERN,
     DUPLICATE_UNDERSCORE_ARGUMENT,
@@ -181,7 +203,8 @@ declare_lint_pass!(MiscEarlyLints => [
     MIXED_CASE_HEX_LITERALS,
     UNSEPARATED_LITERAL_SUFFIX,
     ZERO_PREFIXED_LITERAL,
-    BUILTIN_TYPE_SHADOW
+    BUILTIN_TYPE_SHADOW,
+    REDUNDANT_PATTERN
 ]);
 
 // Used to find `return` statements or equivalents e.g., `?`
@@ -286,6 +309,23 @@ impl EarlyLintPass for MiscEarlyLints {
                 }
             }
         }
+
+        if let PatKind::Ident(_, ident, Some(ref right)) = pat.node {
+            if let PatKind::Wild = right.node {
+                span_lint_and_sugg(
+                    cx,
+                    REDUNDANT_PATTERN,
+                    pat.span,
+                    &format!(
+                        "the `{} @ _` pattern can be written as just `{}`",
+                        ident.name, ident.name,
+                    ),
+                    "try",
+                    format!("{}", ident.name),
+                    Applicability::MachineApplicable,
+                );
+            }
+        }
     }
 
     fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) {
diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs
index 1435e9968dd..223e6aa9acd 100644
--- a/src/lintlist/mod.rs
+++ b/src/lintlist/mod.rs
@@ -1552,7 +1552,7 @@ pub const ALL_LINTS: [Lint; 313] = [
         group: "style",
         desc: "using `name @ _` in a pattern",
         deprecation: None,
-        module: "misc",
+        module: "misc_early",
     },
     Lint {
         name: "redundant_pattern_matching",
diff --git a/tests/ui/patterns.fixed b/tests/ui/patterns.fixed
new file mode 100644
index 00000000000..a443db7495d
--- /dev/null
+++ b/tests/ui/patterns.fixed
@@ -0,0 +1,21 @@
+// run-rustfix
+#![allow(unused)]
+#![warn(clippy::all)]
+#![feature(slice_patterns)]
+
+fn main() {
+    let v = Some(true);
+    let s = [0, 1, 2, 3, 4];
+    match v {
+        Some(x) => (),
+        y => (),
+    }
+    match v {
+        Some(x) => (),
+        y @ None => (), // no error
+    }
+    match s {
+        [x, inside @ .., y] => (), // no error
+        [..] => (),
+    }
+}
diff --git a/tests/ui/patterns.rs b/tests/ui/patterns.rs
index 576e6c9ab92..2c9f839ecf6 100644
--- a/tests/ui/patterns.rs
+++ b/tests/ui/patterns.rs
@@ -1,8 +1,11 @@
+// run-rustfix
 #![allow(unused)]
 #![warn(clippy::all)]
+#![feature(slice_patterns)]
 
 fn main() {
     let v = Some(true);
+    let s = [0, 1, 2, 3, 4];
     match v {
         Some(x) => (),
         y @ _ => (),
@@ -11,4 +14,8 @@ fn main() {
         Some(x) => (),
         y @ None => (), // no error
     }
+    match s {
+        [x, inside @ .., y] => (), // no error
+        [..] => (),
+    }
 }
diff --git a/tests/ui/patterns.stderr b/tests/ui/patterns.stderr
index 39dc034a014..784a3feaace 100644
--- a/tests/ui/patterns.stderr
+++ b/tests/ui/patterns.stderr
@@ -1,8 +1,8 @@
 error: the `y @ _` pattern can be written as just `y`
-  --> $DIR/patterns.rs:8:9
+  --> $DIR/patterns.rs:11:9
    |
 LL |         y @ _ => (),
-   |         ^^^^^
+   |         ^^^^^ help: try: `y`
    |
    = note: `-D clippy::redundant-pattern` implied by `-D warnings`