Merge branch 'pr-260'

Conflicts:
	src/lib.rs
This commit is contained in:
Manish Goregaokar 2015-08-31 15:06:53 +05:30
commit d3da9f6c81
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

@ -75,6 +75,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::CMP_OWNED,
misc::FLOAT_CMP,
misc::MODULO_ONE,
misc::REDUNDANT_PATTERN,
misc::TOPLEVEL_REF_ARG,
mut_mut::MUT_MUT,
needless_bool::NEEDLESS_BOOL,

View file

@ -191,3 +191,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
}
}