rust/tests/ui/pattern/usefulness/empty-match.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
8.0 KiB
Rust
Raw Normal View History

2020-11-16 15:13:00 -06:00
// aux-build:empty.rs
2020-12-02 14:24:20 -06:00
// revisions: normal exhaustive_patterns
2020-12-02 17:11:29 -06:00
//
// This tests a match with no arms on various types.
2019-11-30 09:51:26 -06:00
#![feature(never_type)]
2020-11-12 11:24:42 -06:00
#![feature(never_type_fallback)]
2020-12-02 14:24:20 -06:00
#![cfg_attr(exhaustive_patterns, feature(exhaustive_patterns))]
2019-11-30 09:51:26 -06:00
#![deny(unreachable_patterns)]
2023-05-25 13:17:02 -05:00
//~^ NOTE the lint level is defined here
2020-11-16 15:13:00 -06:00
extern crate empty;
enum EmptyEnum {}
2019-11-30 09:51:26 -06:00
2020-12-02 17:11:29 -06:00
struct NonEmptyStruct1;
2023-05-25 13:17:02 -05:00
//~^ NOTE `NonEmptyStruct1` defined here
//~| NOTE `NonEmptyStruct1` defined here
2020-12-02 17:11:29 -06:00
struct NonEmptyStruct2(bool);
2023-05-25 13:17:02 -05:00
//~^ NOTE `NonEmptyStruct2` defined here
//~| NOTE `NonEmptyStruct2` defined here
2020-12-02 17:11:29 -06:00
union NonEmptyUnion1 {
2023-05-25 13:17:02 -05:00
//~^ NOTE `NonEmptyUnion1` defined here
//~| NOTE `NonEmptyUnion1` defined here
2019-12-03 08:07:14 -06:00
foo: (),
}
2020-12-02 17:11:29 -06:00
union NonEmptyUnion2 {
2023-05-25 13:17:02 -05:00
//~^ NOTE `NonEmptyUnion2` defined here
//~| NOTE `NonEmptyUnion2` defined here
2019-12-03 08:07:14 -06:00
foo: (),
bar: (),
}
2020-12-02 17:11:29 -06:00
enum NonEmptyEnum1 {
Foo(bool),
2023-05-25 13:17:02 -05:00
//~^ NOTE `NonEmptyEnum1` defined here
//~| NOTE `NonEmptyEnum1` defined here
//~| NOTE not covered
//~| NOTE not covered
2019-11-30 09:51:26 -06:00
}
2020-12-02 17:11:29 -06:00
enum NonEmptyEnum2 {
Foo(bool),
2023-05-25 13:17:02 -05:00
//~^ NOTE `NonEmptyEnum2` defined here
//~| NOTE `NonEmptyEnum2` defined here
//~| NOTE not covered
//~| NOTE not covered
Bar,
2023-05-25 13:17:02 -05:00
//~^ NOTE not covered
//~| NOTE not covered
2019-11-30 09:51:26 -06:00
}
2020-12-02 17:11:29 -06:00
enum NonEmptyEnum5 {
2023-05-25 13:17:02 -05:00
//~^ NOTE `NonEmptyEnum5` defined here
//~| NOTE `NonEmptyEnum5` defined here
2019-11-30 09:51:26 -06:00
V1, V2, V3, V4, V5,
}
2020-11-16 15:13:00 -06:00
fn empty_enum(x: EmptyEnum) {
match x {} // ok
match x {
_ => {}, //~ ERROR unreachable pattern
}
match x {
_ if false => {}, //~ ERROR unreachable pattern
}
}
fn empty_foreign_enum(x: empty::EmptyForeignEnum) {
2020-11-12 11:24:42 -06:00
match x {} // ok
match x {
_ => {}, //~ ERROR unreachable pattern
}
match x {
_ if false => {}, //~ ERROR unreachable pattern
}
}
2023-05-25 11:42:12 -05:00
fn empty_foreign_enum_private(x: Option<empty::SecretlyUninhabitedForeignStruct>) {
2023-05-25 13:17:02 -05:00
let None = x;
//~^ ERROR refutable pattern in local binding
//~| NOTE `let` bindings require an "irrefutable pattern"
//~| NOTE for more information, visit
//~| NOTE the matched value is of type
//~| NOTE pattern `Some(_)` not covered
//[exhaustive_patterns]~| NOTE currently uninhabited, but this variant contains private fields
2023-05-25 11:42:12 -05:00
}
2020-11-12 11:24:42 -06:00
fn never(x: !) {
match x {} // ok
2019-11-30 09:51:26 -06:00
match x {
_ => {}, //~ ERROR unreachable pattern
}
2019-12-03 09:57:04 -06:00
match x {
_ if false => {}, //~ ERROR unreachable pattern
}
2019-11-30 09:51:26 -06:00
}
2020-12-02 17:11:29 -06:00
macro_rules! match_no_arms {
($e:expr) => {
match $e {}
};
}
macro_rules! match_guarded_arm {
($e:expr) => {
match $e {
_ if false => {}
}
};
}
2019-11-30 09:51:26 -06:00
2020-12-02 17:11:29 -06:00
fn main() {
match_no_arms!(0u8); //~ ERROR type `u8` is non-empty
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
2020-12-02 17:11:29 -06:00
match_no_arms!(NonEmptyStruct1); //~ ERROR type `NonEmptyStruct1` is non-empty
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
2020-12-02 17:11:29 -06:00
match_no_arms!(NonEmptyStruct2(true)); //~ ERROR type `NonEmptyStruct2` is non-empty
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
2020-12-02 17:11:29 -06:00
match_no_arms!((NonEmptyUnion1 { foo: () })); //~ ERROR type `NonEmptyUnion1` is non-empty
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
2020-12-02 17:11:29 -06:00
match_no_arms!((NonEmptyUnion2 { foo: () })); //~ ERROR type `NonEmptyUnion2` is non-empty
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
match_no_arms!(NonEmptyEnum1::Foo(true)); //~ ERROR `NonEmptyEnum1::Foo(_)` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE pattern `NonEmptyEnum1::Foo(_)` not covered
//~| NOTE the matched value is of type
match_no_arms!(NonEmptyEnum2::Foo(true)); //~ ERROR `NonEmptyEnum2::Foo(_)` and `NonEmptyEnum2::Bar` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE patterns `NonEmptyEnum2::Foo(_)` and
//~| NOTE the matched value is of type
match_no_arms!(NonEmptyEnum5::V1); //~ ERROR `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`, `NonEmptyEnum5::V3` and 2 more not covered
2023-05-25 13:17:02 -05:00
//~| NOTE patterns `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`
//~| NOTE the matched value is of type
2019-12-03 09:57:04 -06:00
2020-12-02 17:11:29 -06:00
match_guarded_arm!(0u8); //~ ERROR `_` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE pattern `_` not covered
//~| NOTE in this expansion of match_guarded_arm!
2020-12-02 17:11:29 -06:00
match_guarded_arm!(NonEmptyStruct1); //~ ERROR `NonEmptyStruct1` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE pattern `NonEmptyStruct1` not covered
//~| NOTE the matched value is of type
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE in this expansion of match_guarded_arm!
2020-12-02 17:11:29 -06:00
match_guarded_arm!(NonEmptyStruct2(true)); //~ ERROR `NonEmptyStruct2(_)` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
//~| NOTE pattern `NonEmptyStruct2(_)` not covered
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE in this expansion of match_guarded_arm!
2020-12-02 17:11:29 -06:00
match_guarded_arm!((NonEmptyUnion1 { foo: () })); //~ ERROR `NonEmptyUnion1 { .. }` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
//~| NOTE pattern `NonEmptyUnion1 { .. }` not covered
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE in this expansion of match_guarded_arm!
2020-12-02 17:11:29 -06:00
match_guarded_arm!((NonEmptyUnion2 { foo: () })); //~ ERROR `NonEmptyUnion2 { .. }` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
//~| NOTE pattern `NonEmptyUnion2 { .. }` not covered
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE in this expansion of match_guarded_arm!
match_guarded_arm!(NonEmptyEnum1::Foo(true)); //~ ERROR `NonEmptyEnum1::Foo(_)` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
//~| NOTE pattern `NonEmptyEnum1::Foo(_)` not covered
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE in this expansion of match_guarded_arm!
match_guarded_arm!(NonEmptyEnum2::Foo(true)); //~ ERROR `NonEmptyEnum2::Foo(_)` and `NonEmptyEnum2::Bar` not covered
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
//~| NOTE patterns `NonEmptyEnum2::Foo(_)` and
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE in this expansion of match_guarded_arm!
match_guarded_arm!(NonEmptyEnum5::V1); //~ ERROR `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`, `NonEmptyEnum5::V3` and 2 more not covered
2023-05-25 13:17:02 -05:00
//~| NOTE the matched value is of type
//~| NOTE patterns `NonEmptyEnum5::V1`,
//~| NOTE match arms with guards don't count towards exhaustivity
2023-05-25 13:17:02 -05:00
//~| NOTE in this expansion of match_guarded_arm!
2019-11-30 09:51:26 -06:00
}