From 5a3b7d205589f49280e26e2f8e1b37719dee158a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Dec 2019 15:57:04 +0000 Subject: [PATCH] Add tests --- .../match-empty-exhaustive_patterns.rs | 47 +++++- .../match-empty-exhaustive_patterns.stderr | 142 ++++++++++++++--- src/test/ui/pattern/usefulness/match-empty.rs | 47 ++++-- .../ui/pattern/usefulness/match-empty.stderr | 143 +++++++++++++++--- 4 files changed, 313 insertions(+), 66 deletions(-) diff --git a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs index b55673a1322..78f3e4a9b43 100644 --- a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs +++ b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs @@ -22,11 +22,27 @@ enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here V1, V2, V3, V4, V5, } +macro_rules! match_empty { + ($e:expr) => { + match $e {} + }; +} +macro_rules! match_false { + ($e:expr) => { + match $e { + _ if false => {} + } + }; +} + fn foo(x: Foo) { - match x {} // ok + match_empty!(x); // ok match x { _ => {}, //~ ERROR unreachable pattern } + match x { + _ if false => {}, //~ ERROR unreachable pattern + } } fn main() { @@ -39,18 +55,33 @@ fn main() { Some(_) => {} //~ ERROR unreachable pattern } - match 0u8 {} + match_empty!(0u8); //~^ ERROR type `u8` is non-empty - match NonEmptyStruct(true) {} + match_empty!(NonEmptyStruct(true)); //~^ ERROR type `NonEmptyStruct` is non-empty - match (NonEmptyUnion1 { foo: () }) {} + match_empty!((NonEmptyUnion1 { foo: () })); //~^ ERROR type `NonEmptyUnion1` is non-empty - match (NonEmptyUnion2 { foo: () }) {} + match_empty!((NonEmptyUnion2 { foo: () })); //~^ ERROR type `NonEmptyUnion2` is non-empty - match NonEmptyEnum1::Foo(true) {} + match_empty!(NonEmptyEnum1::Foo(true)); //~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled - match NonEmptyEnum2::Foo(true) {} + match_empty!(NonEmptyEnum2::Foo(true)); //~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled - match NonEmptyEnum5::V1 {} + match_empty!(NonEmptyEnum5::V1); //~^ ERROR multiple patterns of type `NonEmptyEnum5` are not handled + + match_false!(0u8); + //~^ ERROR `_` not covered + match_false!(NonEmptyStruct(true)); + //~^ ERROR `_` not covered + match_false!((NonEmptyUnion1 { foo: () })); + //~^ ERROR `_` not covered + match_false!((NonEmptyUnion2 { foo: () })); + //~^ ERROR `_` not covered + match_false!(NonEmptyEnum1::Foo(true)); + //~^ ERROR `_` not covered + match_false!(NonEmptyEnum2::Foo(true)); + //~^ ERROR `_` not covered + match_false!(NonEmptyEnum5::V1); + //~^ ERROR `_` not covered } diff --git a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr index 7b8bb4158e7..8af74f6e52b 100644 --- a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr +++ b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr @@ -1,5 +1,5 @@ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:28:9 + --> $DIR/match-empty-exhaustive_patterns.rs:41:9 | LL | _ => {}, | ^ @@ -11,51 +11,57 @@ LL | #![deny(unreachable_patterns)] | ^^^^^^^^^^^^^^^^^^^^ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:35:9 + --> $DIR/match-empty-exhaustive_patterns.rs:44:9 + | +LL | _ if false => {}, + | ^ + +error: unreachable pattern + --> $DIR/match-empty-exhaustive_patterns.rs:51:9 | LL | Some(_) => {} | ^^^^^^^ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:39:9 + --> $DIR/match-empty-exhaustive_patterns.rs:55:9 | LL | Some(_) => {} | ^^^^^^^ error[E0004]: non-exhaustive patterns: type `u8` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:42:11 + --> $DIR/match-empty-exhaustive_patterns.rs:58:18 | -LL | match 0u8 {} - | ^^^ +LL | match_empty!(0u8); + | ^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:44:11 + --> $DIR/match-empty-exhaustive_patterns.rs:60:18 | -LL | match NonEmptyStruct(true) {} - | ^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyStruct(true)); + | ^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:46:11 + --> $DIR/match-empty-exhaustive_patterns.rs:62:18 | -LL | match (NonEmptyUnion1 { foo: () }) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!((NonEmptyUnion1 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:48:11 + --> $DIR/match-empty-exhaustive_patterns.rs:64:18 | -LL | match (NonEmptyUnion2 { foo: () }) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!((NonEmptyUnion2 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = 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 `NonEmptyEnum1` is not handled - --> $DIR/match-empty-exhaustive_patterns.rs:50:11 + --> $DIR/match-empty-exhaustive_patterns.rs:66:18 | LL | / enum NonEmptyEnum1 { LL | | Foo(bool), @@ -63,13 +69,13 @@ LL | | Foo(bool), LL | | } | |_- `NonEmptyEnum1` defined here ... -LL | match NonEmptyEnum1::Foo(true) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyEnum1::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum2` are not handled - --> $DIR/match-empty-exhaustive_patterns.rs:52:11 + --> $DIR/match-empty-exhaustive_patterns.rs:68:18 | LL | / enum NonEmptyEnum2 { LL | | Foo(bool), @@ -79,24 +85,110 @@ LL | | Bar, LL | | } | |_- `NonEmptyEnum2` defined here ... -LL | match NonEmptyEnum2::Foo(true) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyEnum2::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum5` are not handled - --> $DIR/match-empty-exhaustive_patterns.rs:54:11 + --> $DIR/match-empty-exhaustive_patterns.rs:70:18 | LL | / enum NonEmptyEnum5 { LL | | V1, V2, V3, V4, V5, LL | | } | |_- `NonEmptyEnum5` defined here ... -LL | match NonEmptyEnum5::V1 {} - | ^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyEnum5::V1); + | ^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 10 previous errors +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty-exhaustive_patterns.rs:73:18 + | +LL | match_false!(0u8); + | ^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty-exhaustive_patterns.rs:75:18 + | +LL | struct NonEmptyStruct(bool); + | ---------------------------- `NonEmptyStruct` defined here +... +LL | match_false!(NonEmptyStruct(true)); + | ^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty-exhaustive_patterns.rs:77:18 + | +LL | / union NonEmptyUnion1 { +LL | | foo: (), +LL | | } + | |_- `NonEmptyUnion1` defined here +... +LL | match_false!((NonEmptyUnion1 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty-exhaustive_patterns.rs:79:18 + | +LL | / union NonEmptyUnion2 { +LL | | foo: (), +LL | | bar: (), +LL | | } + | |_- `NonEmptyUnion2` defined here +... +LL | match_false!((NonEmptyUnion2 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty-exhaustive_patterns.rs:81:18 + | +LL | / enum NonEmptyEnum1 { +LL | | Foo(bool), +LL | | } + | |_- `NonEmptyEnum1` defined here +... +LL | match_false!(NonEmptyEnum1::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty-exhaustive_patterns.rs:83:18 + | +LL | / enum NonEmptyEnum2 { +LL | | Foo(bool), +LL | | Bar, +LL | | } + | |_- `NonEmptyEnum2` defined here +... +LL | match_false!(NonEmptyEnum2::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty-exhaustive_patterns.rs:85:18 + | +LL | / enum NonEmptyEnum5 { +LL | | V1, V2, V3, V4, V5, +LL | | } + | |_- `NonEmptyEnum5` defined here +... +LL | match_false!(NonEmptyEnum5::V1); + | ^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error: aborting due to 18 previous errors For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/pattern/usefulness/match-empty.rs b/src/test/ui/pattern/usefulness/match-empty.rs index 3adbcb8cbbb..80b761728b8 100644 --- a/src/test/ui/pattern/usefulness/match-empty.rs +++ b/src/test/ui/pattern/usefulness/match-empty.rs @@ -21,11 +21,23 @@ enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here V1, V2, V3, V4, V5, } -fn foo1(x: Foo) { - match x {} // ok +macro_rules! match_empty { + ($e:expr) => { + match $e {} + }; +} +macro_rules! match_false { + ($e:expr) => { + match $e { + _ if false => {} + } + }; } -fn foo2(x: Foo) { +fn foo(x: Foo) { + match_empty!(x); // ok + match_false!(x); // Not detected as unreachable nor exhaustive. + //~^ ERROR non-exhaustive patterns: `_` not covered match x { _ => {}, // Not detected as unreachable, see #55123. } @@ -42,18 +54,33 @@ fn main() { Some(_) => {} } - match 0u8 {} + match_empty!(0u8); //~^ ERROR type `u8` is non-empty - match NonEmptyStruct(true) {} + match_empty!(NonEmptyStruct(true)); //~^ ERROR type `NonEmptyStruct` is non-empty - match (NonEmptyUnion1 { foo: () }) {} + match_empty!((NonEmptyUnion1 { foo: () })); //~^ ERROR type `NonEmptyUnion1` is non-empty - match (NonEmptyUnion2 { foo: () }) {} + match_empty!((NonEmptyUnion2 { foo: () })); //~^ ERROR type `NonEmptyUnion2` is non-empty - match NonEmptyEnum1::Foo(true) {} + match_empty!(NonEmptyEnum1::Foo(true)); //~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled - match NonEmptyEnum2::Foo(true) {} + match_empty!(NonEmptyEnum2::Foo(true)); //~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled - match NonEmptyEnum5::V1 {} + match_empty!(NonEmptyEnum5::V1); //~^ ERROR multiple patterns of type `NonEmptyEnum5` are not handled + + match_false!(0u8); + //~^ ERROR `_` not covered + match_false!(NonEmptyStruct(true)); + //~^ ERROR `_` not covered + match_false!((NonEmptyUnion1 { foo: () })); + //~^ ERROR `_` not covered + match_false!((NonEmptyUnion2 { foo: () })); + //~^ ERROR `_` not covered + match_false!(NonEmptyEnum1::Foo(true)); + //~^ ERROR `_` not covered + match_false!(NonEmptyEnum2::Foo(true)); + //~^ ERROR `_` not covered + match_false!(NonEmptyEnum5::V1); + //~^ ERROR `_` not covered } diff --git a/src/test/ui/pattern/usefulness/match-empty.stderr b/src/test/ui/pattern/usefulness/match-empty.stderr index 7446a8e4aee..b0532de66fd 100644 --- a/src/test/ui/pattern/usefulness/match-empty.stderr +++ b/src/test/ui/pattern/usefulness/match-empty.stderr @@ -1,37 +1,48 @@ -error[E0004]: non-exhaustive patterns: type `u8` is non-empty - --> $DIR/match-empty.rs:45:11 +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:39:18 | -LL | match 0u8 {} - | ^^^ +LL | enum Foo {} + | ----------- `Foo` defined here +... +LL | match_false!(x); // Not detected as unreachable nor exhaustive. + | ^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: type `u8` is non-empty + --> $DIR/match-empty.rs:57:18 + | +LL | match_empty!(0u8); + | ^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty - --> $DIR/match-empty.rs:47:11 + --> $DIR/match-empty.rs:59:18 | -LL | match NonEmptyStruct(true) {} - | ^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyStruct(true)); + | ^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty - --> $DIR/match-empty.rs:49:11 + --> $DIR/match-empty.rs:61:18 | -LL | match (NonEmptyUnion1 { foo: () }) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!((NonEmptyUnion1 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty - --> $DIR/match-empty.rs:51:11 + --> $DIR/match-empty.rs:63:18 | -LL | match (NonEmptyUnion2 { foo: () }) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!((NonEmptyUnion2 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = 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 `NonEmptyEnum1` is not handled - --> $DIR/match-empty.rs:53:11 + --> $DIR/match-empty.rs:65:18 | LL | / enum NonEmptyEnum1 { LL | | Foo(bool), @@ -39,13 +50,13 @@ LL | | Foo(bool), LL | | } | |_- `NonEmptyEnum1` defined here ... -LL | match NonEmptyEnum1::Foo(true) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyEnum1::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum2` are not handled - --> $DIR/match-empty.rs:55:11 + --> $DIR/match-empty.rs:67:18 | LL | / enum NonEmptyEnum2 { LL | | Foo(bool), @@ -55,24 +66,110 @@ LL | | Bar, LL | | } | |_- `NonEmptyEnum2` defined here ... -LL | match NonEmptyEnum2::Foo(true) {} - | ^^^^^^^^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyEnum2::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum5` are not handled - --> $DIR/match-empty.rs:57:11 + --> $DIR/match-empty.rs:69:18 | LL | / enum NonEmptyEnum5 { LL | | V1, V2, V3, V4, V5, LL | | } | |_- `NonEmptyEnum5` defined here ... -LL | match NonEmptyEnum5::V1 {} - | ^^^^^^^^^^^^^^^^^ +LL | match_empty!(NonEmptyEnum5::V1); + | ^^^^^^^^^^^^^^^^^ | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 7 previous errors +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:72:18 + | +LL | match_false!(0u8); + | ^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:74:18 + | +LL | struct NonEmptyStruct(bool); + | ---------------------------- `NonEmptyStruct` defined here +... +LL | match_false!(NonEmptyStruct(true)); + | ^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:76:18 + | +LL | / union NonEmptyUnion1 { +LL | | foo: (), +LL | | } + | |_- `NonEmptyUnion1` defined here +... +LL | match_false!((NonEmptyUnion1 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:78:18 + | +LL | / union NonEmptyUnion2 { +LL | | foo: (), +LL | | bar: (), +LL | | } + | |_- `NonEmptyUnion2` defined here +... +LL | match_false!((NonEmptyUnion2 { foo: () })); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:80:18 + | +LL | / enum NonEmptyEnum1 { +LL | | Foo(bool), +LL | | } + | |_- `NonEmptyEnum1` defined here +... +LL | match_false!(NonEmptyEnum1::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:82:18 + | +LL | / enum NonEmptyEnum2 { +LL | | Foo(bool), +LL | | Bar, +LL | | } + | |_- `NonEmptyEnum2` defined here +... +LL | match_false!(NonEmptyEnum2::Foo(true)); + | ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error[E0004]: non-exhaustive patterns: `_` not covered + --> $DIR/match-empty.rs:84:18 + | +LL | / enum NonEmptyEnum5 { +LL | | V1, V2, V3, V4, V5, +LL | | } + | |_- `NonEmptyEnum5` defined here +... +LL | match_false!(NonEmptyEnum5::V1); + | ^^^^^^^^^^^^^^^^^ pattern `_` not covered + | + = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms + +error: aborting due to 15 previous errors For more information about this error, try `rustc --explain E0004`.