Skip to content

Commit 2e7b2ba

Browse files
committed
add tests for collapsible_match: no lint when intermediate non-wildcard arm present
1 parent 0c14721 commit 2e7b2ba

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

tests/ui/collapsible_match.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,26 @@ fn take<T>(t: T) {}
390390

391391
fn main() {}
392392

393+
// https://github.com/rust-lang/rust-clippy/issues/16875
394+
// Adding a match guard allows fall-through to subsequent arms, which changes semantics
395+
// when non-wildcard arms follow the arm being collapsed.
396+
fn issue16875(a: Option<&str>, b: i32) -> i32 {
397+
let mut res = 0;
398+
// should NOT lint: `_ if b == 1` is not wild-like (has a guard), so collapsing
399+
// `Some(_)` into `Some(_) if b == 0` would let `_ if b == 1` match Some values
400+
// that previously fell through to the no-op arm body.
401+
match a {
402+
Some(_) => {
403+
if b == 0 {
404+
res = 1;
405+
}
406+
},
407+
_ if b == 1 => res = 2,
408+
_ => {},
409+
}
410+
res
411+
}
412+
393413
fn issue16705(x: Option<String>) {
394414
fn takes_ownership(s: String) -> bool {
395415
true

tests/ui/collapsible_match_fixable.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ fn issue16558() {
2828
_ => 1,
2929
};
3030
}
31+
32+
// https://github.com/rust-lang/rust-clippy/issues/16875
33+
// lint still fires when only wildcard-like arms follow (fall-through is harmless)
34+
fn issue16875(a: Option<&str>, b: i32) -> i32 {
35+
let mut res = 0;
36+
match a {
37+
Some(_)
38+
if b == 0 => {
39+
//~^ collapsible_match
40+
res = 1;
41+
},
42+
_ => {},
43+
}
44+
res
45+
}

tests/ui/collapsible_match_fixable.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,19 @@ fn issue16558() {
2929
_ => 1,
3030
};
3131
}
32+
33+
// https://github.com/rust-lang/rust-clippy/issues/16875
34+
// lint still fires when only wildcard-like arms follow (fall-through is harmless)
35+
fn issue16875(a: Option<&str>, b: i32) -> i32 {
36+
let mut res = 0;
37+
match a {
38+
Some(_) => {
39+
if b == 0 {
40+
//~^ collapsible_match
41+
res = 1;
42+
}
43+
},
44+
_ => {},
45+
}
46+
res
47+
}

tests/ui/collapsible_match_fixable.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,23 @@ LL |
4646
LL ~ ,
4747
|
4848

49-
error: aborting due to 3 previous errors
49+
error: this `if` can be collapsed into the outer `match`
50+
--> tests/ui/collapsible_match_fixable.rs:39:13
51+
|
52+
LL | / if b == 0 {
53+
LL | |
54+
LL | | res = 1;
55+
LL | | }
56+
| |_____________^
57+
|
58+
help: collapse nested if block
59+
|
60+
LL ~ Some(_)
61+
LL ~ if b == 0 => {
62+
LL |
63+
LL | res = 1;
64+
LL ~ },
65+
|
66+
67+
error: aborting due to 4 previous errors
5068

0 commit comments

Comments
 (0)