]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc-2008-non-exhaustive/omitted-patterns.rs
parser will not give wrong help message for 'public'
[rust.git] / src / test / ui / rfc-2008-non-exhaustive / omitted-patterns.rs
1 // Test that the `non_exhaustive_omitted_patterns` lint is triggered correctly.
2
3 #![feature(non_exhaustive_omitted_patterns_lint, unstable_test_feature)]
4
5 // aux-build:enums.rs
6 extern crate enums;
7 // aux-build:unstable.rs
8 extern crate unstable;
9 // aux-build:structs.rs
10 extern crate structs;
11
12 use enums::{
13     EmptyNonExhaustiveEnum, NestedNonExhaustive, NonExhaustiveEnum, NonExhaustiveSingleVariant,
14     VariantNonExhaustive,
15 };
16 use unstable::{UnstableEnum, OnlyUnstableEnum, UnstableStruct, OnlyUnstableStruct};
17 use structs::{FunctionalRecord, MixedVisFields, NestedStruct, NormalStruct};
18
19 #[non_exhaustive]
20 #[derive(Default)]
21 pub struct Foo {
22     a: u8,
23     b: usize,
24     c: String,
25 }
26
27 #[non_exhaustive]
28 pub enum Bar {
29     A,
30     B,
31     C,
32 }
33
34 fn main() {
35     let enumeration = Bar::A;
36
37     // Ok: this is a crate local non_exhaustive enum
38     match enumeration {
39         Bar::A => {}
40         Bar::B => {}
41         #[deny(non_exhaustive_omitted_patterns)]
42         _ => {}
43     }
44
45     let non_enum = NonExhaustiveEnum::Unit;
46
47     // Ok: without the attribute
48     match non_enum {
49         NonExhaustiveEnum::Unit => {}
50         NonExhaustiveEnum::Tuple(_) => {}
51         _ => {}
52     }
53
54     match non_enum {
55         NonExhaustiveEnum::Unit => {}
56         NonExhaustiveEnum::Tuple(_) => {}
57         #[deny(non_exhaustive_omitted_patterns)]
58         _ => {}
59     }
60     //~^^ some variants are not matched explicitly
61
62     match non_enum {
63         NonExhaustiveEnum::Unit | NonExhaustiveEnum::Struct { .. } => {}
64         #[deny(non_exhaustive_omitted_patterns)]
65         _ => {}
66     }
67     //~^^ some variants are not matched explicitly
68
69     let x = 5;
70     match non_enum {
71         NonExhaustiveEnum::Unit if x > 10 => {}
72         NonExhaustiveEnum::Tuple(_) => {}
73         NonExhaustiveEnum::Struct { .. } => {}
74         #[deny(non_exhaustive_omitted_patterns)]
75         _ => {}
76     }
77     //~^^ some variants are not matched explicitly
78
79     // Ok: all covered and not `unreachable-patterns`
80     #[deny(unreachable_patterns)]
81     match non_enum {
82         NonExhaustiveEnum::Unit => {}
83         NonExhaustiveEnum::Tuple(_) => {}
84         NonExhaustiveEnum::Struct { .. } => {}
85         #[deny(non_exhaustive_omitted_patterns)]
86         _ => {}
87     }
88
89     #[deny(non_exhaustive_omitted_patterns)]
90     match NestedNonExhaustive::B {
91         NestedNonExhaustive::A(NonExhaustiveEnum::Unit) => {}
92         NestedNonExhaustive::A(_) => {}
93         NestedNonExhaustive::B => {}
94         _ => {}
95     }
96     //~^^ some variants are not matched explicitly
97     //~^^^^^ some variants are not matched explicitly
98
99     #[warn(non_exhaustive_omitted_patterns)]
100     match VariantNonExhaustive::Baz(1, 2) {
101         VariantNonExhaustive::Baz(_, _) => {}
102         VariantNonExhaustive::Bar { x, .. } => {}
103     }
104     //~^^ some fields are not explicitly listed
105
106     #[warn(non_exhaustive_omitted_patterns)]
107     let FunctionalRecord { first_field, second_field, .. } = FunctionalRecord::default();
108     //~^ some fields are not explicitly listed
109
110     // Ok: this is local
111     #[warn(non_exhaustive_omitted_patterns)]
112     let Foo { a, b, .. } = Foo::default();
113
114     #[warn(non_exhaustive_omitted_patterns)]
115     let NestedStruct { bar: NormalStruct { first_field, .. }, .. } = NestedStruct::default();
116     //~^ some fields are not explicitly listed
117     //~^^ some fields are not explicitly listed
118
119     // Ok: this tests https://github.com/rust-lang/rust/issues/89382
120     #[warn(non_exhaustive_omitted_patterns)]
121     let MixedVisFields { a, b, .. } = MixedVisFields::default();
122
123     // Ok: because this only has 1 variant
124     #[deny(non_exhaustive_omitted_patterns)]
125     match NonExhaustiveSingleVariant::A(true) {
126         NonExhaustiveSingleVariant::A(true) => {}
127         _ => {}
128     }
129
130     #[deny(non_exhaustive_omitted_patterns)]
131     match NonExhaustiveSingleVariant::A(true) {
132         _ => {}
133     }
134     //~^^ some variants are not matched explicitly
135
136     // Ok: we don't lint on `if let` expressions
137     #[deny(non_exhaustive_omitted_patterns)]
138     if let NonExhaustiveEnum::Tuple(_) = non_enum {}
139
140     match UnstableEnum::Stable {
141         UnstableEnum::Stable => {}
142         UnstableEnum::Stable2 => {}
143         #[deny(non_exhaustive_omitted_patterns)]
144         _ => {}
145     }
146     //~^^ some variants are not matched explicitly
147
148     // Ok: the feature is on and all variants are matched
149     #[deny(non_exhaustive_omitted_patterns)]
150     match UnstableEnum::Stable {
151         UnstableEnum::Stable => {}
152         UnstableEnum::Stable2 => {}
153         UnstableEnum::Unstable => {}
154         _ => {}
155     }
156
157     // Ok: the feature is on and both variants are matched
158     #[deny(non_exhaustive_omitted_patterns)]
159     match OnlyUnstableEnum::Unstable {
160         OnlyUnstableEnum::Unstable => {}
161         OnlyUnstableEnum::Unstable2 => {}
162         _ => {}
163     }
164
165     #[deny(non_exhaustive_omitted_patterns)]
166     match OnlyUnstableEnum::Unstable {
167         OnlyUnstableEnum::Unstable => {}
168         _ => {}
169     }
170     //~^^ some variants are not matched explicitly
171
172     #[warn(non_exhaustive_omitted_patterns)]
173     let OnlyUnstableStruct { unstable, .. } = OnlyUnstableStruct::new();
174     //~^ some fields are not explicitly listed
175
176     // OK: both unstable fields are matched with feature on
177     #[warn(non_exhaustive_omitted_patterns)]
178     let OnlyUnstableStruct { unstable, unstable2, .. } = OnlyUnstableStruct::new();
179
180     #[warn(non_exhaustive_omitted_patterns)]
181     let UnstableStruct { stable, stable2, .. } = UnstableStruct::default();
182     //~^ some fields are not explicitly listed
183
184     // OK: both unstable and stable fields are matched with feature on
185     #[warn(non_exhaustive_omitted_patterns)]
186     let UnstableStruct { stable, stable2, unstable, .. } = UnstableStruct::default();
187 }