summaryrefslogtreecommitdiffstats
path: root/tests/ui/pattern/usefulness/empty-match.rs
blob: d56d2e3c817ce7016994e3ec0991eb42f548ec90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// aux-build:empty.rs
// revisions: normal exhaustive_patterns
//
// This tests a match with no arms on various types.
#![feature(never_type)]
#![feature(never_type_fallback)]
#![cfg_attr(exhaustive_patterns, feature(exhaustive_patterns))]
#![deny(unreachable_patterns)]
//~^ NOTE the lint level is defined here

extern crate empty;

enum EmptyEnum {}

struct NonEmptyStruct1;
//~^ NOTE `NonEmptyStruct1` defined here
//~| NOTE `NonEmptyStruct1` defined here
struct NonEmptyStruct2(bool);
//~^ NOTE `NonEmptyStruct2` defined here
//~| NOTE `NonEmptyStruct2` defined here
union NonEmptyUnion1 {
    //~^ NOTE `NonEmptyUnion1` defined here
    //~| NOTE `NonEmptyUnion1` defined here
    foo: (),
}
union NonEmptyUnion2 {
    //~^ NOTE `NonEmptyUnion2` defined here
    //~| NOTE `NonEmptyUnion2` defined here
    foo: (),
    bar: (),
}
enum NonEmptyEnum1 {
    Foo(bool),
    //~^ NOTE `NonEmptyEnum1` defined here
    //~| NOTE `NonEmptyEnum1` defined here
    //~| NOTE not covered
    //~| NOTE not covered
}
enum NonEmptyEnum2 {
    Foo(bool),
    //~^ NOTE `NonEmptyEnum2` defined here
    //~| NOTE `NonEmptyEnum2` defined here
    //~| NOTE not covered
    //~| NOTE not covered
    Bar,
    //~^ NOTE not covered
    //~| NOTE not covered
}
enum NonEmptyEnum5 {
    //~^ NOTE `NonEmptyEnum5` defined here
    //~| NOTE `NonEmptyEnum5` defined here
    V1, V2, V3, V4, V5,
}

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) {
    match x {} // ok
    match x {
        _ => {}, //~ ERROR unreachable pattern
    }
    match x {
        _ if false => {}, //~ ERROR unreachable pattern
    }
}

fn empty_foreign_enum_private(x: Option<empty::SecretlyUninhabitedForeignStruct>) {
    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
}

fn never(x: !) {
    match x {} // ok
    match x {
        _ => {}, //~ ERROR unreachable pattern
    }
    match x {
        _ if false => {}, //~ ERROR unreachable pattern
    }
}

macro_rules! match_no_arms {
    ($e:expr) => {
        match $e {}
    };
}
macro_rules! match_guarded_arm {
    ($e:expr) => {
        match $e {
            _ if false => {}
        }
    };
}

fn main() {
    match_no_arms!(0u8); //~ ERROR type `u8` is non-empty
                         //~| NOTE the matched value is of type
    match_no_arms!(NonEmptyStruct1); //~ ERROR type `NonEmptyStruct1` is non-empty
                                     //~| NOTE the matched value is of type
    match_no_arms!(NonEmptyStruct2(true)); //~ ERROR type `NonEmptyStruct2` is non-empty
                                           //~| NOTE the matched value is of type
    match_no_arms!((NonEmptyUnion1 { foo: () })); //~ ERROR type `NonEmptyUnion1` is non-empty
                                                  //~| NOTE the matched value is of type
    match_no_arms!((NonEmptyUnion2 { foo: () })); //~ ERROR type `NonEmptyUnion2` is non-empty
                                                  //~| NOTE the matched value is of type
    match_no_arms!(NonEmptyEnum1::Foo(true)); //~ ERROR `NonEmptyEnum1::Foo(_)` not covered
                                              //~| 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
                                              //~| 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
                                       //~| NOTE patterns `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`
                                       //~| NOTE the matched value is of type

    match_guarded_arm!(0u8); //~ ERROR `_` not covered
                             //~| NOTE the matched value is of type
                             //~| NOTE match arms with guards don't count towards exhaustivity
                             //~| NOTE pattern `_` not covered
                             //~| NOTE in this expansion of match_guarded_arm!
    match_guarded_arm!(NonEmptyStruct1); //~ ERROR `NonEmptyStruct1` not covered
                                         //~| NOTE pattern `NonEmptyStruct1` not covered
                                         //~| NOTE the matched value is of type
                                         //~| NOTE match arms with guards don't count towards exhaustivity
                                         //~| NOTE in this expansion of match_guarded_arm!
    match_guarded_arm!(NonEmptyStruct2(true)); //~ ERROR `NonEmptyStruct2(_)` not covered
                                               //~| NOTE the matched value is of type
                                               //~| NOTE pattern `NonEmptyStruct2(_)` not covered
                                               //~| NOTE match arms with guards don't count towards exhaustivity
                                               //~| NOTE in this expansion of match_guarded_arm!
    match_guarded_arm!((NonEmptyUnion1 { foo: () })); //~ ERROR `NonEmptyUnion1 { .. }` not covered
                                                      //~| NOTE the matched value is of type
                                                      //~| NOTE pattern `NonEmptyUnion1 { .. }` not covered
                                                      //~| NOTE match arms with guards don't count towards exhaustivity
                                                      //~| NOTE in this expansion of match_guarded_arm!
    match_guarded_arm!((NonEmptyUnion2 { foo: () })); //~ ERROR `NonEmptyUnion2 { .. }` not covered
                                                      //~| NOTE the matched value is of type
                                                      //~| NOTE pattern `NonEmptyUnion2 { .. }` not covered
                                                      //~| NOTE match arms with guards don't count towards exhaustivity
                                                      //~| NOTE in this expansion of match_guarded_arm!
    match_guarded_arm!(NonEmptyEnum1::Foo(true)); //~ ERROR `NonEmptyEnum1::Foo(_)` not covered
                                                  //~| NOTE the matched value is of type
                                                  //~| NOTE pattern `NonEmptyEnum1::Foo(_)` not covered
                                                  //~| NOTE match arms with guards don't count towards exhaustivity
                                                  //~| NOTE in this expansion of match_guarded_arm!
    match_guarded_arm!(NonEmptyEnum2::Foo(true)); //~ ERROR `NonEmptyEnum2::Foo(_)` and `NonEmptyEnum2::Bar` not covered
                                                  //~| NOTE the matched value is of type
                                                  //~| NOTE patterns `NonEmptyEnum2::Foo(_)` and
                                                  //~| NOTE match arms with guards don't count towards exhaustivity
                                                  //~| 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
                                           //~| NOTE the matched value is of type
                                           //~| NOTE patterns `NonEmptyEnum5::V1`,
                                           //~| NOTE match arms with guards don't count towards exhaustivity
                                           //~| NOTE in this expansion of match_guarded_arm!
}