]> git.lizzy.rs Git - rust.git/blob - src/test/ui/or-patterns/exhaustiveness-unreachable-pattern.rs
Auto merge of #82864 - jyn514:short-circuit, r=GuillaumeGomez
[rust.git] / src / test / ui / or-patterns / exhaustiveness-unreachable-pattern.rs
1 #![deny(unreachable_patterns)]
2
3 // We wrap patterns in a tuple because top-level or-patterns were special-cased.
4 fn main() {
5     match (0u8,) {
6         (1 | 2,) => {}
7         (1,) => {} //~ ERROR unreachable pattern
8         _ => {}
9     }
10     match (0u8,) {
11         (1 | 2,) => {}
12         (2,) => {} //~ ERROR unreachable pattern
13         _ => {}
14     }
15     match (0u8,) {
16         (1,) => {}
17         (2,) => {}
18         (1 | 2,) => {} //~ ERROR unreachable pattern
19         _ => {}
20     }
21     match (0u8, 0u8) {
22         (1 | 2, 3 | 4) => {}
23         (1, 3) => {}     //~ ERROR unreachable pattern
24         (1, 4) => {}     //~ ERROR unreachable pattern
25         (2, 4) => {}     //~ ERROR unreachable pattern
26         (2 | 1, 4) => {} //~ ERROR unreachable pattern
27         (1, 5 | 6) => {}
28         (1, 4 | 5) => {} //~ ERROR unreachable pattern
29         _ => {}
30     }
31     match (true, true) {
32         (false | true, false | true) => (),
33     }
34     match (Some(0u8),) {
35         (None | Some(1 | 2),) => {}
36         (Some(1),) => {} //~ ERROR unreachable pattern
37         (None,) => {}    //~ ERROR unreachable pattern
38         _ => {}
39     }
40     match ((0u8,),) {
41         ((1 | 2,) | (3 | 4,),) => {}
42         ((1..=4,),) => {} //~ ERROR unreachable pattern
43         _ => {}
44     }
45
46     match (0,) {
47         (1 | 1,) => {} //~ ERROR unreachable
48         _ => {}
49     }
50     match 0 {
51         (0 | 1) | 1 => {} //~ ERROR unreachable
52         _ => {}
53     }
54     match 0 {
55         // We get two errors because recursive or-pattern expansion means we don't notice the two
56         // errors span a whole pattern. This could be better but doesn't matter much
57         0 | (0 | 0) => {}
58         //~^ ERROR unreachable
59         //~| ERROR unreachable
60         _ => {}
61     }
62     match None {
63         // There is only one error that correctly points to the whole subpattern
64         Some(0) |
65             Some( //~ ERROR unreachable
66                 0 | 0) => {}
67         _ => {}
68     }
69     match [0; 2] {
70         [0
71             | 0 //~ ERROR unreachable
72         , 0
73             | 0] => {} //~ ERROR unreachable
74         _ => {}
75     }
76     match &[][..] {
77         [0] => {}
78         [0, _] => {}
79         [0, _, _] => {}
80         [1, ..] => {}
81         [1 //~ ERROR unreachable
82             | 2, ..] => {}
83         _ => {}
84     }
85     match &[][..] {
86         [true] => {}
87         [true | false, ..] => {}
88         _ => {}
89     }
90     match &[][..] {
91         [false] => {}
92         [true, ..] => {}
93         [true //~ ERROR unreachable
94             | false, ..] => {}
95         _ => {}
96     }
97     match (true, None) {
98         (true, Some(_)) => {}
99         (false, Some(true)) => {}
100         (true | false, None | Some(true //~ ERROR unreachable
101                                    | false)) => {}
102     }
103     macro_rules! t_or_f {
104         () => {
105             (true //~ ERROR unreachable
106             | false)
107         };
108     }
109     match (true, None) {
110         (true, Some(_)) => {}
111         (false, Some(true)) => {}
112         (true | false, None | Some(t_or_f!())) => {}
113     }
114     match Some(0) {
115         Some(0) => {}
116         Some(0 //~ ERROR unreachable
117              | 1) => {}
118         _ => {}
119     }
120
121     // A subpattern that is only unreachable in one branch is overall reachable.
122     match (true, true) {
123         (true, true) => {}
124         (false | true, false | true) => {}
125     }
126     match (true, true) {
127         (true, true) => {}
128         (false, false) => {}
129         (false | true, false | true) => {}
130     }
131     // https://github.com/rust-lang/rust/issues/76836
132     match None {
133         Some(false) => {}
134         None | Some(true
135                 | false) => {} //~ ERROR unreachable
136     }
137
138     // A subpattern that is unreachable in all branches is overall unreachable.
139     match (true, true) {
140         (false, true) => {}
141         (true, true) => {}
142         (false | true, false
143             | true) => {} //~ ERROR unreachable
144     }
145     match (true, true) {
146         (true, false) => {}
147         (true, true) => {}
148         (false
149             | true, //~ ERROR unreachable
150             false | true) => {}
151     }
152 }