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
16 changes: 14 additions & 2 deletions clippy_lints/src/matches/match_same_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,19 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
span_lint_and_then(
cx,
MATCH_SAME_ARMS,
group.iter().map(|(_, arm)| arm.span).collect_vec(),
group
.iter()
.enumerate() // gives index
.map(|(i, (_, arm))| {
// i gives postion of the arm
if i == 0 {
//target only primary arm
adjusted_arm_span(cx, arm.span) //fixes the span to include the comma and whitespaces
} else {
arm.span // other arms keep the original span
}
})
.collect_vec(),
"these match arms have identical bodies",
|diag| {
diag.help("if this is unintentional make the arms return different values");
Expand Down Expand Up @@ -177,7 +189,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
fn adjusted_arm_span(cx: &LateContext<'_>, span: Span) -> Span {
let source_map = cx.sess().source_map();
source_map
.span_extend_while(span, |c| c == ',' || c.is_ascii_whitespace())
.span_extend_while(span, |c| c == ',' || c.is_whitespace())
.unwrap_or(span)
}

Expand Down
89 changes: 89 additions & 0 deletions tests/ui/match_same_arm_vertical_tab2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#![warn(clippy::match_same_arms)]
#![allow(clippy::manual_range_patterns)]

// This test ensures `match_same_arms` correctly handles all Rust whitespace.
//@no-rustfix
#[rustfmt::skip]
fn main() {
let x = 1;

// ================= ASCII WHITESPACE =================

// Space
match x {
1 => println!("same"), //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {},
}

// Horizontal tab \t
match x {
1 => println!("same"), /* TAB */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {},
}

// ================= IMPORTANT BUG TARGET =================

// Vertical tab (U+000B)
match x {
1 => println!("same"), /* VT (U+000B) */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {},
}

// ================= OTHER NON-ASCII WHITESPACE =================

// Form feed (U+000C)
match x {
1 => println!("same"), /* FF (U+000C) */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {},
}

// Next line (U+0085)
match x {
1 => println!("same"), /* NEL (U+0085) */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {},
}

// ================= UNICODE WHITESPACE =================

// Left-to-right mark (U+200E)
match x {
1 => println!("same"), /* LRM (U+200E) */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {}
}

// Right-to-left mark (U+200F)
match x {
1 => println!("same"), /* RLM (U+200F) */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {}
}

// Line separator (U+2028)
match x {
1 => println!("same"), /* LS (U+2028) */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {},
}

// Paragraph separator (U+2029)
match x {
1 => println!("same"), /* PS (U+2029) */ //~ ERROR: these match arms have identical bodies
2 => println!("same"),
_ => {},
}

// ================= MULTIPLE DUPLICATE ARMS =================

match x {
1 => println!("same"), //~ ERROR: these match arms have identical bodies
2 => println!("same"),
3 => println!("same"),
_ => {},
}
}
156 changes: 156 additions & 0 deletions tests/ui/match_same_arm_vertical_tab2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:14:9
|
LL | 1 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => 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 ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:21:9
|
LL | 1 => println!("same"), /* TAB */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* TAB */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:30:9
|
LL | 1 => println!("same"), /* VT (U+000B) */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* VT (U+000B) */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:39:9
|
LL | 1 => println!("same"), /* FF (U+000C) */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* FF (U+000C) */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:46:9
|
LL | 1 => println!("same"), /* NEL (U+0085) */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* NEL (U+0085) */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:55:9
|
LL | 1 => println!("same"), /* LRM (U+200E) */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* LRM (U+200E) */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:62:9
|
LL | 1 => println!("same"), /* RLM (U+200F) */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* RLM (U+200F) */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:69:9
|
LL | 1 => println!("same"), /* LS (U+2028) */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* LS (U+2028) */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:76:9
|
LL | 1 => println!("same"), /* PS (U+2029) */
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~ /* PS (U+2029) */
LL ~ 1 | 2 => println!("same"),
|

error: these match arms have identical bodies
--> tests/ui/match_same_arm_vertical_tab2.rs:84:9
|
LL | 1 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^^^
LL | 2 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
LL | 3 => println!("same"),
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is unintentional make the arms return different values
help: otherwise merge the patterns into a single arm
|
LL ~
LL ~ 1 | 2 | 3 => println!("same"),
|

error: aborting due to 10 previous errors

Loading
Loading