Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clippy_lints/src/matches/match_same_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
continue;
}

// Skip the first (original) arm and only highlight duplicates.
// This avoids pointing at the non-redundant arm and keeps the lint clear.
let spans = group.iter().skip(1).map(|(_, arm)| arm.span).collect_vec();

span_lint_and_then(
cx,
MATCH_SAME_ARMS,
group.iter().map(|(_, arm)| arm.span).collect_vec(),
spans,
"these match arms have identical bodies",
|diag| {
diag.help("if this is unintentional make the arms return different values");
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/span_in_match_same_arm.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![warn(clippy::match_same_arms)]

fn multiple_duplicate_groups() {
let y = 5;

match y {
//~ ERROR: these match arms have identical bodies
3 => println!("different"),
1 | 2 | 4 | 5 => println!("same"),
_ => println!("other"),
}
}
14 changes: 14 additions & 0 deletions tests/ui/span_in_match_same_arm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![warn(clippy::match_same_arms)]

fn multiple_duplicate_groups() {
let y = 5;

match y {
1 => println!("same"),
2 => println!("same"), //~ ERROR: these match arms have identical bodies
3 => println!("different"),
4 => println!("same"),
5 => println!("same"),
_ => println!("other"),
}
}
23 changes: 23 additions & 0 deletions tests/ui/span_in_match_same_arm.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: these match arms have identical bodies
--> tests/ui/span_in_match_same_arm.rs:8:9
|
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
LL | 3 => println!("different"),
LL | 4 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
LL | 5 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
= note: `-D clippy::match-same-arms` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::match_same_arms)]`
help: otherwise merge the patterns into a single arm
|
LL ~
LL | 3 => println!("different"),
LL ~ 1 | 2 | 4 | 5 => println!("same"),
|

error: aborting due to 1 previous error

Loading