summaryrefslogtreecommitdiffstats
path: root/src/test/ui/rfc-2497-if-let-chains/irrefutable-lets.rs
blob: 9afb6853b362752bc954bba811dae86aaf1babba (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
// revisions: allowed disallowed
//[allowed] check-pass

#![feature(if_let_guard, let_chains)]
#![cfg_attr(allowed, allow(irrefutable_let_patterns))]
#![cfg_attr(disallowed, deny(irrefutable_let_patterns))]

use std::ops::Range;

fn main() {
    let opt = Some(None..Some(1));

    if let first = &opt && let Some(ref second) = first && let None = second.start {}
    //[disallowed]~^ ERROR leading irrefutable pattern in let chain

    // No lint as the irrefutable pattern is surrounded by other stuff
    if 4 * 2 == 0 && let first = &opt && let Some(ref second) = first && let None = second.start {}

    if let first = &opt && let (a, b) = (1, 2) {}
    //[disallowed]~^ ERROR irrefutable `if let` patterns

    if let first = &opt && let Some(ref second) = first && let None = second.start && let v = 0 {}
    //[disallowed]~^ ERROR leading irrefutable pattern in let chain
    //[disallowed]~^^ ERROR trailing irrefutable pattern in let chain

    if let Some(ref first) = opt && let second = first && let _third = second {}
    //[disallowed]~^ ERROR trailing irrefutable patterns in let chain

    if let Range { start: local_start, end: _ } = (None..Some(1)) && let None = local_start {}
    //[disallowed]~^ ERROR leading irrefutable pattern in let chain

    if let (a, b, c) = (Some(1), Some(1), Some(1)) && let None = Some(1) {}
    //[disallowed]~^ ERROR leading irrefutable pattern in let chain

    if let first = &opt && let None = Some(1) {}
    //[disallowed]~^ ERROR leading irrefutable pattern in let chain

    if let Some(ref first) = opt
        && let Range { start: local_start, end: _ } = first
        && let None = local_start {
    }

    match opt {
        Some(ref first) if let second = first && let _third = second && let v = 4 + 4 => {},
        //[disallowed]~^ ERROR irrefutable `if let` guard patterns
        _ => {}
    }

    // No error about leading irrefutable patterns: the expr on the rhs might
    // use the bindings created by the match.
    match opt {
        Some(ref first) if let Range { start: local_start, end: _ } = first
            && let None = local_start => {},
        _ => {}
    }

    match opt {
        Some(ref first) if let Range { start: Some(_), end: local_end } = first
            && let v = local_end && let w = v => {},
        //[disallowed]~^ ERROR trailing irrefutable patterns in let chain
        _ => {}
    }

    // No error, despite the prefix being irrefutable: moving out could change the behaviour,
    // due to possible side effects of the operation.
    while let first = &opt && let Some(ref second) = first && let None = second.start {}

    while let first = &opt && let (a, b) = (1, 2) {}
    //[disallowed]~^ ERROR irrefutable `while let` patterns

    while let Some(ref first) = opt && let second = first && let _third = second {}
    //[disallowed]~^ ERROR trailing irrefutable patterns in let chain

    while let Some(ref first) = opt
        && let Range { start: local_start, end: _ } = first
        && let None = local_start {
    }
}