Skip to content

Commit 2216318

Browse files
committed
Use the default code path to list missing patterns
This uses the exact same code path that would be used for `match x { _ if false => {} }`, since in both cases the resulting matrix is empty. Since we think the behaviour in that case is ok, then we can remove the special case and use the default code path.
1 parent e444346 commit 2216318

File tree

6 files changed

+81
-82
lines changed

6 files changed

+81
-82
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -192,41 +192,28 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
192192
_ => (None, vec![]),
193193
};
194194

195-
let mut err = create_e0004(
196-
self.tcx.sess,
197-
scrut.span,
198-
format!(
199-
"non-exhaustive patterns: {}",
200-
match missing_variants.len() {
201-
0 => format!("type `{}` is non-empty", pat_ty),
202-
1 => format!(
203-
"pattern `{}` of type `{}` is not handled",
204-
missing_variants[0].name, pat_ty,
205-
),
206-
_ => format!(
207-
"multiple patterns of type `{}` are not handled",
208-
pat_ty
209-
),
210-
}
211-
),
212-
);
213-
err.help(
214-
"ensure that all possible cases are being handled, \
215-
possibly by adding wildcards or more match arms",
216-
);
217-
if let Some(sp) = def_span {
218-
err.span_label(sp, format!("`{}` defined here", pat_ty));
219-
}
220-
// point at the definition of non-covered enum variants
221-
if missing_variants.len() < 4 {
222-
for variant in &missing_variants {
223-
err.span_label(variant.span, "variant not covered");
195+
if missing_variants.is_empty() {
196+
let mut err = create_e0004(
197+
self.tcx.sess,
198+
scrut.span,
199+
format!("non-exhaustive patterns: type `{}` is non-empty", pat_ty),
200+
);
201+
err.help(
202+
"ensure that all possible cases are being handled, \
203+
possibly by adding wildcards or more match arms",
204+
);
205+
if let Some(sp) = def_span {
206+
err.span_label(sp, format!("`{}` defined here", pat_ty));
224207
}
208+
err.emit();
209+
return;
210+
} else {
211+
// Continue to the normal code path
225212
}
226-
err.emit();
213+
} else {
214+
// If the type *is* uninhabited, it's vacuously exhaustive
215+
return;
227216
}
228-
// If the type *is* uninhabited, it's vacuously exhaustive
229-
return;
230217
}
231218

232219
let scrut_ty = self.tables.node_type(scrut.hir_id);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ union NonEmptyUnion2 { //~ `NonEmptyUnion2` defined here
1313
}
1414
enum NonEmptyEnum1 { //~ `NonEmptyEnum1` defined here
1515
Foo(bool),
16-
//~^ variant not covered
16+
//~^ not covered
1717
//~| not covered
1818
}
1919
enum NonEmptyEnum2 { //~ `NonEmptyEnum2` defined here
2020
Foo(bool),
21-
//~^ variant not covered
21+
//~^ not covered
2222
//~| not covered
2323
Bar,
24-
//~^ variant not covered
24+
//~^ not covered
2525
//~| not covered
2626
}
2727
enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here
@@ -70,11 +70,11 @@ fn main() {
7070
match_empty!((NonEmptyUnion2 { foo: () }));
7171
//~^ ERROR type `NonEmptyUnion2` is non-empty
7272
match_empty!(NonEmptyEnum1::Foo(true));
73-
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
73+
//~^ ERROR `Foo(_)` not covered
7474
match_empty!(NonEmptyEnum2::Foo(true));
75-
//~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled
75+
//~^ ERROR `Foo(_)` and `Bar` not covered
7676
match_empty!(NonEmptyEnum5::V1);
77-
//~^ ERROR multiple patterns of type `NonEmptyEnum5` are not handled
77+
//~^ ERROR `V1`, `V2`, `V3` and 2 more not covered
7878

7979
match_false!(0u8);
8080
//~^ ERROR `0u8..=std::u8::MAX` not covered

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

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unreachable pattern
2-
--> $DIR/match-empty-exhaustive_patterns.rs:41:9
2+
--> $DIR/match-empty-exhaustive_patterns.rs:47:9
33
|
44
LL | _ => {},
55
| ^
@@ -11,108 +11,114 @@ LL | #![deny(unreachable_patterns)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

1313
error: unreachable pattern
14-
--> $DIR/match-empty-exhaustive_patterns.rs:44:9
14+
--> $DIR/match-empty-exhaustive_patterns.rs:50:9
1515
|
1616
LL | _ if false => {},
1717
| ^
1818

1919
error: unreachable pattern
20-
--> $DIR/match-empty-exhaustive_patterns.rs:51:9
20+
--> $DIR/match-empty-exhaustive_patterns.rs:57:9
2121
|
2222
LL | Some(_) => {}
2323
| ^^^^^^^
2424

2525
error: unreachable pattern
26-
--> $DIR/match-empty-exhaustive_patterns.rs:55:9
26+
--> $DIR/match-empty-exhaustive_patterns.rs:61:9
2727
|
2828
LL | Some(_) => {}
2929
| ^^^^^^^
3030

3131
error[E0004]: non-exhaustive patterns: type `u8` is non-empty
32-
--> $DIR/match-empty-exhaustive_patterns.rs:58:18
32+
--> $DIR/match-empty-exhaustive_patterns.rs:64:18
3333
|
3434
LL | match_empty!(0u8);
3535
| ^^^
3636
|
3737
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3838

3939
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
40-
--> $DIR/match-empty-exhaustive_patterns.rs:60:18
40+
--> $DIR/match-empty-exhaustive_patterns.rs:66:18
4141
|
4242
LL | match_empty!(NonEmptyStruct(true));
4343
| ^^^^^^^^^^^^^^^^^^^^
4444
|
4545
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4646

4747
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
48-
--> $DIR/match-empty-exhaustive_patterns.rs:62:18
48+
--> $DIR/match-empty-exhaustive_patterns.rs:68:18
4949
|
5050
LL | match_empty!((NonEmptyUnion1 { foo: () }));
5151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5252
|
5353
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5454

5555
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
56-
--> $DIR/match-empty-exhaustive_patterns.rs:64:18
56+
--> $DIR/match-empty-exhaustive_patterns.rs:70:18
5757
|
5858
LL | match_empty!((NonEmptyUnion2 { foo: () }));
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6060
|
6161
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
6262

63-
error[E0004]: non-exhaustive patterns: pattern `Foo` of type `NonEmptyEnum1` is not handled
64-
--> $DIR/match-empty-exhaustive_patterns.rs:66:18
63+
error[E0004]: non-exhaustive patterns: `Foo(_)` not covered
64+
--> $DIR/match-empty-exhaustive_patterns.rs:72:18
6565
|
6666
LL | / enum NonEmptyEnum1 {
6767
LL | | Foo(bool),
68-
| | --- variant not covered
68+
| | --- not covered
69+
LL | |
70+
LL | |
6971
LL | | }
7072
| |_- `NonEmptyEnum1` defined here
7173
...
7274
LL | match_empty!(NonEmptyEnum1::Foo(true));
73-
| ^^^^^^^^^^^^^^^^^^^^^^^^
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `Foo(_)` not covered
7476
|
7577
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
7678

77-
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum2` are not handled
78-
--> $DIR/match-empty-exhaustive_patterns.rs:68:18
79+
error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered
80+
--> $DIR/match-empty-exhaustive_patterns.rs:74:18
7981
|
8082
LL | / enum NonEmptyEnum2 {
8183
LL | | Foo(bool),
82-
| | --- variant not covered
84+
| | --- not covered
85+
LL | |
86+
LL | |
8387
LL | | Bar,
84-
| | --- variant not covered
88+
| | --- not covered
89+
LL | |
90+
LL | |
8591
LL | | }
8692
| |_- `NonEmptyEnum2` defined here
8793
...
8894
LL | match_empty!(NonEmptyEnum2::Foo(true));
89-
| ^^^^^^^^^^^^^^^^^^^^^^^^
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^ patterns `Foo(_)` and `Bar` not covered
9096
|
9197
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
9298

93-
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum5` are not handled
94-
--> $DIR/match-empty-exhaustive_patterns.rs:70:18
99+
error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered
100+
--> $DIR/match-empty-exhaustive_patterns.rs:76:18
95101
|
96102
LL | / enum NonEmptyEnum5 {
97103
LL | | V1, V2, V3, V4, V5,
98104
LL | | }
99105
| |_- `NonEmptyEnum5` defined here
100106
...
101107
LL | match_empty!(NonEmptyEnum5::V1);
102-
| ^^^^^^^^^^^^^^^^^
108+
| ^^^^^^^^^^^^^^^^^ patterns `V1`, `V2`, `V3` and 2 more not covered
103109
|
104110
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
105111

106112
error[E0004]: non-exhaustive patterns: `0u8..=std::u8::MAX` not covered
107-
--> $DIR/match-empty-exhaustive_patterns.rs:73:18
113+
--> $DIR/match-empty-exhaustive_patterns.rs:79:18
108114
|
109115
LL | match_false!(0u8);
110116
| ^^^ pattern `0u8..=std::u8::MAX` not covered
111117
|
112118
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
113119

114120
error[E0004]: non-exhaustive patterns: `NonEmptyStruct(_)` not covered
115-
--> $DIR/match-empty-exhaustive_patterns.rs:75:18
121+
--> $DIR/match-empty-exhaustive_patterns.rs:81:18
116122
|
117123
LL | struct NonEmptyStruct(bool);
118124
| ---------------------------- `NonEmptyStruct` defined here
@@ -123,7 +129,7 @@ LL | match_false!(NonEmptyStruct(true));
123129
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
124130

125131
error[E0004]: non-exhaustive patterns: `NonEmptyUnion1 { .. }` not covered
126-
--> $DIR/match-empty-exhaustive_patterns.rs:77:18
132+
--> $DIR/match-empty-exhaustive_patterns.rs:83:18
127133
|
128134
LL | / union NonEmptyUnion1 {
129135
LL | | foo: (),
@@ -136,7 +142,7 @@ LL | match_false!((NonEmptyUnion1 { foo: () }));
136142
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
137143

138144
error[E0004]: non-exhaustive patterns: `NonEmptyUnion2 { .. }` not covered
139-
--> $DIR/match-empty-exhaustive_patterns.rs:79:18
145+
--> $DIR/match-empty-exhaustive_patterns.rs:85:18
140146
|
141147
LL | / union NonEmptyUnion2 {
142148
LL | | foo: (),
@@ -150,11 +156,13 @@ LL | match_false!((NonEmptyUnion2 { foo: () }));
150156
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
151157

152158
error[E0004]: non-exhaustive patterns: `Foo(_)` not covered
153-
--> $DIR/match-empty-exhaustive_patterns.rs:81:18
159+
--> $DIR/match-empty-exhaustive_patterns.rs:87:18
154160
|
155161
LL | / enum NonEmptyEnum1 {
156162
LL | | Foo(bool),
157163
| | --- not covered
164+
LL | |
165+
LL | |
158166
LL | | }
159167
| |_- `NonEmptyEnum1` defined here
160168
...
@@ -164,13 +172,17 @@ LL | match_false!(NonEmptyEnum1::Foo(true));
164172
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
165173

166174
error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered
167-
--> $DIR/match-empty-exhaustive_patterns.rs:83:18
175+
--> $DIR/match-empty-exhaustive_patterns.rs:89:18
168176
|
169177
LL | / enum NonEmptyEnum2 {
170178
LL | | Foo(bool),
171179
| | --- not covered
180+
LL | |
181+
LL | |
172182
LL | | Bar,
173183
| | --- not covered
184+
LL | |
185+
LL | |
174186
LL | | }
175187
| |_- `NonEmptyEnum2` defined here
176188
...
@@ -180,7 +192,7 @@ LL | match_false!(NonEmptyEnum2::Foo(true));
180192
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
181193

182194
error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered
183-
--> $DIR/match-empty-exhaustive_patterns.rs:85:18
195+
--> $DIR/match-empty-exhaustive_patterns.rs:91:18
184196
|
185197
LL | / enum NonEmptyEnum5 {
186198
LL | | V1, V2, V3, V4, V5,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ union NonEmptyUnion2 { //~ `NonEmptyUnion2` defined here
1212
}
1313
enum NonEmptyEnum1 { //~ `NonEmptyEnum1` defined here
1414
Foo(bool),
15-
//~^ variant not covered
15+
//~^ not covered
1616
//~| not covered
1717
}
1818
enum NonEmptyEnum2 { //~ `NonEmptyEnum2` defined here
1919
Foo(bool),
20-
//~^ variant not covered
20+
//~^ not covered
2121
//~| not covered
2222
Bar,
23-
//~^ variant not covered
23+
//~^ not covered
2424
//~| not covered
2525
}
2626
enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here
@@ -69,11 +69,11 @@ fn main() {
6969
match_empty!((NonEmptyUnion2 { foo: () }));
7070
//~^ ERROR type `NonEmptyUnion2` is non-empty
7171
match_empty!(NonEmptyEnum1::Foo(true));
72-
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
72+
//~^ ERROR `Foo(_)` not covered
7373
match_empty!(NonEmptyEnum2::Foo(true));
74-
//~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled
74+
//~^ ERROR `Foo(_)` and `Bar` not covered
7575
match_empty!(NonEmptyEnum5::V1);
76-
//~^ ERROR multiple patterns of type `NonEmptyEnum5` are not handled
76+
//~^ ERROR `V1`, `V2`, `V3` and 2 more not covered
7777

7878
match_false!(0u8);
7979
//~^ ERROR `0u8..=std::u8::MAX` not covered

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,43 +41,43 @@ LL | match_empty!((NonEmptyUnion2 { foo: () }));
4141
|
4242
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4343

44-
error[E0004]: non-exhaustive patterns: pattern `Foo` of type `NonEmptyEnum1` is not handled
44+
error[E0004]: non-exhaustive patterns: `Foo(_)` not covered
4545
--> $DIR/match-empty.rs:71:18
4646
|
4747
LL | / enum NonEmptyEnum1 {
4848
LL | | Foo(bool),
49-
| | --- variant not covered
49+
| | --- not covered
5050
LL | |
5151
LL | |
5252
LL | | }
5353
| |_- `NonEmptyEnum1` defined here
5454
...
5555
LL | match_empty!(NonEmptyEnum1::Foo(true));
56-
| ^^^^^^^^^^^^^^^^^^^^^^^^
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `Foo(_)` not covered
5757
|
5858
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5959

60-
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum2` are not handled
60+
error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered
6161
--> $DIR/match-empty.rs:73:18
6262
|
6363
LL | / enum NonEmptyEnum2 {
6464
LL | | Foo(bool),
65-
| | --- variant not covered
65+
| | --- not covered
6666
LL | |
6767
LL | |
6868
LL | | Bar,
69-
| | --- variant not covered
69+
| | --- not covered
7070
LL | |
7171
LL | |
7272
LL | | }
7373
| |_- `NonEmptyEnum2` defined here
7474
...
7575
LL | match_empty!(NonEmptyEnum2::Foo(true));
76-
| ^^^^^^^^^^^^^^^^^^^^^^^^
76+
| ^^^^^^^^^^^^^^^^^^^^^^^^ patterns `Foo(_)` and `Bar` not covered
7777
|
7878
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
7979

80-
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum5` are not handled
80+
error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered
8181
--> $DIR/match-empty.rs:75:18
8282
|
8383
LL | / enum NonEmptyEnum5 {
@@ -86,7 +86,7 @@ LL | | }
8686
| |_- `NonEmptyEnum5` defined here
8787
...
8888
LL | match_empty!(NonEmptyEnum5::V1);
89-
| ^^^^^^^^^^^^^^^^^
89+
| ^^^^^^^^^^^^^^^^^ patterns `V1`, `V2`, `V3` and 2 more not covered
9090
|
9191
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
9292

0 commit comments

Comments
 (0)