Skip to content

Commit 5628d4a

Browse files
committed
Make empty match lint more consistent under exhaustive_patterns
1 parent 1c77a04 commit 5628d4a

File tree

7 files changed

+74
-39
lines changed

7 files changed

+74
-39
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,27 +169,29 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
169169
// Then, if the match has no arms, check whether the scrutinee
170170
// is uninhabited.
171171
let pat_ty = self.tables.node_type(scrut.hir_id);
172-
let module = self.tcx.hir().get_module_parent(scrut.hir_id);
173-
let mut def_span = None;
174-
let mut missing_variants = vec![];
175172
if inlined_arms.is_empty() {
176173
let scrutinee_is_visibly_uninhabited = if self.tcx.features().exhaustive_patterns {
174+
let module = self.tcx.hir().get_module_parent(scrut.hir_id);
177175
self.tcx.is_ty_uninhabited_from(module, pat_ty)
178176
} else {
179177
match pat_ty.kind {
180178
ty::Never => true,
181179
ty::Adt(def, _) => {
182-
def_span = self.tcx.hir().span_if_local(def.did);
183-
missing_variants =
184-
def.variants.iter().map(|variant| variant.ident).collect();
185-
186180
def.variants.is_empty() && !cx.is_foreign_non_exhaustive_enum(pat_ty)
187181
}
188182
_ => false,
189183
}
190184
};
191185
if !scrutinee_is_visibly_uninhabited {
192186
// We know the type is inhabited, so this must be wrong
187+
let (def_span, missing_variants) = match pat_ty.kind {
188+
ty::Adt(def, _) => (
189+
self.tcx.hir().span_if_local(def.did),
190+
def.variants.iter().map(|variant| variant.ident).collect(),
191+
),
192+
_ => (None, vec![]),
193+
};
194+
193195
let mut err = create_e0004(
194196
self.tcx.sess,
195197
scrut.span,

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: type `Foo` is non-empty
28+
//~^ ERROR non-exhaustive patterns: pattern `Foo` of type `Foo` is not handled
2929
}
3030
}
3131

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ 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: type `Foo` is non-empty
9+
error[E0004]: non-exhaustive patterns: pattern `Foo` of type `Foo` is not handled
1010
--> $DIR/always-inhabited-union-ref.rs:27:11
1111
|
12-
LL | match uninhab_union() {
13-
| ^^^^^^^^^^^^^^^
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+
| ^^^^^^^^^^^^^^^
1422
|
1523
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1624

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

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

6-
struct NonEmptyStruct(bool);
7-
enum NonEmptyEnum1 {
8-
Foo(bool),
6+
struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here
7+
enum NonEmptyEnum1 { //~ `NonEmptyEnum1` defined here
8+
Foo(bool), //~ variant not covered
99
}
10-
enum NonEmptyEnum2 {
11-
Foo(bool),
12-
Bar,
10+
enum NonEmptyEnum2 { //~ `NonEmptyEnum2` defined here
11+
Foo(bool), //~ variant not covered
12+
Bar, //~ variant not covered
1313
}
14-
enum NonEmptyEnum5 {
14+
enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here
1515
V1, V2, V3, V4, V5,
1616
}
1717

@@ -35,11 +35,11 @@ fn main() {
3535
match 0u8 {}
3636
//~^ ERROR type `u8` is non-empty
3737
match NonEmptyStruct(true) {}
38-
//~^ ERROR type `NonEmptyStruct` is non-empty
38+
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
3939
match NonEmptyEnum1::Foo(true) {}
40-
//~^ ERROR type `NonEmptyEnum1` is non-empty
40+
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
4141
match NonEmptyEnum2::Foo(true) {}
42-
//~^ ERROR type `NonEmptyEnum2` is non-empty
42+
//~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled
4343
match NonEmptyEnum5::V1 {}
44-
//~^ ERROR type `NonEmptyEnum5` is non-empty
44+
//~^ ERROR multiple patterns of type `NonEmptyEnum5` are not handled
4545
}

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,60 @@ 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: type `NonEmptyStruct` is non-empty
33+
error[E0004]: non-exhaustive patterns: pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
3434
--> $DIR/match-empty-exhaustive_patterns.rs:37:11
3535
|
36+
LL | struct NonEmptyStruct(bool);
37+
| ----------------------------
38+
| | |
39+
| | variant not covered
40+
| `NonEmptyStruct` defined here
41+
...
3642
LL | match NonEmptyStruct(true) {}
3743
| ^^^^^^^^^^^^^^^^^^^^
3844
|
3945
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4046

41-
error[E0004]: non-exhaustive patterns: type `NonEmptyEnum1` is non-empty
47+
error[E0004]: non-exhaustive patterns: pattern `Foo` of type `NonEmptyEnum1` is not handled
4248
--> $DIR/match-empty-exhaustive_patterns.rs:39:11
4349
|
44-
LL | match NonEmptyEnum1::Foo(true) {}
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^
50+
LL | / enum NonEmptyEnum1 {
51+
LL | | Foo(bool),
52+
| | --- variant not covered
53+
LL | | }
54+
| |_- `NonEmptyEnum1` defined here
55+
...
56+
LL | match NonEmptyEnum1::Foo(true) {}
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^
4658
|
4759
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4860

49-
error[E0004]: non-exhaustive patterns: type `NonEmptyEnum2` is non-empty
61+
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum2` are not handled
5062
--> $DIR/match-empty-exhaustive_patterns.rs:41:11
5163
|
52-
LL | match NonEmptyEnum2::Foo(true) {}
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^
64+
LL | / enum NonEmptyEnum2 {
65+
LL | | Foo(bool),
66+
| | --- variant not covered
67+
LL | | Bar,
68+
| | --- variant not covered
69+
LL | | }
70+
| |_- `NonEmptyEnum2` defined here
71+
...
72+
LL | match NonEmptyEnum2::Foo(true) {}
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^
5474
|
5575
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5676

57-
error[E0004]: non-exhaustive patterns: type `NonEmptyEnum5` is non-empty
77+
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum5` are not handled
5878
--> $DIR/match-empty-exhaustive_patterns.rs:43:11
5979
|
60-
LL | match NonEmptyEnum5::V1 {}
61-
| ^^^^^^^^^^^^^^^^^
80+
LL | / enum NonEmptyEnum5 {
81+
LL | | V1, V2, V3, V4, V5,
82+
LL | | }
83+
| |_- `NonEmptyEnum5` defined here
84+
...
85+
LL | match NonEmptyEnum5::V1 {}
86+
| ^^^^^^^^^^^^^^^^^
6287
|
6388
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
6489

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: type `uninhabited::IndirectUninhabitedEnum` is non-empty
1+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedEnum` of type `uninhabited::IndirectUninhabitedEnum` is not handled
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: type `uninhabited::IndirectUninhabitedStruct` is non-empty
9+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedStruct` of type `uninhabited::IndirectUninhabitedStruct` is not handled
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: type `uninhabited::IndirectUninhabitedTupleStruct` is non-empty
17+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedTupleStruct` of type `uninhabited::IndirectUninhabitedTupleStruct` is not handled
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: type `uninhabited::IndirectUninhabitedVariants` is non-empty
25+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedVariants` of type `uninhabited::IndirectUninhabitedVariants` is not handled
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ 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: type `uninhabited::UninhabitedStruct` is non-empty
9+
error[E0004]: non-exhaustive patterns: pattern `UninhabitedStruct` of type `uninhabited::UninhabitedStruct` is not handled
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: type `uninhabited::UninhabitedTupleStruct` is non-empty
17+
error[E0004]: non-exhaustive patterns: pattern `UninhabitedTupleStruct` of type `uninhabited::UninhabitedTupleStruct` is not handled
1818
--> $DIR/match_with_exhaustive_patterns.rs:29: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: type `uninhabited::UninhabitedVariants` is non-empty
25+
error[E0004]: non-exhaustive patterns: multiple patterns of type `uninhabited::UninhabitedVariants` are not handled
2626
--> $DIR/match_with_exhaustive_patterns.rs:33:11
2727
|
2828
LL | match x {}

0 commit comments

Comments
 (0)