]> git.lizzy.rs Git - rust.git/blob - src/test/ui/pattern/usefulness/empty-match.rs
:arrow_up: rust-analyzer
[rust.git] / src / test / ui / pattern / usefulness / empty-match.rs
1 // aux-build:empty.rs
2 // revisions: normal exhaustive_patterns
3 //
4 // This tests a match with no arms on various types.
5 #![feature(never_type)]
6 #![feature(never_type_fallback)]
7 #![cfg_attr(exhaustive_patterns, feature(exhaustive_patterns))]
8 #![deny(unreachable_patterns)]
9
10 extern crate empty;
11
12 enum EmptyEnum {}
13
14 struct NonEmptyStruct1;
15 struct NonEmptyStruct2(bool);
16 union NonEmptyUnion1 {
17     foo: (),
18 }
19 union NonEmptyUnion2 {
20     foo: (),
21     bar: (),
22 }
23 enum NonEmptyEnum1 {
24     Foo(bool),
25 }
26 enum NonEmptyEnum2 {
27     Foo(bool),
28     Bar,
29 }
30 enum NonEmptyEnum5 {
31     V1, V2, V3, V4, V5,
32 }
33
34 fn empty_enum(x: EmptyEnum) {
35     match x {} // ok
36     match x {
37         _ => {}, //~ ERROR unreachable pattern
38     }
39     match x {
40         _ if false => {}, //~ ERROR unreachable pattern
41     }
42 }
43
44 fn empty_foreign_enum(x: empty::EmptyForeignEnum) {
45     match x {} // ok
46     match x {
47         _ => {}, //~ ERROR unreachable pattern
48     }
49     match x {
50         _ if false => {}, //~ ERROR unreachable pattern
51     }
52 }
53
54 fn never(x: !) {
55     match x {} // ok
56     match x {
57         _ => {}, //~ ERROR unreachable pattern
58     }
59     match x {
60         _ if false => {}, //~ ERROR unreachable pattern
61     }
62 }
63
64 macro_rules! match_no_arms {
65     ($e:expr) => {
66         match $e {}
67     };
68 }
69 macro_rules! match_guarded_arm {
70     ($e:expr) => {
71         match $e {
72             _ if false => {}
73         }
74     };
75 }
76
77 fn main() {
78     match_no_arms!(0u8); //~ ERROR type `u8` is non-empty
79     match_no_arms!(NonEmptyStruct1); //~ ERROR type `NonEmptyStruct1` is non-empty
80     match_no_arms!(NonEmptyStruct2(true)); //~ ERROR type `NonEmptyStruct2` is non-empty
81     match_no_arms!((NonEmptyUnion1 { foo: () })); //~ ERROR type `NonEmptyUnion1` is non-empty
82     match_no_arms!((NonEmptyUnion2 { foo: () })); //~ ERROR type `NonEmptyUnion2` is non-empty
83     match_no_arms!(NonEmptyEnum1::Foo(true)); //~ ERROR `Foo(_)` not covered
84     match_no_arms!(NonEmptyEnum2::Foo(true)); //~ ERROR `Foo(_)` and `Bar` not covered
85     match_no_arms!(NonEmptyEnum5::V1); //~ ERROR `V1`, `V2`, `V3` and 2 more not covered
86
87     match_guarded_arm!(0u8); //~ ERROR `_` not covered
88     match_guarded_arm!(NonEmptyStruct1); //~ ERROR `NonEmptyStruct1` not covered
89     match_guarded_arm!(NonEmptyStruct2(true)); //~ ERROR `NonEmptyStruct2(_)` not covered
90     match_guarded_arm!((NonEmptyUnion1 { foo: () })); //~ ERROR `NonEmptyUnion1 { .. }` not covered
91     match_guarded_arm!((NonEmptyUnion2 { foo: () })); //~ ERROR `NonEmptyUnion2 { .. }` not covered
92     match_guarded_arm!(NonEmptyEnum1::Foo(true)); //~ ERROR `Foo(_)` not covered
93     match_guarded_arm!(NonEmptyEnum2::Foo(true)); //~ ERROR `Foo(_)` and `Bar` not covered
94     match_guarded_arm!(NonEmptyEnum5::V1); //~ ERROR `V1`, `V2`, `V3` and 2 more not covered
95 }