summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unneeded_struct_pattern.rs
blob: c7658617ad376457c14af6b805333395b56cc13e (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
//@aux-build:non-exhaustive-enum.rs
#![allow(
    clippy::manual_unwrap_or_default,
    clippy::manual_unwrap_or,
    clippy::redundant_pattern_matching
)]
#![warn(clippy::unneeded_struct_pattern)]

extern crate non_exhaustive_enum;
use non_exhaustive_enum::*;

fn noop() {}

fn main() {
    match Some(114514) {
        Some(v) => v,
        None {} => 0,
    };

    match Some(1919810) {
        Some(v) => v,
        None { .. } => 0,
    };

    match Some(123456) {
        Some(v) => v,
        None => 0,
    };

    match Some(Some(123456)) {
        Some(Some(v)) => v,
        Some(None {}) => 0,
        None {} => 0,
    };

    if let None {} = Some(0) {}
    if let None { .. } = Some(0) {}
    if let Some(None {}) = Some(Some(0)) {}
    let None {} = Some(0) else { panic!() };
    let None { .. } = Some(0) else { panic!() };
    let Some(None {}) = Some(Some(0)) else { panic!() };

    enum Custom {
        HasFields {
            field: i32,
        },
        HasBracketsNoFields {},
        NoBrackets,
        #[non_exhaustive]
        NoBracketsNonExhaustive,
        Init,
    };

    match Custom::Init {
        Custom::HasFields { field: value } => value,
        Custom::HasBracketsNoFields {} => 0,
        Custom::NoBrackets {} => 0, //~ ERROR: struct pattern is not needed for a unit variant
        Custom::NoBracketsNonExhaustive {} => 0, //~ ERROR: struct pattern is not needed for a unit variant
        _ => 0,
    };

    match Custom::Init {
        Custom::HasFields { field: value } => value,
        Custom::HasBracketsNoFields { .. } => 0,
        Custom::NoBrackets { .. } => 0, //~ ERROR: struct pattern is not needed for a unit variant
        Custom::NoBracketsNonExhaustive { .. } => 0, //~ ERROR: struct pattern is not needed for a unit variant
        _ => 0,
    };

    match Custom::Init {
        Custom::NoBrackets {} if true => 0, //~ ERROR: struct pattern is not needed for a unit variant
        _ => 0,
    };

    match Custom::Init {
        Custom::NoBrackets {} | Custom::NoBracketsNonExhaustive {} => 0, //~ ERROR: struct pattern is not needed for a unit variant
        _ => 0,
    };

    if let Custom::HasFields { field: value } = Custom::Init {
        noop();
    }
    if let Custom::HasBracketsNoFields {} = Custom::Init {
        noop();
    }
    if let Custom::HasBracketsNoFields { .. } = Custom::Init {
        noop();
    }
    if let Custom::NoBrackets {} = Custom::Init {
        //~^ ERROR: struct pattern is not needed for a unit variant
        noop();
    }
    if let Custom::NoBrackets { .. } = Custom::Init {
        //~^ ERROR: struct pattern is not needed for a unit variant
        noop();
    }
    if let Custom::NoBrackets {} | Custom::NoBracketsNonExhaustive {} = Custom::Init {
        //~^ ERROR: struct pattern is not needed for a unit variant
        noop();
    }
    if let Custom::NoBracketsNonExhaustive {} = Custom::Init {
        //~^ ERROR: struct pattern is not needed for a unit variant
        noop();
    }
    if let Custom::NoBracketsNonExhaustive { .. } = Custom::Init {
        //~^ ERROR: struct pattern is not needed for a unit variant
        noop();
    }

    let Custom::HasFields { field: value } = Custom::Init else {
        panic!()
    };

    let Custom::HasBracketsNoFields {} = Custom::Init else {
        panic!()
    };

    let Custom::HasBracketsNoFields { .. } = Custom::Init else {
        panic!()
    };
    let Custom::NoBrackets {} = Custom::Init else { panic!() }; //~ ERROR: struct pattern is not needed for a unit variant

    let Custom::NoBrackets { .. } = Custom::Init else {
        //~^ ERROR: struct pattern is not needed for a unit variant
        panic!()
    };
    let Custom::NoBracketsNonExhaustive {} = Custom::Init else {
        //~^ ERROR: struct pattern is not needed for a unit variant
        panic!()
    };
    let Custom::NoBracketsNonExhaustive { .. } = Custom::Init else {
        //~^ ERROR: struct pattern is not needed for a unit variant
        panic!()
    };

    enum Refutable {
        Variant,
    }

    fn pat_in_fn_param_1(Refutable::Variant {}: Refutable) {} //~ ERROR: struct pattern is not needed for a unit variant
    fn pat_in_fn_param_2(Refutable::Variant { .. }: Refutable) {} //~ ERROR: struct pattern is not needed for a unit variant

    for Refutable::Variant {} in [] {} //~ ERROR: struct pattern is not needed for a unit variant
    for Refutable::Variant { .. } in [] {} //~ ERROR: struct pattern is not needed for a unit variant
}

fn external_crate() {
    use ExtNonExhaustiveVariant::*;

    match ExhaustiveUnit {
        // Expected
        ExhaustiveUnit => 0,
        _ => 0,
    };

    match ExhaustiveUnit {
        // Exhaustive variant
        ExhaustiveUnit { .. } => 0, //~ ERROR: struct pattern is not needed for a unit variant
        _ => 0,
    };

    match ExhaustiveUnit {
        // Exhaustive variant
        ExhaustiveUnit {} => 0, //~ ERROR: struct pattern is not needed for a unit variant
        _ => 0,
    };

    match ExhaustiveUnit {
        ExhaustiveUnit => 0,
        // vvvvv Non-exhaustive variants, should all be ignored
        Unit { .. } => 0,
        Tuple { 0: field, .. } => field,
        StructNoField { .. } => 0,
        Struct { field, .. } => field,
        _ => 0,
    };
}