Skip to content

Commit 787f5a2

Browse files
committed
Add run-rustfix to infallible_destructuring_match
1 parent 87407c5 commit 787f5a2

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// run-rustfix
2+
#![feature(exhaustive_patterns, never_type)]
3+
#![allow(dead_code, unreachable_code, unused_variables)]
4+
#![allow(clippy::let_and_return)]
5+
6+
enum SingleVariantEnum {
7+
Variant(i32),
8+
}
9+
10+
struct TupleStruct(i32);
11+
12+
enum EmptyEnum {}
13+
14+
fn infallible_destructuring_match_enum() {
15+
let wrapper = SingleVariantEnum::Variant(0);
16+
17+
// This should lint!
18+
let SingleVariantEnum::Variant(data) = wrapper;
19+
20+
// This shouldn't!
21+
let data = match wrapper {
22+
SingleVariantEnum::Variant(_) => -1,
23+
};
24+
25+
// Neither should this!
26+
let data = match wrapper {
27+
SingleVariantEnum::Variant(i) => -1,
28+
};
29+
30+
let SingleVariantEnum::Variant(data) = wrapper;
31+
}
32+
33+
fn infallible_destructuring_match_struct() {
34+
let wrapper = TupleStruct(0);
35+
36+
// This should lint!
37+
let TupleStruct(data) = wrapper;
38+
39+
// This shouldn't!
40+
let data = match wrapper {
41+
TupleStruct(_) => -1,
42+
};
43+
44+
// Neither should this!
45+
let data = match wrapper {
46+
TupleStruct(i) => -1,
47+
};
48+
49+
let TupleStruct(data) = wrapper;
50+
}
51+
52+
fn never_enum() {
53+
let wrapper: Result<i32, !> = Ok(23);
54+
55+
// This should lint!
56+
let Ok(data) = wrapper;
57+
58+
// This shouldn't!
59+
let data = match wrapper {
60+
Ok(_) => -1,
61+
};
62+
63+
// Neither should this!
64+
let data = match wrapper {
65+
Ok(i) => -1,
66+
};
67+
68+
let Ok(data) = wrapper;
69+
}
70+
71+
impl EmptyEnum {
72+
fn match_on(&self) -> ! {
73+
// The lint shouldn't pick this up, as `let` won't work here!
74+
let data = match *self {};
75+
data
76+
}
77+
}
78+
79+
fn main() {}

tests/ui/infallible_destructuring_match.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
// run-rustfix
12
#![feature(exhaustive_patterns, never_type)]
3+
#![allow(dead_code, unreachable_code, unused_variables)]
24
#![allow(clippy::let_and_return)]
35

46
enum SingleVariantEnum {

tests/ui/infallible_destructuring_match.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: you seem to be trying to use match to destructure a single infallible pattern. Consider using `let`
2-
--> $DIR/infallible_destructuring_match.rs:16:5
2+
--> $DIR/infallible_destructuring_match.rs:18:5
33
|
44
LL | / let data = match wrapper {
55
LL | | SingleVariantEnum::Variant(i) => i,
@@ -9,15 +9,15 @@ LL | | };
99
= note: `-D clippy::infallible-destructuring-match` implied by `-D warnings`
1010

1111
error: you seem to be trying to use match to destructure a single infallible pattern. Consider using `let`
12-
--> $DIR/infallible_destructuring_match.rs:37:5
12+
--> $DIR/infallible_destructuring_match.rs:39:5
1313
|
1414
LL | / let data = match wrapper {
1515
LL | | TupleStruct(i) => i,
1616
LL | | };
1717
| |______^ help: try this: `let TupleStruct(data) = wrapper;`
1818

1919
error: you seem to be trying to use match to destructure a single infallible pattern. Consider using `let`
20-
--> $DIR/infallible_destructuring_match.rs:58:5
20+
--> $DIR/infallible_destructuring_match.rs:60:5
2121
|
2222
LL | / let data = match wrapper {
2323
LL | | Ok(i) => i,

0 commit comments

Comments
 (0)