Avoid redundant multiple pattern suggested when one pattern is _

This commit is contained in:
d-dorazio 2016-10-08 15:16:00 +02:00
parent e03c969322
commit a4d4a372e0
2 changed files with 23 additions and 2 deletions

View file

@ -201,7 +201,14 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
if i.pats.len() == 1 && j.pats.len() == 1 { if i.pats.len() == 1 && j.pats.len() == 1 {
let lhs = snippet(cx, i.pats[0].span, "<pat1>"); let lhs = snippet(cx, i.pats[0].span, "<pat1>");
let rhs = snippet(cx, j.pats[0].span, "<pat2>"); let rhs = snippet(cx, j.pats[0].span, "<pat2>");
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));
}
} }
}); });
} }

View file

@ -18,6 +18,12 @@ struct Foo {
bar: u8, bar: u8,
} }
pub enum Abc {
A,
B,
C,
}
#[deny(if_same_then_else)] #[deny(if_same_then_else)]
#[deny(match_same_arms)] #[deny(match_same_arms)]
fn if_same_then_else() -> Result<&'static str, ()> { fn if_same_then_else() -> Result<&'static str, ()> {
@ -73,7 +79,7 @@ fn if_same_then_else() -> Result<&'static str, ()> {
let _ = match 42 { let _ = match 42 {
42 => { 42 => {
//~^ NOTE same as this //~^ NOTE same as this
//~| NOTE refactoring //~| NOTE removing
foo(); foo();
let mut a = 42 + [23].len() as i32; let mut a = 42 + [23].len() as i32;
if true { 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 { if true {
foo(); foo();
} }