]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc-2497-if-let-chains/irrefutable-lets.rs
Rollup merge of #105758 - Nilstrieb:typeck-results-mod, r=compiler-errors
[rust.git] / src / test / ui / rfc-2497-if-let-chains / irrefutable-lets.rs
1 // revisions: allowed disallowed
2 //[allowed] check-pass
3
4 #![feature(if_let_guard, let_chains)]
5 #![cfg_attr(allowed, allow(irrefutable_let_patterns))]
6 #![cfg_attr(disallowed, deny(irrefutable_let_patterns))]
7
8 use std::ops::Range;
9
10 fn main() {
11     let opt = Some(None..Some(1));
12
13     if let first = &opt && let Some(ref second) = first && let None = second.start {}
14     //[disallowed]~^ ERROR leading irrefutable pattern in let chain
15
16     // No lint as the irrefutable pattern is surrounded by other stuff
17     if 4 * 2 == 0 && let first = &opt && let Some(ref second) = first && let None = second.start {}
18
19     if let first = &opt && let (a, b) = (1, 2) {}
20     //[disallowed]~^ ERROR irrefutable `if let` patterns
21
22     if let first = &opt && let Some(ref second) = first && let None = second.start && let v = 0 {}
23     //[disallowed]~^ ERROR leading irrefutable pattern in let chain
24     //[disallowed]~^^ ERROR trailing irrefutable pattern in let chain
25
26     if let Some(ref first) = opt && let second = first && let _third = second {}
27     //[disallowed]~^ ERROR trailing irrefutable patterns in let chain
28
29     if let Range { start: local_start, end: _ } = (None..Some(1)) && let None = local_start {}
30     //[disallowed]~^ ERROR leading irrefutable pattern in let chain
31
32     if let (a, b, c) = (Some(1), Some(1), Some(1)) && let None = Some(1) {}
33     //[disallowed]~^ ERROR leading irrefutable pattern in let chain
34
35     if let first = &opt && let None = Some(1) {}
36     //[disallowed]~^ ERROR leading irrefutable pattern in let chain
37
38     if let Some(ref first) = opt
39         && let Range { start: local_start, end: _ } = first
40         && let None = local_start {
41     }
42
43     match opt {
44         Some(ref first) if let second = first && let _third = second && let v = 4 + 4 => {},
45         //[disallowed]~^ ERROR irrefutable `if let` guard patterns
46         _ => {}
47     }
48
49     // No error about leading irrefutable patterns: the expr on the rhs might
50     // use the bindings created by the match.
51     match opt {
52         Some(ref first) if let Range { start: local_start, end: _ } = first
53             && let None = local_start => {},
54         _ => {}
55     }
56
57     match opt {
58         Some(ref first) if let Range { start: Some(_), end: local_end } = first
59             && let v = local_end && let w = v => {},
60         //[disallowed]~^ ERROR trailing irrefutable patterns in let chain
61         _ => {}
62     }
63
64     // No error, despite the prefix being irrefutable: moving out could change the behaviour,
65     // due to possible side effects of the operation.
66     while let first = &opt && let Some(ref second) = first && let None = second.start {}
67
68     while let first = &opt && let (a, b) = (1, 2) {}
69     //[disallowed]~^ ERROR irrefutable `while let` patterns
70
71     while let Some(ref first) = opt && let second = first && let _third = second {}
72     //[disallowed]~^ ERROR trailing irrefutable patterns in let chain
73
74     while let Some(ref first) = opt
75         && let Range { start: local_start, end: _ } = first
76         && let None = local_start {
77     }
78 }