diff --git a/clippy_lints/src/matches/match_same_arms.rs b/clippy_lints/src/matches/match_same_arms.rs index a8312a04f36f..c7216130c9cc 100644 --- a/clippy_lints/src/matches/match_same_arms.rs +++ b/clippy_lints/src/matches/match_same_arms.rs @@ -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"); diff --git a/tests/ui/span_in_match_same_arm.fixed b/tests/ui/span_in_match_same_arm.fixed new file mode 100644 index 000000000000..726242c80fc5 --- /dev/null +++ b/tests/ui/span_in_match_same_arm.fixed @@ -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"), + } +} diff --git a/tests/ui/span_in_match_same_arm.rs b/tests/ui/span_in_match_same_arm.rs new file mode 100644 index 000000000000..edafec86e455 --- /dev/null +++ b/tests/ui/span_in_match_same_arm.rs @@ -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"), + } +} diff --git a/tests/ui/span_in_match_same_arm.stderr b/tests/ui/span_in_match_same_arm.stderr new file mode 100644 index 000000000000..69c3a3e688e1 --- /dev/null +++ b/tests/ui/span_in_match_same_arm.stderr @@ -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 +