new lint: unnecessary patterns (x@_ -> x)

This commit is contained in:
Georg Brandl 2015-08-30 19:02:30 +02:00
parent 298728ed65
commit 03abe275b2
4 changed files with 41 additions and 1 deletions

View File

@ -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`

View File

@ -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,

View 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));
}
}
}
}

16
tests/compile-fail/patterns.rs Executable file
View File

@ -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
}
}