From 03abe275b212890416b3015da8c2b323e946f424 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 30 Aug 2015 19:02:30 +0200 Subject: [PATCH] new lint: unnecessary patterns (x@_ -> x) --- README.md | 3 ++- src/lib.rs | 2 ++ src/misc.rs | 21 +++++++++++++++++++++ tests/compile-fail/patterns.rs | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100755 tests/compile-fail/patterns.rs diff --git a/README.md b/README.md index 4d5c59f2aef..cb9ee6671f3 100644 --- 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` diff --git a/src/lib.rs b/src/lib.rs index 5e9205e32f9..d3f45af0754 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, diff --git a/src/misc.rs b/src/misc.rs index 6e438407216..ccf67b0fae0 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -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 index 00000000000..62bd2c43cc1 --- /dev/null +++ b/tests/compile-fail/patterns.rs @@ -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 + } +}