1
1
// Test that the `non_exhaustive_omitted_patterns` lint is triggered correctly.
2
2
3
3
#![ feature( non_exhaustive_omitted_patterns_lint, unstable_test_feature) ]
4
+ #![ deny( unreachable_patterns) ]
4
5
5
6
// aux-build:enums.rs
6
7
extern crate enums;
@@ -31,37 +32,38 @@ pub enum Bar {
31
32
C ,
32
33
}
33
34
35
+ fn no_lint ( ) {
36
+ let non_enum = NonExhaustiveEnum :: Unit ;
37
+ // Ok: without the attribute
38
+ match non_enum {
39
+ NonExhaustiveEnum :: Unit => { }
40
+ NonExhaustiveEnum :: Tuple ( _) => { }
41
+ _ => { }
42
+ }
43
+ }
44
+
45
+ #[ warn( non_exhaustive_omitted_patterns) ]
34
46
fn main ( ) {
35
47
let enumeration = Bar :: A ;
36
48
37
49
// Ok: this is a crate local non_exhaustive enum
38
50
match enumeration {
39
51
Bar :: A => { }
40
52
Bar :: B => { }
41
- #[ deny( non_exhaustive_omitted_patterns) ]
42
53
_ => { }
43
54
}
44
55
45
56
let non_enum = NonExhaustiveEnum :: Unit ;
46
57
47
- // Ok: without the attribute
48
- match non_enum {
49
- NonExhaustiveEnum :: Unit => { }
50
- NonExhaustiveEnum :: Tuple ( _) => { }
51
- _ => { }
52
- }
53
-
54
58
match non_enum {
55
59
NonExhaustiveEnum :: Unit => { }
56
60
NonExhaustiveEnum :: Tuple ( _) => { }
57
- #[ deny( non_exhaustive_omitted_patterns) ]
58
61
_ => { }
59
62
}
60
63
//~^^ some variants are not matched explicitly
61
64
62
65
match non_enum {
63
66
NonExhaustiveEnum :: Unit | NonExhaustiveEnum :: Struct { .. } => { }
64
- #[ deny( non_exhaustive_omitted_patterns) ]
65
67
_ => { }
66
68
}
67
69
//~^^ some variants are not matched explicitly
@@ -71,22 +73,18 @@ fn main() {
71
73
NonExhaustiveEnum :: Unit if x > 10 => { }
72
74
NonExhaustiveEnum :: Tuple ( _) => { }
73
75
NonExhaustiveEnum :: Struct { .. } => { }
74
- #[ deny( non_exhaustive_omitted_patterns) ]
75
76
_ => { }
76
77
}
77
78
//~^^ some variants are not matched explicitly
78
79
79
80
// Ok: all covered and not `unreachable-patterns`
80
- #[ deny( unreachable_patterns) ]
81
81
match non_enum {
82
82
NonExhaustiveEnum :: Unit => { }
83
83
NonExhaustiveEnum :: Tuple ( _) => { }
84
84
NonExhaustiveEnum :: Struct { .. } => { }
85
- #[ deny( non_exhaustive_omitted_patterns) ]
86
85
_ => { }
87
86
}
88
87
89
- #[ deny( non_exhaustive_omitted_patterns) ]
90
88
match NestedNonExhaustive :: B {
91
89
NestedNonExhaustive :: A ( NonExhaustiveEnum :: Unit ) => { }
92
90
NestedNonExhaustive :: A ( _) => { }
@@ -96,68 +94,56 @@ fn main() {
96
94
//~^^ some variants are not matched explicitly
97
95
//~^^^^^ some variants are not matched explicitly
98
96
99
- #[ warn( non_exhaustive_omitted_patterns) ]
100
97
match VariantNonExhaustive :: Baz ( 1 , 2 ) {
101
98
VariantNonExhaustive :: Baz ( _, _) => { }
102
99
VariantNonExhaustive :: Bar { x, .. } => { }
103
100
}
104
101
//~^^ some fields are not explicitly listed
105
102
106
- #[ warn( non_exhaustive_omitted_patterns) ]
107
103
let FunctionalRecord { first_field, second_field, .. } = FunctionalRecord :: default ( ) ;
108
104
//~^ some fields are not explicitly listed
109
105
110
106
// Ok: this is local
111
- #[ warn( non_exhaustive_omitted_patterns) ]
112
107
let Foo { a, b, .. } = Foo :: default ( ) ;
113
108
114
- #[ warn( non_exhaustive_omitted_patterns) ]
115
109
let NestedStruct { bar : NormalStruct { first_field, .. } , .. } = NestedStruct :: default ( ) ;
116
110
//~^ some fields are not explicitly listed
117
111
//~^^ some fields are not explicitly listed
118
112
119
113
// Ok: this tests https://github.com/rust-lang/rust/issues/89382
120
- #[ warn( non_exhaustive_omitted_patterns) ]
121
114
let MixedVisFields { a, b, .. } = MixedVisFields :: default ( ) ;
122
115
123
116
// Ok: because this only has 1 variant
124
- #[ deny( non_exhaustive_omitted_patterns) ]
125
117
match NonExhaustiveSingleVariant :: A ( true ) {
126
118
NonExhaustiveSingleVariant :: A ( true ) => { }
127
119
_ => { }
128
120
}
129
121
130
122
// No variants are mentioned
131
- #[ deny( non_exhaustive_omitted_patterns) ]
132
123
match NonExhaustiveSingleVariant :: A ( true ) {
133
124
_ => { }
134
125
}
135
126
//~^^ some variants are not matched explicitly
136
- #[ deny( non_exhaustive_omitted_patterns) ]
137
127
match Some ( NonExhaustiveSingleVariant :: A ( true ) ) {
138
128
Some ( _) => { }
139
129
None => { }
140
130
}
141
- #[ deny( non_exhaustive_omitted_patterns) ]
142
131
match Some ( & NonExhaustiveSingleVariant :: A ( true ) ) {
143
132
Some ( _) => { }
144
133
None => { }
145
134
}
146
135
147
136
// Ok: we don't lint on `if let` expressions
148
- #[ deny( non_exhaustive_omitted_patterns) ]
149
137
if let NonExhaustiveEnum :: Tuple ( _) = non_enum { }
150
138
151
139
match UnstableEnum :: Stable {
152
140
UnstableEnum :: Stable => { }
153
141
UnstableEnum :: Stable2 => { }
154
- #[ deny( non_exhaustive_omitted_patterns) ]
155
142
_ => { }
156
143
}
157
144
//~^^ some variants are not matched explicitly
158
145
159
146
// Ok: the feature is on and all variants are matched
160
- #[ deny( non_exhaustive_omitted_patterns) ]
161
147
match UnstableEnum :: Stable {
162
148
UnstableEnum :: Stable => { }
163
149
UnstableEnum :: Stable2 => { }
@@ -166,68 +152,56 @@ fn main() {
166
152
}
167
153
168
154
// Ok: the feature is on and both variants are matched
169
- #[ deny( non_exhaustive_omitted_patterns) ]
170
155
match OnlyUnstableEnum :: Unstable {
171
156
OnlyUnstableEnum :: Unstable => { }
172
157
OnlyUnstableEnum :: Unstable2 => { }
173
158
_ => { }
174
159
}
175
160
176
- #[ deny( non_exhaustive_omitted_patterns) ]
177
161
match OnlyUnstableEnum :: Unstable {
178
162
OnlyUnstableEnum :: Unstable => { }
179
163
_ => { }
180
164
}
181
165
//~^^ some variants are not matched explicitly
182
166
183
- #[ warn( non_exhaustive_omitted_patterns) ]
184
167
let OnlyUnstableStruct { unstable, .. } = OnlyUnstableStruct :: new ( ) ;
185
168
//~^ some fields are not explicitly listed
186
169
187
170
// OK: both unstable fields are matched with feature on
188
- #[ warn( non_exhaustive_omitted_patterns) ]
189
171
let OnlyUnstableStruct { unstable, unstable2, .. } = OnlyUnstableStruct :: new ( ) ;
190
172
191
- #[ warn( non_exhaustive_omitted_patterns) ]
192
173
let UnstableStruct { stable, stable2, .. } = UnstableStruct :: default ( ) ;
193
174
//~^ some fields are not explicitly listed
194
175
195
176
// OK: both unstable and stable fields are matched with feature on
196
- #[ warn( non_exhaustive_omitted_patterns) ]
197
177
let UnstableStruct { stable, stable2, unstable, .. } = UnstableStruct :: default ( ) ;
198
178
199
179
// Ok: local bindings are allowed
200
- #[ deny( non_exhaustive_omitted_patterns) ]
201
180
let local = NonExhaustiveEnum :: Unit ;
202
181
203
182
// Ok: missing patterns will be blocked by the pattern being refutable
204
- #[ deny( non_exhaustive_omitted_patterns) ]
205
183
let local_refutable @ NonExhaustiveEnum :: Unit = NonExhaustiveEnum :: Unit ;
206
184
//~^ refutable pattern in local binding
207
185
208
186
// Check that matching on a reference results in a correctly spanned diagnostic
209
- #[ deny( non_exhaustive_omitted_patterns) ]
210
187
match & non_enum {
211
188
NonExhaustiveEnum :: Unit => { }
212
189
NonExhaustiveEnum :: Tuple ( _) => { }
213
190
_ => { }
214
191
}
215
192
//~^^ some variants are not matched explicitly
216
193
217
- #[ deny( non_exhaustive_omitted_patterns) ]
218
194
match ( true , & non_enum) {
219
195
( true , NonExhaustiveEnum :: Unit ) => { }
220
196
_ => { }
221
197
}
222
198
223
- #[ deny( non_exhaustive_omitted_patterns) ]
224
199
match ( & non_enum, true ) {
225
200
( NonExhaustiveEnum :: Unit , true ) => { }
226
201
_ => { }
227
202
}
228
203
//~^^ some variants are not matched explicitly
229
204
230
- #[ deny( non_exhaustive_omitted_patterns) ]
231
205
match Some ( & non_enum) {
232
206
Some ( NonExhaustiveEnum :: Unit | NonExhaustiveEnum :: Tuple ( _) ) => { }
233
207
_ => { }
0 commit comments