Skip to content

Commit c0f3c06

Browse files
committed
Only warn about missing patterns in the case of an enum
1 parent 2099dd1 commit c0f3c06

File tree

9 files changed

+34
-88
lines changed

9 files changed

+34
-88
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
176176
} else {
177177
match pat_ty.kind {
178178
ty::Never => true,
179-
ty::Adt(def, _) => {
179+
ty::Adt(def, _) if def.is_enum() => {
180180
def.variants.is_empty() && !cx.is_foreign_non_exhaustive_enum(pat_ty)
181181
}
182182
_ => false,
@@ -185,7 +185,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
185185
if !scrutinee_is_visibly_uninhabited {
186186
// We know the type is inhabited, so this must be wrong
187187
let (def_span, missing_variants) = match pat_ty.kind {
188-
ty::Adt(def, _) => (
188+
ty::Adt(def, _) if def.is_enum() => (
189189
self.tcx.hir().span_if_local(def.did),
190190
def.variants.iter().map(|variant| variant.ident).collect(),
191191
),

src/test/ui/pattern/usefulness/always-inhabited-union-ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn match_on_uninhab() {
2525
}
2626

2727
match uninhab_union() {
28-
//~^ ERROR non-exhaustive patterns: pattern `Foo` of type `Foo` is not handled
28+
//~^ ERROR non-exhaustive patterns: type `Foo` is non-empty
2929
}
3030
}
3131

src/test/ui/pattern/usefulness/always-inhabited-union-ref.stderr

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@ LL | match uninhab_ref() {
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error[E0004]: non-exhaustive patterns: pattern `Foo` of type `Foo` is not handled
9+
error[E0004]: non-exhaustive patterns: type `Foo` is non-empty
1010
--> $DIR/always-inhabited-union-ref.rs:27:11
1111
|
12-
LL | pub union Foo {
13-
| - --- variant not covered
14-
| _|
15-
| |
16-
LL | | foo: !,
17-
LL | | }
18-
| |_- `Foo` defined here
19-
...
20-
LL | match uninhab_union() {
21-
| ^^^^^^^^^^^^^^^
12+
LL | match uninhab_union() {
13+
| ^^^^^^^^^^^^^^^
2214
|
2315
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2416

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![deny(unreachable_patterns)]
44
enum Foo {}
55

6-
struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here
6+
struct NonEmptyStruct(bool);
77
union NonEmptyUnion1 {
88
foo: (),
99
}
@@ -42,11 +42,11 @@ fn main() {
4242
match 0u8 {}
4343
//~^ ERROR type `u8` is non-empty
4444
match NonEmptyStruct(true) {}
45-
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
45+
//~^ ERROR type `NonEmptyStruct` is non-empty
4646
match (NonEmptyUnion1 { foo: () }) {}
47-
//~^ ERROR pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
47+
//~^ ERROR type `NonEmptyUnion1` is non-empty
4848
match (NonEmptyUnion2 { foo: () }) {}
49-
//~^ ERROR pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
49+
//~^ ERROR type `NonEmptyUnion2` is non-empty
5050
match NonEmptyEnum1::Foo(true) {}
5151
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
5252
match NonEmptyEnum2::Foo(true) {}

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

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,27 @@ LL | match 0u8 {}
3030
|
3131
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3232

33-
error[E0004]: non-exhaustive patterns: pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
33+
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
3434
--> $DIR/match-empty-exhaustive_patterns.rs:44:11
3535
|
36-
LL | struct NonEmptyStruct(bool);
37-
| ----------------------------
38-
| | |
39-
| | variant not covered
40-
| `NonEmptyStruct` defined here
41-
...
4236
LL | match NonEmptyStruct(true) {}
4337
| ^^^^^^^^^^^^^^^^^^^^
4438
|
4539
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4640

47-
error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
41+
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
4842
--> $DIR/match-empty-exhaustive_patterns.rs:46:11
4943
|
50-
LL | union NonEmptyUnion1 {
51-
| - -------------- variant not covered
52-
| _|
53-
| |
54-
LL | | foo: (),
55-
LL | | }
56-
| |_- `NonEmptyUnion1` defined here
57-
...
58-
LL | match (NonEmptyUnion1 { foo: () }) {}
59-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
LL | match (NonEmptyUnion1 { foo: () }) {}
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6046
|
6147
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
6248

63-
error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
49+
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
6450
--> $DIR/match-empty-exhaustive_patterns.rs:48:11
6551
|
66-
LL | union NonEmptyUnion2 {
67-
| - -------------- variant not covered
68-
| _|
69-
| |
70-
LL | | foo: (),
71-
LL | | bar: (),
72-
LL | | }
73-
| |_- `NonEmptyUnion2` defined here
74-
...
75-
LL | match (NonEmptyUnion2 { foo: () }) {}
76-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
LL | match (NonEmptyUnion2 { foo: () }) {}
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7754
|
7855
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
7956

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![deny(unreachable_patterns)]
33
enum Foo {}
44

5-
struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here
5+
struct NonEmptyStruct(bool);
66
union NonEmptyUnion1 {
77
foo: (),
88
}
@@ -45,11 +45,11 @@ fn main() {
4545
match 0u8 {}
4646
//~^ ERROR type `u8` is non-empty
4747
match NonEmptyStruct(true) {}
48-
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
48+
//~^ ERROR type `NonEmptyStruct` is non-empty
4949
match (NonEmptyUnion1 { foo: () }) {}
50-
//~^ ERROR pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
50+
//~^ ERROR type `NonEmptyUnion1` is non-empty
5151
match (NonEmptyUnion2 { foo: () }) {}
52-
//~^ ERROR pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
52+
//~^ ERROR type `NonEmptyUnion2` is non-empty
5353
match NonEmptyEnum1::Foo(true) {}
5454
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
5555
match NonEmptyEnum2::Foo(true) {}

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

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,27 @@ LL | match 0u8 {}
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error[E0004]: non-exhaustive patterns: pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
9+
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
1010
--> $DIR/match-empty.rs:47:11
1111
|
12-
LL | struct NonEmptyStruct(bool);
13-
| ----------------------------
14-
| | |
15-
| | variant not covered
16-
| `NonEmptyStruct` defined here
17-
...
1812
LL | match NonEmptyStruct(true) {}
1913
| ^^^^^^^^^^^^^^^^^^^^
2014
|
2115
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2216

23-
error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
17+
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
2418
--> $DIR/match-empty.rs:49:11
2519
|
26-
LL | union NonEmptyUnion1 {
27-
| - -------------- variant not covered
28-
| _|
29-
| |
30-
LL | | foo: (),
31-
LL | | }
32-
| |_- `NonEmptyUnion1` defined here
33-
...
34-
LL | match (NonEmptyUnion1 { foo: () }) {}
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
LL | match (NonEmptyUnion1 { foo: () }) {}
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3622
|
3723
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3824

39-
error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
25+
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
4026
--> $DIR/match-empty.rs:51:11
4127
|
42-
LL | union NonEmptyUnion2 {
43-
| - -------------- variant not covered
44-
| _|
45-
| |
46-
LL | | foo: (),
47-
LL | | bar: (),
48-
LL | | }
49-
| |_- `NonEmptyUnion2` defined here
50-
...
51-
LL | match (NonEmptyUnion2 { foo: () }) {}
52-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
LL | match (NonEmptyUnion2 { foo: () }) {}
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5330
|
5431
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5532

src/test/ui/rfc-2008-non-exhaustive/uninhabited/indirect_match_with_exhaustive_patterns.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedEnum` of type `uninhabited::IndirectUninhabitedEnum` is not handled
1+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedEnum` is non-empty
22
--> $DIR/indirect_match_with_exhaustive_patterns.rs:22:11
33
|
44
LL | match x {}
55
| ^
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedStruct` of type `uninhabited::IndirectUninhabitedStruct` is not handled
9+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedStruct` is non-empty
1010
--> $DIR/indirect_match_with_exhaustive_patterns.rs:26:11
1111
|
1212
LL | match x {}
1313
| ^
1414
|
1515
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

17-
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedTupleStruct` of type `uninhabited::IndirectUninhabitedTupleStruct` is not handled
17+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedTupleStruct` is non-empty
1818
--> $DIR/indirect_match_with_exhaustive_patterns.rs:30:11
1919
|
2020
LL | match x {}
2121
| ^
2222
|
2323
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2424

25-
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedVariants` of type `uninhabited::IndirectUninhabitedVariants` is not handled
25+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedVariants` is non-empty
2626
--> $DIR/indirect_match_with_exhaustive_patterns.rs:36:11
2727
|
2828
LL | match x {}

src/test/ui/rfc-2008-non-exhaustive/uninhabited/match_with_exhaustive_patterns.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ LL | match x {}
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error[E0004]: non-exhaustive patterns: pattern `UninhabitedStruct` of type `uninhabited::UninhabitedStruct` is not handled
9+
error[E0004]: non-exhaustive patterns: type `uninhabited::UninhabitedStruct` is non-empty
1010
--> $DIR/match_with_exhaustive_patterns.rs:25:11
1111
|
1212
LL | match x {}
1313
| ^
1414
|
1515
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

17-
error[E0004]: non-exhaustive patterns: pattern `UninhabitedTupleStruct` of type `uninhabited::UninhabitedTupleStruct` is not handled
17+
error[E0004]: non-exhaustive patterns: type `uninhabited::UninhabitedTupleStruct` is non-empty
1818
--> $DIR/match_with_exhaustive_patterns.rs:29:11
1919
|
2020
LL | match x {}

0 commit comments

Comments
 (0)