summaryrefslogtreecommitdiffstats
path: root/src/test/ui/rfc-2008-non-exhaustive/omitted-patterns.rs
blob: d8f07bb8f24d1b44f165421fc86739b09884c772 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Test that the `non_exhaustive_omitted_patterns` lint is triggered correctly.

#![feature(non_exhaustive_omitted_patterns_lint, unstable_test_feature)]

// aux-build:enums.rs
extern crate enums;
// aux-build:unstable.rs
extern crate unstable;
// aux-build:structs.rs
extern crate structs;

use enums::{
    EmptyNonExhaustiveEnum, NestedNonExhaustive, NonExhaustiveEnum, NonExhaustiveSingleVariant,
    VariantNonExhaustive,
};
use unstable::{UnstableEnum, OnlyUnstableEnum, UnstableStruct, OnlyUnstableStruct};
use structs::{FunctionalRecord, MixedVisFields, NestedStruct, NormalStruct};

#[non_exhaustive]
#[derive(Default)]
pub struct Foo {
    a: u8,
    b: usize,
    c: String,
}

#[non_exhaustive]
pub enum Bar {
    A,
    B,
    C,
}

fn main() {
    let enumeration = Bar::A;

    // Ok: this is a crate local non_exhaustive enum
    match enumeration {
        Bar::A => {}
        Bar::B => {}
        #[deny(non_exhaustive_omitted_patterns)]
        _ => {}
    }

    let non_enum = NonExhaustiveEnum::Unit;

    // Ok: without the attribute
    match non_enum {
        NonExhaustiveEnum::Unit => {}
        NonExhaustiveEnum::Tuple(_) => {}
        _ => {}
    }

    match non_enum {
        NonExhaustiveEnum::Unit => {}
        NonExhaustiveEnum::Tuple(_) => {}
        #[deny(non_exhaustive_omitted_patterns)]
        _ => {}
    }
    //~^^ some variants are not matched explicitly

    match non_enum {
        NonExhaustiveEnum::Unit | NonExhaustiveEnum::Struct { .. } => {}
        #[deny(non_exhaustive_omitted_patterns)]
        _ => {}
    }
    //~^^ some variants are not matched explicitly

    let x = 5;
    match non_enum {
        NonExhaustiveEnum::Unit if x > 10 => {}
        NonExhaustiveEnum::Tuple(_) => {}
        NonExhaustiveEnum::Struct { .. } => {}
        #[deny(non_exhaustive_omitted_patterns)]
        _ => {}
    }
    //~^^ some variants are not matched explicitly

    // Ok: all covered and not `unreachable-patterns`
    #[deny(unreachable_patterns)]
    match non_enum {
        NonExhaustiveEnum::Unit => {}
        NonExhaustiveEnum::Tuple(_) => {}
        NonExhaustiveEnum::Struct { .. } => {}
        #[deny(non_exhaustive_omitted_patterns)]
        _ => {}
    }

    #[deny(non_exhaustive_omitted_patterns)]
    match NestedNonExhaustive::B {
        NestedNonExhaustive::A(NonExhaustiveEnum::Unit) => {}
        NestedNonExhaustive::A(_) => {}
        NestedNonExhaustive::B => {}
        _ => {}
    }
    //~^^ some variants are not matched explicitly
    //~^^^^^ some variants are not matched explicitly

    #[warn(non_exhaustive_omitted_patterns)]
    match VariantNonExhaustive::Baz(1, 2) {
        VariantNonExhaustive::Baz(_, _) => {}
        VariantNonExhaustive::Bar { x, .. } => {}
    }
    //~^^ some fields are not explicitly listed

    #[warn(non_exhaustive_omitted_patterns)]
    let FunctionalRecord { first_field, second_field, .. } = FunctionalRecord::default();
    //~^ some fields are not explicitly listed

    // Ok: this is local
    #[warn(non_exhaustive_omitted_patterns)]
    let Foo { a, b, .. } = Foo::default();

    #[warn(non_exhaustive_omitted_patterns)]
    let NestedStruct { bar: NormalStruct { first_field, .. }, .. } = NestedStruct::default();
    //~^ some fields are not explicitly listed
    //~^^ some fields are not explicitly listed

    // Ok: this tests https://github.com/rust-lang/rust/issues/89382
    #[warn(non_exhaustive_omitted_patterns)]
    let MixedVisFields { a, b, .. } = MixedVisFields::default();

    // Ok: because this only has 1 variant
    #[deny(non_exhaustive_omitted_patterns)]
    match NonExhaustiveSingleVariant::A(true) {
        NonExhaustiveSingleVariant::A(true) => {}
        _ => {}
    }

    #[deny(non_exhaustive_omitted_patterns)]
    match NonExhaustiveSingleVariant::A(true) {
        _ => {}
    }
    //~^^ some variants are not matched explicitly

    // Ok: we don't lint on `if let` expressions
    #[deny(non_exhaustive_omitted_patterns)]
    if let NonExhaustiveEnum::Tuple(_) = non_enum {}

    match UnstableEnum::Stable {
        UnstableEnum::Stable => {}
        UnstableEnum::Stable2 => {}
        #[deny(non_exhaustive_omitted_patterns)]
        _ => {}
    }
    //~^^ some variants are not matched explicitly

    // Ok: the feature is on and all variants are matched
    #[deny(non_exhaustive_omitted_patterns)]
    match UnstableEnum::Stable {
        UnstableEnum::Stable => {}
        UnstableEnum::Stable2 => {}
        UnstableEnum::Unstable => {}
        _ => {}
    }

    // Ok: the feature is on and both variants are matched
    #[deny(non_exhaustive_omitted_patterns)]
    match OnlyUnstableEnum::Unstable {
        OnlyUnstableEnum::Unstable => {}
        OnlyUnstableEnum::Unstable2 => {}
        _ => {}
    }

    #[deny(non_exhaustive_omitted_patterns)]
    match OnlyUnstableEnum::Unstable {
        OnlyUnstableEnum::Unstable => {}
        _ => {}
    }
    //~^^ some variants are not matched explicitly

    #[warn(non_exhaustive_omitted_patterns)]
    let OnlyUnstableStruct { unstable, .. } = OnlyUnstableStruct::new();
    //~^ some fields are not explicitly listed

    // OK: both unstable fields are matched with feature on
    #[warn(non_exhaustive_omitted_patterns)]
    let OnlyUnstableStruct { unstable, unstable2, .. } = OnlyUnstableStruct::new();

    #[warn(non_exhaustive_omitted_patterns)]
    let UnstableStruct { stable, stable2, .. } = UnstableStruct::default();
    //~^ some fields are not explicitly listed

    // OK: both unstable and stable fields are matched with feature on
    #[warn(non_exhaustive_omitted_patterns)]
    let UnstableStruct { stable, stable2, unstable, .. } = UnstableStruct::default();
}