]> git.lizzy.rs Git - rust.git/blob - src/test/ui/pattern/usefulness/match-empty.rs
Rollup merge of #80298 - PankajChaudhary5:PankajChaudhary, r=GuillaumeGomez
[rust.git] / src / test / ui / pattern / usefulness / match-empty.rs
1 // aux-build:empty.rs
2 #![feature(never_type)]
3 #![feature(never_type_fallback)]
4 #![deny(unreachable_patterns)]
5
6 extern crate empty;
7
8 enum EmptyEnum {}
9
10 struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here
11 union NonEmptyUnion1 { //~ `NonEmptyUnion1` defined here
12     foo: (),
13 }
14 union NonEmptyUnion2 { //~ `NonEmptyUnion2` defined here
15     foo: (),
16     bar: (),
17 }
18 enum NonEmptyEnum1 { //~ `NonEmptyEnum1` defined here
19     Foo(bool),
20     //~^ not covered
21     //~| not covered
22 }
23 enum NonEmptyEnum2 { //~ `NonEmptyEnum2` defined here
24     Foo(bool),
25     //~^ not covered
26     //~| not covered
27     Bar,
28     //~^ not covered
29     //~| not covered
30 }
31 enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here
32     V1, V2, V3, V4, V5,
33 }
34
35 macro_rules! match_empty {
36     ($e:expr) => {
37         match $e {}
38     };
39 }
40 macro_rules! match_false {
41     ($e:expr) => {
42         match $e {
43             _ if false => {}
44         }
45     };
46 }
47
48 fn empty_enum(x: EmptyEnum) {
49     match x {} // ok
50     match x {
51         _ => {}, //~ ERROR unreachable pattern
52     }
53     match x {
54         _ if false => {}, //~ ERROR unreachable pattern
55     }
56 }
57
58 fn empty_foreign_enum(x: empty::EmptyForeignEnum) {
59     match x {} // ok
60     match x {
61         _ => {}, //~ ERROR unreachable pattern
62     }
63     match x {
64         _ if false => {}, //~ ERROR unreachable pattern
65     }
66 }
67
68 fn never(x: !) {
69     match x {} // ok
70     match x {
71         _ => {}, //~ ERROR unreachable pattern
72     }
73     match x {
74         _ if false => {}, //~ ERROR unreachable pattern
75     }
76 }
77
78 fn main() {
79     // `exhaustive_patterns` is not on, so uninhabited branches are not detected as unreachable.
80     match None::<!> {
81         None => {}
82         Some(_) => {}
83     }
84     match None::<EmptyEnum> {
85         None => {}
86         Some(_) => {}
87     }
88
89     match_empty!(0u8);
90     //~^ ERROR type `u8` is non-empty
91     match_empty!(NonEmptyStruct(true));
92     //~^ ERROR type `NonEmptyStruct` is non-empty
93     match_empty!((NonEmptyUnion1 { foo: () }));
94     //~^ ERROR type `NonEmptyUnion1` is non-empty
95     match_empty!((NonEmptyUnion2 { foo: () }));
96     //~^ ERROR type `NonEmptyUnion2` is non-empty
97     match_empty!(NonEmptyEnum1::Foo(true));
98     //~^ ERROR `Foo(_)` not covered
99     match_empty!(NonEmptyEnum2::Foo(true));
100     //~^ ERROR `Foo(_)` and `Bar` not covered
101     match_empty!(NonEmptyEnum5::V1);
102     //~^ ERROR `V1`, `V2`, `V3` and 2 more not covered
103
104     match_false!(0u8);
105     //~^ ERROR `_` not covered
106     match_false!(NonEmptyStruct(true));
107     //~^ ERROR `NonEmptyStruct(_)` not covered
108     match_false!((NonEmptyUnion1 { foo: () }));
109     //~^ ERROR `NonEmptyUnion1 { .. }` not covered
110     match_false!((NonEmptyUnion2 { foo: () }));
111     //~^ ERROR `NonEmptyUnion2 { .. }` not covered
112     match_false!(NonEmptyEnum1::Foo(true));
113     //~^ ERROR `Foo(_)` not covered
114     match_false!(NonEmptyEnum2::Foo(true));
115     //~^ ERROR `Foo(_)` and `Bar` not covered
116     match_false!(NonEmptyEnum5::V1);
117     //~^ ERROR `V1`, `V2`, `V3` and 2 more not covered
118 }