Skip to content

Commit dc91b73

Browse files
authored
Merge pull request #1273 from d-dorazio/1272
Avoid redundant multiple pattern suggested when one pattern is _
2 parents e03c969 + a4d4a37 commit dc91b73

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

clippy_lints/src/copies.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,14 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
201201
if i.pats.len() == 1 && j.pats.len() == 1 {
202202
let lhs = snippet(cx, i.pats[0].span, "<pat1>");
203203
let rhs = snippet(cx, j.pats[0].span, "<pat2>");
204-
db.span_note(i.body.span, &format!("consider refactoring into `{} | {}`", lhs, rhs));
204+
205+
if let PatKind::Wild = j.pats[0].node {
206+
// if the last arm is _, then i could be integrated into _
207+
// note that i.pats[0] cannot be _, because that would mean that we're hiding all the subsequent arms, and rust won't compile
208+
db.span_note(i.body.span, &format!("`{}` has the same arm body as the `_` wildcard, consider removing it`", lhs));
209+
} else {
210+
db.span_note(i.body.span, &format!("consider refactoring into `{} | {}`", lhs, rhs));
211+
}
205212
}
206213
});
207214
}

tests/compile-fail/copies.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ struct Foo {
1818
bar: u8,
1919
}
2020

21+
pub enum Abc {
22+
A,
23+
B,
24+
C,
25+
}
26+
2127
#[deny(if_same_then_else)]
2228
#[deny(match_same_arms)]
2329
fn if_same_then_else() -> Result<&'static str, ()> {
@@ -73,7 +79,7 @@ fn if_same_then_else() -> Result<&'static str, ()> {
7379
let _ = match 42 {
7480
42 => {
7581
//~^ NOTE same as this
76-
//~| NOTE refactoring
82+
//~| NOTE removing
7783
foo();
7884
let mut a = 42 + [23].len() as i32;
7985
if true {
@@ -93,6 +99,14 @@ fn if_same_then_else() -> Result<&'static str, ()> {
9399
}
94100
};
95101

102+
let _ = match Abc::A {
103+
Abc::A => 0,
104+
//~^ NOTE same as this
105+
//~| NOTE removing
106+
Abc::B => 1,
107+
_ => 0, //~ERROR this `match` has identical arm bodies
108+
};
109+
96110
if true {
97111
foo();
98112
}

0 commit comments

Comments
 (0)