-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Summary
In #13803 together with @blyxyas we discovered that match_same_arms does not deal with cases where more than 2 arms have the same body. E.g. this test:
rust-clippy/tests/ui/match_same_arms.rs
Lines 31 to 38 in 6a3ef93
| let _ = match 42 { | |
| 1 => 2, | |
| 2 => 2, //~ ERROR: this match arm has an identical body to another arm | |
| //~^ ERROR: this match arm has an identical body to another arm | |
| 3 => 2, //~ ERROR: this match arm has an identical body to another arm | |
| 4 => 3, | |
| _ => 0, | |
| }; |
Will generate two overlapping suggestions - one to combine 1 and 2, one to combine 2 and 3. This means the lint cannot be automatically applied, and @rust-nofix remains.
It looks like the issue is that the arms are matched up pairwise, rather than collecting all that have the same body and suggesting an aggregate fix:
rust-clippy/clippy_lints/src/matches/match_same_arms.rs
Lines 115 to 116 in 6a3ef93
| for (&(i, arm1), &(j, arm2)) in search_same(&indexed_arms, hash, eq) { | |
| if matches!(arm2.pat.kind, PatKind::Wild) { |
We should change this to aggregate all equivalent bodies and provide a single suggestion to combine them.
Reproducer
Remove @rust-nofix from match_same_arms and check out the overlapping application failure for the test code linked above.
Version
Additional Labels
No response