]> git.lizzy.rs Git - rust.git/commitdiff
new lint: unnecessary patterns (x@_ -> x)
authorGeorg Brandl <georg@python.org>
Sun, 30 Aug 2015 17:02:30 +0000 (19:02 +0200)
committerGeorg Brandl <georg@python.org>
Sun, 30 Aug 2015 17:02:30 +0000 (19:02 +0200)
README.md
src/lib.rs
src/misc.rs
tests/compile-fail/patterns.rs [new file with mode: 0755]

index 4d5c59f2aefd7ede2e56f49e5f26bffbdc2c3ad8..cb9ee6671f356a3aa12df9a1ac5ce6b943643af3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
 A collection of lints that give helpful tips to newbies and catch oversights.
 
 ##Lints
-There are 51 lints included in this crate:
+There are 52 lints included in this crate:
 
 name                                                                                                 | default | meaning
 -----------------------------------------------------------------------------------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -43,6 +43,7 @@ name
 [ptr_arg](https://github.com/Manishearth/rust-clippy/wiki#ptr_arg)                                   | allow   | fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively
 [range_step_by_zero](https://github.com/Manishearth/rust-clippy/wiki#range_step_by_zero)             | warn    | using Range::step_by(0), which produces an infinite iterator
 [redundant_closure](https://github.com/Manishearth/rust-clippy/wiki#redundant_closure)               | warn    | using redundant closures, i.e. `|a| foo(a)` (which can be written as just `foo`)
+[redundant_pattern](https://github.com/Manishearth/rust-clippy/wiki#redundant_pattern)               | warn    | using `name @ _` in a pattern
 [result_unwrap_used](https://github.com/Manishearth/rust-clippy/wiki#result_unwrap_used)             | allow   | using `Result.unwrap()`, which might be better handled
 [shadow_reuse](https://github.com/Manishearth/rust-clippy/wiki#shadow_reuse)                         | allow   | rebinding a name to an expression that re-uses the original value, e.g. `let x = x + 1`
 [shadow_same](https://github.com/Manishearth/rust-clippy/wiki#shadow_same)                           | allow   | rebinding a name to itself, e.g. `let mut x = &mut x`
index 5e9205e32f9cbce500d08383f2ecc9d67dd05cba..d3f45af0754d919cebc75fdccf70913260c3954c 100755 (executable)
@@ -74,6 +74,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
     reg.register_lint_pass(box types::CastPass as LintPassObject);
     reg.register_lint_pass(box types::TypeComplexityPass as LintPassObject);
     reg.register_lint_pass(box matches::MatchPass as LintPassObject);
+    reg.register_lint_pass(box misc::PatternPass as LintPassObject);
 
     reg.register_lint_group("shadow", vec![
         shadow::SHADOW_REUSE,
@@ -110,6 +111,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
         misc::FLOAT_CMP,
         misc::MODULO_ONE,
         misc::PRECEDENCE,
+        misc::REDUNDANT_PATTERN,
         misc::TOPLEVEL_REF_ARG,
         mut_mut::MUT_MUT,
         needless_bool::NEEDLESS_BOOL,
index 6e4384072169b2639ff210161c0b29f12e75f85a..ccf67b0fae0e75fe16bb4ee1b9622f25b02d2936 100644 (file)
@@ -235,3 +235,24 @@ fn is_lit_one(expr: &Expr) -> bool {
     }
     false
 }
+
+declare_lint!(pub REDUNDANT_PATTERN, Warn, "using `name @ _` in a pattern");
+
+#[derive(Copy,Clone)]
+pub struct PatternPass;
+
+impl LintPass for PatternPass {
+    fn get_lints(&self) -> LintArray {
+        lint_array!(REDUNDANT_PATTERN)
+    }
+
+    fn check_pat(&mut self, cx: &Context, pat: &Pat) {
+        if let PatIdent(_, ref ident, Some(ref right)) = pat.node {
+            if right.node == PatWild(PatWildSingle) {
+                cx.span_lint(REDUNDANT_PATTERN, pat.span, &format!(
+                    "the `{} @ _` pattern can be written as just `{}`",
+                    ident.node.name, ident.node.name));
+            }
+        }
+    }
+}
diff --git a/tests/compile-fail/patterns.rs b/tests/compile-fail/patterns.rs
new file mode 100755 (executable)
index 0000000..62bd2c4
--- /dev/null
@@ -0,0 +1,16 @@
+#![feature(plugin)]
+#![plugin(clippy)]
+#![allow(unused)]
+#![deny(clippy)]
+
+fn main() {
+    let v = Some(true);
+    match v {
+        Some(x) => (),
+        y @ _   => (),  //~ERROR the `y @ _` pattern can be written as just `y`
+    }
+    match v {
+        Some(x)  => (),
+        y @ None => (),  // no error
+    }
+}