Handle empty matches cleanly
This commit is contained in:
parent
257ed899c8
commit
b025813f03
6 changed files with 78 additions and 71 deletions
|
|
@ -44,22 +44,20 @@ macro_rules! match_false {
|
|||
fn foo(x: Foo) {
|
||||
match x {} // ok
|
||||
match x {
|
||||
_ => {}, // Not detected as unreachable, see #55123.
|
||||
_ => {}, //~ ERROR unreachable pattern
|
||||
}
|
||||
match x {
|
||||
//~^ ERROR non-exhaustive patterns: `_` not covered
|
||||
_ if false => {}, // Not detected as unreachable nor exhaustive.
|
||||
_ if false => {}, //~ ERROR unreachable pattern
|
||||
}
|
||||
}
|
||||
|
||||
fn never(x: !) {
|
||||
match x {} // ok
|
||||
match x {
|
||||
_ => {}, // Not detected as unreachable.
|
||||
_ => {}, //~ ERROR unreachable pattern
|
||||
}
|
||||
match x {
|
||||
//~^ ERROR non-exhaustive patterns: `_` not covered
|
||||
_ if false => {}, // Not detected as unreachable nor exhaustive.
|
||||
_ if false => {}, //~ ERROR unreachable pattern
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +1,35 @@
|
|||
error[E0004]: non-exhaustive patterns: `_` not covered
|
||||
--> $DIR/match-empty.rs:49:11
|
||||
error: unreachable pattern
|
||||
--> $DIR/match-empty.rs:47:9
|
||||
|
|
||||
LL | enum Foo {}
|
||||
| ----------- `Foo` defined here
|
||||
...
|
||||
LL | match x {
|
||||
| ^ pattern `_` not covered
|
||||
LL | _ => {},
|
||||
| ^
|
||||
|
|
||||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
|
||||
= note: the matched value is of type `Foo`
|
||||
note: the lint level is defined here
|
||||
--> $DIR/match-empty.rs:3:9
|
||||
|
|
||||
LL | #![deny(unreachable_patterns)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `_` not covered
|
||||
--> $DIR/match-empty.rs:60:11
|
||||
error: unreachable pattern
|
||||
--> $DIR/match-empty.rs:50:9
|
||||
|
|
||||
LL | match x {
|
||||
| ^ pattern `_` not covered
|
||||
LL | _ if false => {},
|
||||
| ^
|
||||
|
||||
error: unreachable pattern
|
||||
--> $DIR/match-empty.rs:57:9
|
||||
|
|
||||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
|
||||
= note: the matched value is of type `!`
|
||||
LL | _ => {},
|
||||
| ^
|
||||
|
||||
error: unreachable pattern
|
||||
--> $DIR/match-empty.rs:60:9
|
||||
|
|
||||
LL | _ if false => {},
|
||||
| ^
|
||||
|
||||
error[E0004]: non-exhaustive patterns: type `u8` is non-empty
|
||||
--> $DIR/match-empty.rs:77:18
|
||||
--> $DIR/match-empty.rs:75:18
|
||||
|
|
||||
LL | match_empty!(0u8);
|
||||
| ^^^
|
||||
|
|
@ -29,7 +38,7 @@ LL | match_empty!(0u8);
|
|||
= note: the matched value is of type `u8`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
|
||||
--> $DIR/match-empty.rs:79:18
|
||||
--> $DIR/match-empty.rs:77:18
|
||||
|
|
||||
LL | struct NonEmptyStruct(bool);
|
||||
| ---------------------------- `NonEmptyStruct` defined here
|
||||
|
|
@ -41,7 +50,7 @@ LL | match_empty!(NonEmptyStruct(true));
|
|||
= note: the matched value is of type `NonEmptyStruct`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
|
||||
--> $DIR/match-empty.rs:81:18
|
||||
--> $DIR/match-empty.rs:79:18
|
||||
|
|
||||
LL | / union NonEmptyUnion1 {
|
||||
LL | | foo: (),
|
||||
|
|
@ -55,7 +64,7 @@ LL | match_empty!((NonEmptyUnion1 { foo: () }));
|
|||
= note: the matched value is of type `NonEmptyUnion1`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
|
||||
--> $DIR/match-empty.rs:83:18
|
||||
--> $DIR/match-empty.rs:81:18
|
||||
|
|
||||
LL | / union NonEmptyUnion2 {
|
||||
LL | | foo: (),
|
||||
|
|
@ -70,7 +79,7 @@ LL | match_empty!((NonEmptyUnion2 { foo: () }));
|
|||
= note: the matched value is of type `NonEmptyUnion2`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `Foo(_)` not covered
|
||||
--> $DIR/match-empty.rs:85:18
|
||||
--> $DIR/match-empty.rs:83:18
|
||||
|
|
||||
LL | / enum NonEmptyEnum1 {
|
||||
LL | | Foo(bool),
|
||||
|
|
@ -87,7 +96,7 @@ LL | match_empty!(NonEmptyEnum1::Foo(true));
|
|||
= note: the matched value is of type `NonEmptyEnum1`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered
|
||||
--> $DIR/match-empty.rs:87:18
|
||||
--> $DIR/match-empty.rs:85:18
|
||||
|
|
||||
LL | / enum NonEmptyEnum2 {
|
||||
LL | | Foo(bool),
|
||||
|
|
@ -108,7 +117,7 @@ LL | match_empty!(NonEmptyEnum2::Foo(true));
|
|||
= note: the matched value is of type `NonEmptyEnum2`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered
|
||||
--> $DIR/match-empty.rs:89:18
|
||||
--> $DIR/match-empty.rs:87:18
|
||||
|
|
||||
LL | / enum NonEmptyEnum5 {
|
||||
LL | | V1, V2, V3, V4, V5,
|
||||
|
|
@ -122,7 +131,7 @@ LL | match_empty!(NonEmptyEnum5::V1);
|
|||
= note: the matched value is of type `NonEmptyEnum5`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `_` not covered
|
||||
--> $DIR/match-empty.rs:92:18
|
||||
--> $DIR/match-empty.rs:90:18
|
||||
|
|
||||
LL | match_false!(0u8);
|
||||
| ^^^ pattern `_` not covered
|
||||
|
|
@ -131,7 +140,7 @@ LL | match_false!(0u8);
|
|||
= note: the matched value is of type `u8`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `NonEmptyStruct(_)` not covered
|
||||
--> $DIR/match-empty.rs:94:18
|
||||
--> $DIR/match-empty.rs:92:18
|
||||
|
|
||||
LL | struct NonEmptyStruct(bool);
|
||||
| ---------------------------- `NonEmptyStruct` defined here
|
||||
|
|
@ -143,7 +152,7 @@ LL | match_false!(NonEmptyStruct(true));
|
|||
= note: the matched value is of type `NonEmptyStruct`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `NonEmptyUnion1 { .. }` not covered
|
||||
--> $DIR/match-empty.rs:96:18
|
||||
--> $DIR/match-empty.rs:94:18
|
||||
|
|
||||
LL | / union NonEmptyUnion1 {
|
||||
LL | | foo: (),
|
||||
|
|
@ -157,7 +166,7 @@ LL | match_false!((NonEmptyUnion1 { foo: () }));
|
|||
= note: the matched value is of type `NonEmptyUnion1`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `NonEmptyUnion2 { .. }` not covered
|
||||
--> $DIR/match-empty.rs:98:18
|
||||
--> $DIR/match-empty.rs:96:18
|
||||
|
|
||||
LL | / union NonEmptyUnion2 {
|
||||
LL | | foo: (),
|
||||
|
|
@ -172,7 +181,7 @@ LL | match_false!((NonEmptyUnion2 { foo: () }));
|
|||
= note: the matched value is of type `NonEmptyUnion2`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `Foo(_)` not covered
|
||||
--> $DIR/match-empty.rs:100:18
|
||||
--> $DIR/match-empty.rs:98:18
|
||||
|
|
||||
LL | / enum NonEmptyEnum1 {
|
||||
LL | | Foo(bool),
|
||||
|
|
@ -189,7 +198,7 @@ LL | match_false!(NonEmptyEnum1::Foo(true));
|
|||
= note: the matched value is of type `NonEmptyEnum1`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered
|
||||
--> $DIR/match-empty.rs:102:18
|
||||
--> $DIR/match-empty.rs:100:18
|
||||
|
|
||||
LL | / enum NonEmptyEnum2 {
|
||||
LL | | Foo(bool),
|
||||
|
|
@ -210,7 +219,7 @@ LL | match_false!(NonEmptyEnum2::Foo(true));
|
|||
= note: the matched value is of type `NonEmptyEnum2`
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered
|
||||
--> $DIR/match-empty.rs:104:18
|
||||
--> $DIR/match-empty.rs:102:18
|
||||
|
|
||||
LL | / enum NonEmptyEnum5 {
|
||||
LL | | V1, V2, V3, V4, V5,
|
||||
|
|
@ -223,6 +232,6 @@ LL | match_false!(NonEmptyEnum5::V1);
|
|||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
|
||||
= note: the matched value is of type `NonEmptyEnum5`
|
||||
|
||||
error: aborting due to 16 previous errors
|
||||
error: aborting due to 18 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0004`.
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ pub enum EmptyNonExhaustiveEnum {}
|
|||
fn empty_non_exhaustive(x: EmptyNonExhaustiveEnum) {
|
||||
match x {}
|
||||
match x {
|
||||
_ => {} // not detected as unreachable
|
||||
_ => {}, //~ ERROR unreachable pattern
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,15 @@
|
|||
error: unreachable pattern
|
||||
--> $DIR/enum_same_crate_empty_match.rs:28:9
|
||||
|
|
||||
LL | _ => {},
|
||||
| ^
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/enum_same_crate_empty_match.rs:1:9
|
||||
|
|
||||
LL | #![deny(unreachable_patterns)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0004]: non-exhaustive patterns: `Unit`, `Tuple(_)` and `Struct { .. }` not covered
|
||||
--> $DIR/enum_same_crate_empty_match.rs:33:11
|
||||
|
|
||||
|
|
@ -42,6 +54,6 @@ LL | match NormalEnum::Unit {}
|
|||
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
|
||||
= note: the matched value is of type `NormalEnum`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0004`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue