Skip to content

Commit 5a3b7d2

Browse files
committed
Add tests
1 parent c0f3c06 commit 5a3b7d2

File tree

4 files changed

+312
-65
lines changed

4 files changed

+312
-65
lines changed

src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,27 @@ enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here
2222
V1, V2, V3, V4, V5,
2323
}
2424

25+
macro_rules! match_empty {
26+
($e:expr) => {
27+
match $e {}
28+
};
29+
}
30+
macro_rules! match_false {
31+
($e:expr) => {
32+
match $e {
33+
_ if false => {}
34+
}
35+
};
36+
}
37+
2538
fn foo(x: Foo) {
26-
match x {} // ok
39+
match_empty!(x); // ok
2740
match x {
2841
_ => {}, //~ ERROR unreachable pattern
2942
}
43+
match x {
44+
_ if false => {}, //~ ERROR unreachable pattern
45+
}
3046
}
3147

3248
fn main() {
@@ -39,18 +55,33 @@ fn main() {
3955
Some(_) => {} //~ ERROR unreachable pattern
4056
}
4157

42-
match 0u8 {}
58+
match_empty!(0u8);
4359
//~^ ERROR type `u8` is non-empty
44-
match NonEmptyStruct(true) {}
60+
match_empty!(NonEmptyStruct(true));
4561
//~^ ERROR type `NonEmptyStruct` is non-empty
46-
match (NonEmptyUnion1 { foo: () }) {}
62+
match_empty!((NonEmptyUnion1 { foo: () }));
4763
//~^ ERROR type `NonEmptyUnion1` is non-empty
48-
match (NonEmptyUnion2 { foo: () }) {}
64+
match_empty!((NonEmptyUnion2 { foo: () }));
4965
//~^ ERROR type `NonEmptyUnion2` is non-empty
50-
match NonEmptyEnum1::Foo(true) {}
66+
match_empty!(NonEmptyEnum1::Foo(true));
5167
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
52-
match NonEmptyEnum2::Foo(true) {}
68+
match_empty!(NonEmptyEnum2::Foo(true));
5369
//~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled
54-
match NonEmptyEnum5::V1 {}
70+
match_empty!(NonEmptyEnum5::V1);
5571
//~^ ERROR multiple patterns of type `NonEmptyEnum5` are not handled
72+
73+
match_false!(0u8);
74+
//~^ ERROR `_` not covered
75+
match_false!(NonEmptyStruct(true));
76+
//~^ ERROR `_` not covered
77+
match_false!((NonEmptyUnion1 { foo: () }));
78+
//~^ ERROR `_` not covered
79+
match_false!((NonEmptyUnion2 { foo: () }));
80+
//~^ ERROR `_` not covered
81+
match_false!(NonEmptyEnum1::Foo(true));
82+
//~^ ERROR `_` not covered
83+
match_false!(NonEmptyEnum2::Foo(true));
84+
//~^ ERROR `_` not covered
85+
match_false!(NonEmptyEnum5::V1);
86+
//~^ ERROR `_` not covered
5687
}
Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unreachable pattern
2-
--> $DIR/match-empty-exhaustive_patterns.rs:28:9
2+
--> $DIR/match-empty-exhaustive_patterns.rs:41:9
33
|
44
LL | _ => {},
55
| ^
@@ -11,65 +11,71 @@ LL | #![deny(unreachable_patterns)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

1313
error: unreachable pattern
14-
--> $DIR/match-empty-exhaustive_patterns.rs:35:9
14+
--> $DIR/match-empty-exhaustive_patterns.rs:44:9
15+
|
16+
LL | _ if false => {},
17+
| ^
18+
19+
error: unreachable pattern
20+
--> $DIR/match-empty-exhaustive_patterns.rs:51:9
1521
|
1622
LL | Some(_) => {}
1723
| ^^^^^^^
1824

1925
error: unreachable pattern
20-
--> $DIR/match-empty-exhaustive_patterns.rs:39:9
26+
--> $DIR/match-empty-exhaustive_patterns.rs:55:9
2127
|
2228
LL | Some(_) => {}
2329
| ^^^^^^^
2430

2531
error[E0004]: non-exhaustive patterns: type `u8` is non-empty
26-
--> $DIR/match-empty-exhaustive_patterns.rs:42:11
32+
--> $DIR/match-empty-exhaustive_patterns.rs:58:18
2733
|
28-
LL | match 0u8 {}
29-
| ^^^
34+
LL | match_empty!(0u8);
35+
| ^^^
3036
|
3137
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3238

3339
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
34-
--> $DIR/match-empty-exhaustive_patterns.rs:44:11
40+
--> $DIR/match-empty-exhaustive_patterns.rs:60:18
3541
|
36-
LL | match NonEmptyStruct(true) {}
37-
| ^^^^^^^^^^^^^^^^^^^^
42+
LL | match_empty!(NonEmptyStruct(true));
43+
| ^^^^^^^^^^^^^^^^^^^^
3844
|
3945
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4046

4147
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
42-
--> $DIR/match-empty-exhaustive_patterns.rs:46:11
48+
--> $DIR/match-empty-exhaustive_patterns.rs:62:18
4349
|
44-
LL | match (NonEmptyUnion1 { foo: () }) {}
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
LL | match_empty!((NonEmptyUnion1 { foo: () }));
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4652
|
4753
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4854

4955
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
50-
--> $DIR/match-empty-exhaustive_patterns.rs:48:11
56+
--> $DIR/match-empty-exhaustive_patterns.rs:64:18
5157
|
52-
LL | match (NonEmptyUnion2 { foo: () }) {}
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58+
LL | match_empty!((NonEmptyUnion2 { foo: () }));
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5460
|
5561
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5662

5763
error[E0004]: non-exhaustive patterns: pattern `Foo` of type `NonEmptyEnum1` is not handled
58-
--> $DIR/match-empty-exhaustive_patterns.rs:50:11
64+
--> $DIR/match-empty-exhaustive_patterns.rs:66:18
5965
|
6066
LL | / enum NonEmptyEnum1 {
6167
LL | | Foo(bool),
6268
| | --- variant not covered
6369
LL | | }
6470
| |_- `NonEmptyEnum1` defined here
6571
...
66-
LL | match NonEmptyEnum1::Foo(true) {}
67-
| ^^^^^^^^^^^^^^^^^^^^^^^^
72+
LL | match_empty!(NonEmptyEnum1::Foo(true));
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6874
|
6975
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
7076

7177
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum2` are not handled
72-
--> $DIR/match-empty-exhaustive_patterns.rs:52:11
78+
--> $DIR/match-empty-exhaustive_patterns.rs:68:18
7379
|
7480
LL | / enum NonEmptyEnum2 {
7581
LL | | Foo(bool),
@@ -79,24 +85,110 @@ LL | | Bar,
7985
LL | | }
8086
| |_- `NonEmptyEnum2` defined here
8187
...
82-
LL | match NonEmptyEnum2::Foo(true) {}
83-
| ^^^^^^^^^^^^^^^^^^^^^^^^
88+
LL | match_empty!(NonEmptyEnum2::Foo(true));
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^
8490
|
8591
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
8692

8793
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum5` are not handled
88-
--> $DIR/match-empty-exhaustive_patterns.rs:54:11
94+
--> $DIR/match-empty-exhaustive_patterns.rs:70:18
95+
|
96+
LL | / enum NonEmptyEnum5 {
97+
LL | | V1, V2, V3, V4, V5,
98+
LL | | }
99+
| |_- `NonEmptyEnum5` defined here
100+
...
101+
LL | match_empty!(NonEmptyEnum5::V1);
102+
| ^^^^^^^^^^^^^^^^^
103+
|
104+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
105+
106+
error[E0004]: non-exhaustive patterns: `_` not covered
107+
--> $DIR/match-empty-exhaustive_patterns.rs:73:18
108+
|
109+
LL | match_false!(0u8);
110+
| ^^^ pattern `_` not covered
111+
|
112+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
113+
114+
error[E0004]: non-exhaustive patterns: `_` not covered
115+
--> $DIR/match-empty-exhaustive_patterns.rs:75:18
116+
|
117+
LL | struct NonEmptyStruct(bool);
118+
| ---------------------------- `NonEmptyStruct` defined here
119+
...
120+
LL | match_false!(NonEmptyStruct(true));
121+
| ^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered
122+
|
123+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
124+
125+
error[E0004]: non-exhaustive patterns: `_` not covered
126+
--> $DIR/match-empty-exhaustive_patterns.rs:77:18
127+
|
128+
LL | / union NonEmptyUnion1 {
129+
LL | | foo: (),
130+
LL | | }
131+
| |_- `NonEmptyUnion1` defined here
132+
...
133+
LL | match_false!((NonEmptyUnion1 { foo: () }));
134+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered
135+
|
136+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
137+
138+
error[E0004]: non-exhaustive patterns: `_` not covered
139+
--> $DIR/match-empty-exhaustive_patterns.rs:79:18
140+
|
141+
LL | / union NonEmptyUnion2 {
142+
LL | | foo: (),
143+
LL | | bar: (),
144+
LL | | }
145+
| |_- `NonEmptyUnion2` defined here
146+
...
147+
LL | match_false!((NonEmptyUnion2 { foo: () }));
148+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered
149+
|
150+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
151+
152+
error[E0004]: non-exhaustive patterns: `_` not covered
153+
--> $DIR/match-empty-exhaustive_patterns.rs:81:18
154+
|
155+
LL | / enum NonEmptyEnum1 {
156+
LL | | Foo(bool),
157+
LL | | }
158+
| |_- `NonEmptyEnum1` defined here
159+
...
160+
LL | match_false!(NonEmptyEnum1::Foo(true));
161+
| ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered
162+
|
163+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
164+
165+
error[E0004]: non-exhaustive patterns: `_` not covered
166+
--> $DIR/match-empty-exhaustive_patterns.rs:83:18
167+
|
168+
LL | / enum NonEmptyEnum2 {
169+
LL | | Foo(bool),
170+
LL | | Bar,
171+
LL | | }
172+
| |_- `NonEmptyEnum2` defined here
173+
...
174+
LL | match_false!(NonEmptyEnum2::Foo(true));
175+
| ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered
176+
|
177+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
178+
179+
error[E0004]: non-exhaustive patterns: `_` not covered
180+
--> $DIR/match-empty-exhaustive_patterns.rs:85:18
89181
|
90182
LL | / enum NonEmptyEnum5 {
91183
LL | | V1, V2, V3, V4, V5,
92184
LL | | }
93185
| |_- `NonEmptyEnum5` defined here
94186
...
95-
LL | match NonEmptyEnum5::V1 {}
96-
| ^^^^^^^^^^^^^^^^^
187+
LL | match_false!(NonEmptyEnum5::V1);
188+
| ^^^^^^^^^^^^^^^^^ pattern `_` not covered
97189
|
98190
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
99191

100-
error: aborting due to 10 previous errors
192+
error: aborting due to 18 previous errors
101193

102194
For more information about this error, try `rustc --explain E0004`.

src/test/ui/pattern/usefulness/match-empty.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,23 @@ enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here
2121
V1, V2, V3, V4, V5,
2222
}
2323

24-
fn foo1(x: Foo) {
25-
match x {} // ok
24+
macro_rules! match_empty {
25+
($e:expr) => {
26+
match $e {}
27+
};
28+
}
29+
macro_rules! match_false {
30+
($e:expr) => {
31+
match $e {
32+
_ if false => {}
33+
}
34+
};
2635
}
2736

28-
fn foo2(x: Foo) {
37+
fn foo(x: Foo) {
38+
match_empty!(x); // ok
39+
match_false!(x); // Not detected as unreachable nor exhaustive.
40+
//~^ ERROR non-exhaustive patterns: `_` not covered
2941
match x {
3042
_ => {}, // Not detected as unreachable, see #55123.
3143
}
@@ -42,18 +54,33 @@ fn main() {
4254
Some(_) => {}
4355
}
4456

45-
match 0u8 {}
57+
match_empty!(0u8);
4658
//~^ ERROR type `u8` is non-empty
47-
match NonEmptyStruct(true) {}
59+
match_empty!(NonEmptyStruct(true));
4860
//~^ ERROR type `NonEmptyStruct` is non-empty
49-
match (NonEmptyUnion1 { foo: () }) {}
61+
match_empty!((NonEmptyUnion1 { foo: () }));
5062
//~^ ERROR type `NonEmptyUnion1` is non-empty
51-
match (NonEmptyUnion2 { foo: () }) {}
63+
match_empty!((NonEmptyUnion2 { foo: () }));
5264
//~^ ERROR type `NonEmptyUnion2` is non-empty
53-
match NonEmptyEnum1::Foo(true) {}
65+
match_empty!(NonEmptyEnum1::Foo(true));
5466
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
55-
match NonEmptyEnum2::Foo(true) {}
67+
match_empty!(NonEmptyEnum2::Foo(true));
5668
//~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled
57-
match NonEmptyEnum5::V1 {}
69+
match_empty!(NonEmptyEnum5::V1);
5870
//~^ ERROR multiple patterns of type `NonEmptyEnum5` are not handled
71+
72+
match_false!(0u8);
73+
//~^ ERROR `_` not covered
74+
match_false!(NonEmptyStruct(true));
75+
//~^ ERROR `_` not covered
76+
match_false!((NonEmptyUnion1 { foo: () }));
77+
//~^ ERROR `_` not covered
78+
match_false!((NonEmptyUnion2 { foo: () }));
79+
//~^ ERROR `_` not covered
80+
match_false!(NonEmptyEnum1::Foo(true));
81+
//~^ ERROR `_` not covered
82+
match_false!(NonEmptyEnum2::Foo(true));
83+
//~^ ERROR `_` not covered
84+
match_false!(NonEmptyEnum5::V1);
85+
//~^ ERROR `_` not covered
5986
}

0 commit comments

Comments
 (0)