From a4d4a372e0d5beaa521cfca693057c7fc3eae87a Mon Sep 17 00:00:00 2001 From: d-dorazio Date: Sat, 8 Oct 2016 15:16:00 +0200 Subject: [PATCH] Avoid redundant multiple pattern suggested when one pattern is _ --- clippy_lints/src/copies.rs | 9 ++++++++- tests/compile-fail/copies.rs | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index b2439cacba2..1245ec97051 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -201,7 +201,14 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) { if i.pats.len() == 1 && j.pats.len() == 1 { let lhs = snippet(cx, i.pats[0].span, ""); let rhs = snippet(cx, j.pats[0].span, ""); - db.span_note(i.body.span, &format!("consider refactoring into `{} | {}`", lhs, rhs)); + + if let PatKind::Wild = j.pats[0].node { + // if the last arm is _, then i could be integrated into _ + // note that i.pats[0] cannot be _, because that would mean that we're hiding all the subsequent arms, and rust won't compile + db.span_note(i.body.span, &format!("`{}` has the same arm body as the `_` wildcard, consider removing it`", lhs)); + } else { + db.span_note(i.body.span, &format!("consider refactoring into `{} | {}`", lhs, rhs)); + } } }); } diff --git a/tests/compile-fail/copies.rs b/tests/compile-fail/copies.rs index dba653a148f..d9eb842d680 100644 --- a/tests/compile-fail/copies.rs +++ b/tests/compile-fail/copies.rs @@ -18,6 +18,12 @@ struct Foo { bar: u8, } +pub enum Abc { + A, + B, + C, +} + #[deny(if_same_then_else)] #[deny(match_same_arms)] fn if_same_then_else() -> Result<&'static str, ()> { @@ -73,7 +79,7 @@ fn if_same_then_else() -> Result<&'static str, ()> { let _ = match 42 { 42 => { //~^ NOTE same as this - //~| NOTE refactoring + //~| NOTE removing foo(); let mut a = 42 + [23].len() as i32; if true { @@ -93,6 +99,14 @@ fn if_same_then_else() -> Result<&'static str, ()> { } }; + let _ = match Abc::A { + Abc::A => 0, + //~^ NOTE same as this + //~| NOTE removing + Abc::B => 1, + _ => 0, //~ERROR this `match` has identical arm bodies + }; + if true { foo(); }