1
Fork 0

Only warn about missing patterns in the case of an enum

This commit is contained in:
Nadrieril 2019-12-03 14:08:14 +00:00
parent 2099dd1aa2
commit c0f3c06c6d
9 changed files with 34 additions and 88 deletions

View file

@ -176,7 +176,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
} else { } else {
match pat_ty.kind { match pat_ty.kind {
ty::Never => true, 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) def.variants.is_empty() && !cx.is_foreign_non_exhaustive_enum(pat_ty)
} }
_ => false, _ => false,
@ -185,7 +185,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
if !scrutinee_is_visibly_uninhabited { if !scrutinee_is_visibly_uninhabited {
// We know the type is inhabited, so this must be wrong // We know the type is inhabited, so this must be wrong
let (def_span, missing_variants) = match pat_ty.kind { 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), self.tcx.hir().span_if_local(def.did),
def.variants.iter().map(|variant| variant.ident).collect(), def.variants.iter().map(|variant| variant.ident).collect(),
), ),

View file

@ -25,7 +25,7 @@ fn match_on_uninhab() {
} }
match uninhab_union() { match uninhab_union() {
//~^ ERROR non-exhaustive patterns: pattern `Foo` of type `Foo` is not handled //~^ ERROR non-exhaustive patterns: type `Foo` is non-empty
} }
} }

View file

@ -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 = 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 --> $DIR/always-inhabited-union-ref.rs:27:11
| |
LL | pub union Foo { LL | match uninhab_union() {
| - --- variant not covered | ^^^^^^^^^^^^^^^
| _|
| |
LL | | foo: !,
LL | | }
| |_- `Foo` defined here
...
LL | match uninhab_union() {
| ^^^^^^^^^^^^^^^
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

View file

@ -3,7 +3,7 @@
#![deny(unreachable_patterns)] #![deny(unreachable_patterns)]
enum Foo {} enum Foo {}
struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here struct NonEmptyStruct(bool);
union NonEmptyUnion1 { union NonEmptyUnion1 {
foo: (), foo: (),
} }
@ -42,11 +42,11 @@ fn main() {
match 0u8 {} match 0u8 {}
//~^ ERROR type `u8` is non-empty //~^ ERROR type `u8` is non-empty
match NonEmptyStruct(true) {} match NonEmptyStruct(true) {}
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled //~^ ERROR type `NonEmptyStruct` is non-empty
match (NonEmptyUnion1 { foo: () }) {} match (NonEmptyUnion1 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled //~^ ERROR type `NonEmptyUnion1` is non-empty
match (NonEmptyUnion2 { foo: () }) {} match (NonEmptyUnion2 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled //~^ ERROR type `NonEmptyUnion2` is non-empty
match NonEmptyEnum1::Foo(true) {} match NonEmptyEnum1::Foo(true) {}
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled //~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
match NonEmptyEnum2::Foo(true) {} match NonEmptyEnum2::Foo(true) {}

View file

@ -30,50 +30,27 @@ LL | match 0u8 {}
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match-empty-exhaustive_patterns.rs:44:11
| |
LL | struct NonEmptyStruct(bool);
| ----------------------------
| | |
| | variant not covered
| `NonEmptyStruct` defined here
...
LL | match NonEmptyStruct(true) {} LL | match NonEmptyStruct(true) {}
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match-empty-exhaustive_patterns.rs:46:11
| |
LL | union NonEmptyUnion1 { LL | match (NonEmptyUnion1 { foo: () }) {}
| - -------------- variant not covered | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| _|
| |
LL | | foo: (),
LL | | }
| |_- `NonEmptyUnion1` defined here
...
LL | match (NonEmptyUnion1 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match-empty-exhaustive_patterns.rs:48:11
| |
LL | union NonEmptyUnion2 { LL | match (NonEmptyUnion2 { foo: () }) {}
| - -------------- variant not covered | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| _|
| |
LL | | foo: (),
LL | | bar: (),
LL | | }
| |_- `NonEmptyUnion2` defined here
...
LL | match (NonEmptyUnion2 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

View file

@ -2,7 +2,7 @@
#![deny(unreachable_patterns)] #![deny(unreachable_patterns)]
enum Foo {} enum Foo {}
struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here struct NonEmptyStruct(bool);
union NonEmptyUnion1 { union NonEmptyUnion1 {
foo: (), foo: (),
} }
@ -45,11 +45,11 @@ fn main() {
match 0u8 {} match 0u8 {}
//~^ ERROR type `u8` is non-empty //~^ ERROR type `u8` is non-empty
match NonEmptyStruct(true) {} match NonEmptyStruct(true) {}
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled //~^ ERROR type `NonEmptyStruct` is non-empty
match (NonEmptyUnion1 { foo: () }) {} match (NonEmptyUnion1 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion1` of type `NonEmptyUnion1` is not handled //~^ ERROR type `NonEmptyUnion1` is non-empty
match (NonEmptyUnion2 { foo: () }) {} match (NonEmptyUnion2 { foo: () }) {}
//~^ ERROR pattern `NonEmptyUnion2` of type `NonEmptyUnion2` is not handled //~^ ERROR type `NonEmptyUnion2` is non-empty
match NonEmptyEnum1::Foo(true) {} match NonEmptyEnum1::Foo(true) {}
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled //~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
match NonEmptyEnum2::Foo(true) {} match NonEmptyEnum2::Foo(true) {}

View file

@ -6,50 +6,27 @@ LL | match 0u8 {}
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match-empty.rs:47:11
| |
LL | struct NonEmptyStruct(bool);
| ----------------------------
| | |
| | variant not covered
| `NonEmptyStruct` defined here
...
LL | match NonEmptyStruct(true) {} LL | match NonEmptyStruct(true) {}
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match-empty.rs:49:11
| |
LL | union NonEmptyUnion1 { LL | match (NonEmptyUnion1 { foo: () }) {}
| - -------------- variant not covered | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| _|
| |
LL | | foo: (),
LL | | }
| |_- `NonEmptyUnion1` defined here
...
LL | match (NonEmptyUnion1 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match-empty.rs:51:11
| |
LL | union NonEmptyUnion2 { LL | match (NonEmptyUnion2 { foo: () }) {}
| - -------------- variant not covered | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| _|
| |
LL | | foo: (),
LL | | bar: (),
LL | | }
| |_- `NonEmptyUnion2` defined here
...
LL | match (NonEmptyUnion2 { foo: () }) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

View file

@ -1,4 +1,4 @@
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 --> $DIR/indirect_match_with_exhaustive_patterns.rs:22:11
| |
LL | match x {} LL | match x {}
@ -6,7 +6,7 @@ LL | match x {}
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/indirect_match_with_exhaustive_patterns.rs:26:11
| |
LL | match x {} LL | match x {}
@ -14,7 +14,7 @@ LL | match x {}
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/indirect_match_with_exhaustive_patterns.rs:30:11
| |
LL | match x {} LL | match x {}
@ -22,7 +22,7 @@ LL | match x {}
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/indirect_match_with_exhaustive_patterns.rs:36:11
| |
LL | match x {} LL | match x {}

View file

@ -6,7 +6,7 @@ LL | match x {}
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match_with_exhaustive_patterns.rs:25:11
| |
LL | match x {} LL | match x {}
@ -14,7 +14,7 @@ LL | match x {}
| |
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = 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 --> $DIR/match_with_exhaustive_patterns.rs:29:11
| |
LL | match x {} LL | match x {}