1 // Test that the `non_exhaustive_omitted_patterns` lint is triggered correctly.
3 #![feature(non_exhaustive_omitted_patterns_lint, unstable_test_feature)]
7 // aux-build:unstable.rs
9 // aux-build:structs.rs
13 EmptyNonExhaustiveEnum, NestedNonExhaustive, NonExhaustiveEnum, NonExhaustiveSingleVariant,
16 use unstable::{UnstableEnum, OnlyUnstableEnum};
17 use structs::{FunctionalRecord, MixedVisFields, NestedStruct, NormalStruct};
35 let enumeration = Bar::A;
37 // Ok: this is a crate local non_exhaustive enum
41 #[deny(non_exhaustive_omitted_patterns)]
45 let non_enum = NonExhaustiveEnum::Unit;
47 // Ok: without the attribute
49 NonExhaustiveEnum::Unit => {}
50 NonExhaustiveEnum::Tuple(_) => {}
55 NonExhaustiveEnum::Unit => {}
56 NonExhaustiveEnum::Tuple(_) => {}
57 #[deny(non_exhaustive_omitted_patterns)]
60 //~^^ some variants are not matched explicitly
63 NonExhaustiveEnum::Unit | NonExhaustiveEnum::Struct { .. } => {}
64 #[deny(non_exhaustive_omitted_patterns)]
67 //~^^ some variants are not matched explicitly
71 NonExhaustiveEnum::Unit if x > 10 => {}
72 NonExhaustiveEnum::Tuple(_) => {}
73 NonExhaustiveEnum::Struct { .. } => {}
74 #[deny(non_exhaustive_omitted_patterns)]
77 //~^^ some variants are not matched explicitly
79 // Ok: all covered and not `unreachable-patterns`
80 #[deny(unreachable_patterns)]
82 NonExhaustiveEnum::Unit => {}
83 NonExhaustiveEnum::Tuple(_) => {}
84 NonExhaustiveEnum::Struct { .. } => {}
85 #[deny(non_exhaustive_omitted_patterns)]
89 #[deny(non_exhaustive_omitted_patterns)]
90 match NestedNonExhaustive::B {
91 NestedNonExhaustive::A(NonExhaustiveEnum::Unit) => {}
92 NestedNonExhaustive::A(_) => {}
93 NestedNonExhaustive::B => {}
96 //~^^ some variants are not matched explicitly
97 //~^^^^^ some variants are not matched explicitly
99 #[warn(non_exhaustive_omitted_patterns)]
100 match VariantNonExhaustive::Baz(1, 2) {
101 VariantNonExhaustive::Baz(_, _) => {}
102 VariantNonExhaustive::Bar { x, .. } => {}
104 //~^^ some fields are not explicitly listed
106 #[warn(non_exhaustive_omitted_patterns)]
107 let FunctionalRecord { first_field, second_field, .. } = FunctionalRecord::default();
108 //~^ some fields are not explicitly listed
111 #[warn(non_exhaustive_omitted_patterns)]
112 let Foo { a, b, .. } = Foo::default();
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
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();
123 // Ok: because this only has 1 variant
124 #[deny(non_exhaustive_omitted_patterns)]
125 match NonExhaustiveSingleVariant::A(true) {
126 NonExhaustiveSingleVariant::A(true) => {}
130 #[deny(non_exhaustive_omitted_patterns)]
131 match NonExhaustiveSingleVariant::A(true) {
134 //~^^ some variants are not matched explicitly
136 // Ok: we don't lint on `if let` expressions
137 #[deny(non_exhaustive_omitted_patterns)]
138 if let NonExhaustiveEnum::Tuple(_) = non_enum {}
140 match UnstableEnum::Stable {
141 UnstableEnum::Stable => {}
142 UnstableEnum::Stable2 => {}
143 #[deny(non_exhaustive_omitted_patterns)]
146 //~^^ some variants are not matched explicitly
148 #[deny(non_exhaustive_omitted_patterns)]
149 match UnstableEnum::Stable {
150 UnstableEnum::Stable => {}
151 UnstableEnum::Stable2 => {}
152 UnstableEnum::Unstable => {}
156 // Ok: the feature is on and both variants are matched
157 #[deny(non_exhaustive_omitted_patterns)]
158 match OnlyUnstableEnum::Unstable {
159 OnlyUnstableEnum::Unstable => {}
160 OnlyUnstableEnum::Unstable2 => {}
164 #[deny(non_exhaustive_omitted_patterns)]
165 match OnlyUnstableEnum::Unstable {
166 OnlyUnstableEnum::Unstable => {}
169 //~^^ some variants are not matched explicitly