summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/manual_let_else.rs
blob: 27717ab3a73a6d004bbaabb63199627ce2428667 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#![allow(unused_braces, unused_variables, dead_code)]
#![allow(
    clippy::collapsible_else_if,
    clippy::unused_unit,
    clippy::let_unit_value,
    clippy::match_single_binding,
    clippy::never_loop,
    clippy::needless_if
)]
#![warn(clippy::manual_let_else)]
//@no-rustfix
enum Variant {
    A(usize, usize),
    B(usize),
    C,
}

fn g() -> Option<()> {
    None
}

fn main() {}

fn fire() {
    let v = if let Some(v_some) = g() { v_some } else { return };
    //~^ ERROR: this could be rewritten as `let...else`
    //~| NOTE: `-D clippy::manual-let-else` implied by `-D warnings`
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        return;
    };

    let v = if let Some(v) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        // Blocks around the identity should have no impact
        { { v } }
    } else {
        // Some computation should still make it fire
        g();
        return;
    };

    // continue and break diverge
    loop {
        let v = if let Some(v_some) = g() { v_some } else { continue };
        //~^ ERROR: this could be rewritten as `let...else`
        let v = if let Some(v_some) = g() { v_some } else { break };
        //~^ ERROR: this could be rewritten as `let...else`
    }

    // panic also diverges
    let v = if let Some(v_some) = g() { v_some } else { panic!() };
    //~^ ERROR: this could be rewritten as `let...else`

    // abort also diverges
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        std::process::abort()
    };

    // If whose two branches diverge also diverges
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        if true { return } else { panic!() }
    };

    // Diverging after an if still makes the block diverge:
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        if true {}
        panic!();
    };

    // A match diverges if all branches diverge:
    // Note: the corresponding let-else requires a ; at the end of the match
    // as otherwise the type checker does not turn it into a ! type.
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        match () {
            _ if panic!() => {},
            _ => panic!(),
        }
    };

    // An if's expression can cause divergence:
    let v = if let Some(v_some) = g() { v_some } else { if panic!() {} };
    //~^ ERROR: this could be rewritten as `let...else`

    // An expression of a match can cause divergence:
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        match panic!() {
            _ => {},
        }
    };

    // Top level else if
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else if true {
        return;
    } else {
        panic!("diverge");
    };

    // All match arms diverge
    let v = if let Some(v_some) = g() {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        match (g(), g()) {
            (Some(_), None) => return,
            (None, Some(_)) => {
                if true {
                    return;
                } else {
                    panic!();
                }
            },
            _ => return,
        }
    };

    // Tuples supported for the declared variables
    let (v, w) = if let Some(v_some) = g().map(|v| (v, 42)) {
        //~^ ERROR: this could be rewritten as `let...else`
        v_some
    } else {
        return;
    };

    // Tuples supported with multiple bindings
    let (w, S { v }) = if let (Some(v_some), w_some) = (g().map(|_| S { v: 0 }), 0) {
        //~^ ERROR: this could be rewritten as `let...else`
        (w_some, v_some)
    } else {
        return;
    };

    // entirely inside macro lints
    macro_rules! create_binding_if_some {
        ($n:ident, $e:expr) => {
            let $n = if let Some(v) = $e { v } else { return };
        };
    }
    create_binding_if_some!(w, g());

    fn e() -> Variant {
        Variant::A(0, 0)
    }

    let v = if let Variant::A(a, 0) = e() { a } else { return };
    //~^ ERROR: this could be rewritten as `let...else`

    // `mut v` is inserted into the pattern
    let mut v = if let Variant::B(b) = e() { b } else { return };
    //~^ ERROR: this could be rewritten as `let...else`

    // Nesting works
    let nested = Ok(Some(e()));
    let v = if let Ok(Some(Variant::B(b))) | Err(Some(Variant::A(b, _))) = nested {
        //~^ ERROR: this could be rewritten as `let...else`
        b
    } else {
        return;
    };
    // dot dot works
    let v = if let Variant::A(.., a) = e() { a } else { return };
    //~^ ERROR: this could be rewritten as `let...else`

    // () is preserved: a bit of an edge case but make sure it stays around
    let w = if let (Some(v), ()) = (g(), ()) { v } else { return };
    //~^ ERROR: this could be rewritten as `let...else`

    // Tuple structs work
    let w = if let Some(S { v: x }) = Some(S { v: 0 }) {
        //~^ ERROR: this could be rewritten as `let...else`
        x
    } else {
        return;
    };

    // Field init shorthand is suggested
    let v = if let Some(S { v: x }) = Some(S { v: 0 }) {
        //~^ ERROR: this could be rewritten as `let...else`
        x
    } else {
        return;
    };

    // Multi-field structs also work
    let (x, S { v }, w) = if let Some(U { v, w, x }) = None::<U<S<()>>> {
        //~^ ERROR: this could be rewritten as `let...else`
        (x, v, w)
    } else {
        return;
    };
}

fn not_fire() {
    let v = if let Some(v_some) = g() {
        // Nothing returned. Should not fire.
    } else {
        return;
    };

    let w = 0;
    let v = if let Some(v_some) = g() {
        // Different variable than v_some. Should not fire.
        w
    } else {
        return;
    };

    let v = if let Some(v_some) = g() {
        // Computation in then clause. Should not fire.
        g();
        v_some
    } else {
        return;
    };

    let v = if let Some(v_some) = g() {
        v_some
    } else {
        if false {
            return;
        }
        // This doesn't diverge. Should not fire.
        ()
    };

    let v = if let Some(v_some) = g() {
        v_some
    } else {
        // There is one match arm that doesn't diverge. Should not fire.
        match (g(), g()) {
            (Some(_), None) => return,
            (None, Some(_)) => return,
            (Some(_), Some(_)) => (),
            _ => return,
        }
    };

    let v = if let Some(v_some) = g() {
        v_some
    } else {
        // loop with a break statement inside does not diverge.
        loop {
            break;
        }
    };

    enum Uninhabited {}
    fn un() -> Uninhabited {
        panic!()
    }
    let v = if let Some(v_some) = None {
        v_some
    } else {
        // Don't lint if the type is uninhabited but not !
        un()
    };

    fn question_mark() -> Option<()> {
        let v = if let Some(v) = g() {
            v
        } else {
            // Question mark does not diverge
            g()?
        };
        Some(v)
    }

    // Macro boundary inside let
    macro_rules! some_or_return {
        ($e:expr) => {
            if let Some(v) = $e { v } else { return }
        };
    }
    let v = some_or_return!(g());

    // Also macro boundary inside let, but inside a macro
    macro_rules! create_binding_if_some_nf {
        ($n:ident, $e:expr) => {
            let $n = some_or_return!($e);
        };
    }
    create_binding_if_some_nf!(v, g());

    // Already a let-else
    let Some(a) = (if let Some(b) = Some(Some(())) { b } else { return }) else {
        panic!()
    };

    // If a type annotation is present, don't lint as
    // expressing the type might be too hard
    let v: () = if let Some(v_some) = g() { v_some } else { panic!() };

    // Issue 9940
    // Suggestion should not expand macros
    macro_rules! macro_call {
        () => {
            return ()
        };
    }

    let ff = Some(1);
    let _ = match ff {
        //~^ ERROR: this could be rewritten as `let...else`
        Some(value) => value,
        _ => macro_call!(),
    };

    // Issue 10296
    // The let/else block in the else part is not divergent despite the presence of return
    let _x = if let Some(x) = Some(1) {
        x
    } else {
        let Some(_z) = Some(3) else { return };
        1
    };

    // This would require creation of a suggestion of the form
    // let v @ (Some(_), _) = (...) else { return };
    // Which is too advanced for our code, so we just bail.
    let v = if let (Some(v_some), w_some) = (g(), 0) {
        (w_some, v_some)
    } else {
        return;
    };
}

struct S<T> {
    v: T,
}

struct U<T> {
    v: T,
    w: T,
    x: T,
}