File: exhaustiveness-unreachable-pattern.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (187 lines) | stat: -rw-r--r-- 4,985 bytes parent folder | download | duplicates (4)
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
#![deny(unreachable_patterns)]

// We wrap patterns in a tuple because top-level or-patterns were special-cased.
#[rustfmt::skip]
fn main() {
    match (0u8,) {
        (1 | 2,) => {}
        (1,) => {} //~ ERROR unreachable pattern
        _ => {}
    }
    match (0u8,) {
        (1 | 2,) => {}
        (2,) => {} //~ ERROR unreachable pattern
        _ => {}
    }
    match (0u8,) {
        (1,) => {}
        (2,) => {}
        (1 | 2,) => {} //~ ERROR unreachable pattern
        _ => {}
    }
    match (0u8, 0u8) {
        (1 | 2, 3 | 4) => {}
        (1, 3) => {}     //~ ERROR unreachable pattern
        (1, 4) => {}     //~ ERROR unreachable pattern
        (2, 4) => {}     //~ ERROR unreachable pattern
        (2 | 1, 4) => {} //~ ERROR unreachable pattern
        (1, 5 | 6) => {}
        (1, 4 | 5) => {} //~ ERROR unreachable pattern
        _ => {}
    }
    match (0u8, 0u8, 0u8) {
        (0, 0, 0) => {}
        (0, 0 | 1, 0) => {} //~ ERROR unreachable pattern
        _ => {}
    }
    match (true, true) {
        (false | true, false | true) => (),
    }
    match (Some(0u8),) {
        (None | Some(1 | 2),) => {}
        (Some(1),) => {} //~ ERROR unreachable pattern
        (None,) => {}    //~ ERROR unreachable pattern
        _ => {}
    }
    match ((0u8,),) {
        ((1 | 2,) | (3 | 4,),) => {}
        ((1..=4,),) => {} //~ ERROR unreachable pattern
        _ => {}
    }

    match (0,) {
        (1 | 1,) => {} //~ ERROR unreachable
        _ => {}
    }
    match 0 {
        (0 | 1) | 1 => {} //~ ERROR unreachable
        _ => {}
    }
    match 0 {
        // We get two errors because recursive or-pattern expansion means we don't notice the two
        // errors span a whole pattern. This could be better but doesn't matter much
        0 | (0 | 0) => {}
        //~^ ERROR unreachable
        //~| ERROR unreachable
        _ => {}
    }
    match None {
        // There is only one error that correctly points to the whole subpattern
        Some(0) |
            Some( //~ ERROR unreachable
                0 | 0) => {}
        _ => {}
    }
    match [0; 2] {
        [0
            | 0 //~ ERROR unreachable
        , 0
            | 0] => {} //~ ERROR unreachable
        _ => {}
    }
    match (true, 0) {
        (true, 0 | 0) => {} //~ ERROR unreachable
        (_, 0 | 0) => {} //~ ERROR unreachable
        _ => {}
    }
    match &[][..] {
        [0] => {}
        [0, _] => {}
        [0, _, _] => {}
        [1, ..] => {}
        [1 //~ ERROR unreachable
            | 2, ..] => {}
        _ => {}
    }
    match &[][..] {
        [true] => {}
        [true | false, ..] => {}
        _ => {}
    }
    match &[][..] {
        [false] => {}
        [true, ..] => {}
        [true //~ ERROR unreachable
            | false, ..] => {}
        _ => {}
    }
    match (true, None) {
        (true, Some(_)) => {}
        (false, Some(true)) => {}
        (true | false, None | Some(true //~ ERROR unreachable
                                   | false)) => {}
    }
    macro_rules! t_or_f {
        () => {
            (true //~ ERROR unreachable
            | false)
        };
    }
    match (true, None) {
        (true, Some(_)) => {}
        (false, Some(true)) => {}
        (true | false, None | Some(t_or_f!())) => {}
    }
    match Some(0) {
        Some(0) => {}
        Some(0 //~ ERROR unreachable
             | 1) => {}
        _ => {}
    }

    // A subpattern that is only unreachable in one branch is overall reachable.
    match (true, true) {
        (true, true) => {}
        (false | true, false | true) => {}
    }
    match (true, true) {
        (true, true) => {}
        (false, false) => {}
        (false | true, false | true) => {}
    }
    // https://github.com/rust-lang/rust/issues/76836
    match None {
        Some(false) => {}
        None | Some(true
                | false) => {} //~ ERROR unreachable
    }

    // A subpattern that is unreachable in all branches is overall unreachable.
    match (true, true) {
        (false, true) => {}
        (true, true) => {}
        (false | true, false
            | true) => {} //~ ERROR unreachable
    }
    match (true, true) {
        (true, false) => {}
        (true, true) => {}
        (false
            | true, //~ ERROR unreachable
            false | true) => {}
    }
    match (true, true) {
        (x, y)
            | (y, x) => {} //~ ERROR unreachable
    }
}

fn unreachable_in_param((_ | (_, _)): (bool, bool)) {}
//~^ ERROR unreachable

fn unreachable_in_binding() {
    let bool_pair = (true, true);
    let bool_option = Some(true);

    let (_ | (_, _)) = bool_pair;
    //~^ ERROR unreachable
    for (_ | (_, _)) in [bool_pair] {}
    //~^ ERROR unreachable

    let (Some(_) | Some(true)) = bool_option else { return };
    //~^ ERROR unreachable
    if let Some(_) | Some(true) = bool_option {}
    //~^ ERROR unreachable
    while let Some(_) | Some(true) = bool_option {}
    //~^ ERROR unreachable
}