Skip to content

Commit

Permalink
Only warn about missing patterns in the case of an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Dec 4, 2019
1 parent 2099dd1 commit c0f3c06
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/pattern/check_match.rs
Expand Up @@ -176,7 +176,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
} else {
match pat_ty.kind {
ty::Never => true,
ty::Adt(def, _) => {
ty::Adt(def, _) if def.is_enum() => {
def.variants.is_empty() && !cx.is_foreign_non_exhaustive_enum(pat_ty)
}
_ => false,
Expand All @@ -185,7 +185,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
if !scrutinee_is_visibly_uninhabited {
// We know the type is inhabited, so this must be wrong
let (def_span, missing_variants) = match pat_ty.kind {
ty::Adt(def, _) => (
ty::Adt(def, _) if def.is_enum() => (
self.tcx.hir().span_if_local(def.did),
def.variants.iter().map(|variant| variant.ident).collect(),
),
Expand Down
Expand Up @@ -25,7 +25,7 @@ fn match_on_uninhab() {
}

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

Expand Down
14 changes: 3 additions & 11 deletions src/test/ui/pattern/usefulness/always-inhabited-union-ref.stderr
Expand Up @@ -6,19 +6,11 @@ LL | match uninhab_ref() {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `Foo` of type `Foo` is not handled
error[E0004]: non-exhaustive patterns: type `Foo` is non-empty
--> $DIR/always-inhabited-union-ref.rs:27:11
|
LL | pub union Foo {
| - --- variant not covered
| _|
| |
LL | | foo: !,
LL | | }
| |_- `Foo` defined here
...
LL | match uninhab_union() {
| ^^^^^^^^^^^^^^^
LL | match uninhab_union() {
| ^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
Expand Up @@ -3,7 +3,7 @@
#![deny(unreachable_patterns)]
enum Foo {}

struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here
struct NonEmptyStruct(bool);
union NonEmptyUnion1 {
foo: (),
}
Expand Down Expand Up @@ -42,11 +42,11 @@ fn main() {
match 0u8 {}
//~^ ERROR type `u8` is non-empty
match NonEmptyStruct(true) {}
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
//~^ ERROR type `NonEmptyStruct` is non-empty
match (NonEmptyUnion1 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
//~^ ERROR type `NonEmptyUnion1` is non-empty
match (NonEmptyUnion2 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
//~^ ERROR type `NonEmptyUnion2` is non-empty
match NonEmptyEnum1::Foo(true) {}
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
match NonEmptyEnum2::Foo(true) {}
Expand Down
Expand Up @@ -30,50 +30,27 @@ LL | match 0u8 {}
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

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

error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
--> $DIR/match-empty-exhaustive_patterns.rs:46:11
|
LL | union NonEmptyUnion1 {
| - -------------- variant not covered
| _|
| |
LL | | foo: (),
LL | | }
| |_- `NonEmptyUnion1` defined here
...
LL | match (NonEmptyUnion1 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | match (NonEmptyUnion1 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
--> $DIR/match-empty-exhaustive_patterns.rs:48:11
|
LL | union NonEmptyUnion2 {
| - -------------- variant not covered
| _|
| |
LL | | foo: (),
LL | | bar: (),
LL | | }
| |_- `NonEmptyUnion2` defined here
...
LL | match (NonEmptyUnion2 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | match (NonEmptyUnion2 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/pattern/usefulness/match-empty.rs
Expand Up @@ -2,7 +2,7 @@
#![deny(unreachable_patterns)]
enum Foo {}

struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here
struct NonEmptyStruct(bool);
union NonEmptyUnion1 {
foo: (),
}
Expand Down Expand Up @@ -45,11 +45,11 @@ fn main() {
match 0u8 {}
//~^ ERROR type `u8` is non-empty
match NonEmptyStruct(true) {}
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
//~^ ERROR type `NonEmptyStruct` is non-empty
match (NonEmptyUnion1 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
//~^ ERROR type `NonEmptyUnion1` is non-empty
match (NonEmptyUnion2 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
//~^ ERROR type `NonEmptyUnion2` is non-empty
match NonEmptyEnum1::Foo(true) {}
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
match NonEmptyEnum2::Foo(true) {}
Expand Down
37 changes: 7 additions & 30 deletions src/test/ui/pattern/usefulness/match-empty.stderr
Expand Up @@ -6,50 +6,27 @@ LL | match 0u8 {}
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
--> $DIR/match-empty.rs:47:11
|
LL | struct NonEmptyStruct(bool);
| ----------------------------
| | |
| | variant not covered
| `NonEmptyStruct` defined here
...
LL | match NonEmptyStruct(true) {}
| ^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
--> $DIR/match-empty.rs:49:11
|
LL | union NonEmptyUnion1 {
| - -------------- variant not covered
| _|
| |
LL | | foo: (),
LL | | }
| |_- `NonEmptyUnion1` defined here
...
LL | match (NonEmptyUnion1 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | match (NonEmptyUnion1 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
--> $DIR/match-empty.rs:51:11
|
LL | union NonEmptyUnion2 {
| - -------------- variant not covered
| _|
| |
LL | | foo: (),
LL | | bar: (),
LL | | }
| |_- `NonEmptyUnion2` defined here
...
LL | match (NonEmptyUnion2 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | match (NonEmptyUnion2 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
@@ -1,28 +1,28 @@
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedEnum` of type `uninhabited::IndirectUninhabitedEnum` is not handled
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedEnum` is non-empty
--> $DIR/indirect_match_with_exhaustive_patterns.rs:22:11
|
LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

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

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

error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedVariants` of type `uninhabited::IndirectUninhabitedVariants` is not handled
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedVariants` is non-empty
--> $DIR/indirect_match_with_exhaustive_patterns.rs:36:11
|
LL | match x {}
Expand Down
Expand Up @@ -6,15 +6,15 @@ LL | match x {}
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

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

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

0 comments on commit c0f3c06

Please sign in to comment.