File: explain-unreachable-pats.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 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; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (123 lines) | stat: -rw-r--r-- 3,804 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
#![feature(never_type)]
#![feature(exhaustive_patterns)]
#![deny(unreachable_patterns)]
//~^ NOTE lint level is defined here

#[rustfmt::skip]
fn main() {
    match (0u8,) {
        (1 | 2,) => {}
        //~^ NOTE matches all the relevant values
        (2,) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE no value can reach this
        _ => {}
    }

    match (0u8,) {
        (1,) => {}
        //~^ NOTE matches some of the same values
        (2,) => {}
        //~^ NOTE matches some of the same values
        (1 | 2,) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE no value can reach this
        //~| NOTE multiple earlier patterns match some of the same values
        //~| NOTE collectively making this unreachable
        _ => {}
    }

    match 0u8 {
        1 => {}
        //~^ NOTE matches some of the same values
        2 => {}
        //~^ NOTE matches some of the same values
        3 => {}
        //~^ NOTE matches some of the same values
        4 => {}
        //~^ NOTE matches some of the same values
        5 => {}
        6 => {}
        1 ..= 6 => {}
        //~^ ERROR unreachable pattern
        //~| NOTE no value can reach this
        //~| NOTE multiple earlier patterns match some of the same values
        //~| NOTE ...and 2 other patterns
        _ => {}
    }

    let res: Result<(),!> = Ok(());
    match res {
        Ok(_) => {}
        Err(_) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE matches no values because `!` is uninhabited
        //~| NOTE to learn more about uninhabited types, see
    }

    #[derive(Copy, Clone)]
    enum Void1 {}
    #[derive(Copy, Clone)]
    enum Void2 {}
    // Only an empty type matched _by value_ can make an arm unreachable. We must get the right one.
    let res1: Result<(), Void1> = Ok(());
    let res2: Result<(), Void2> = Ok(());
    match (&res1, res2) {
        (Err(_), Err(_)) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE matches no values because `Void2` is uninhabited
        //~| NOTE to learn more about uninhabited types, see
        _ => {}
    }
    match (res1, &res2) {
        (Err(_), Err(_)) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE matches no values because `Void1` is uninhabited
        //~| NOTE to learn more about uninhabited types, see
        _ => {}
    }


    if let (0
        //~^ NOTE matches all the relevant values
        | 0, _) = (0, 0) {}
        //~^ ERROR unreachable pattern
        //~| NOTE no value can reach this

    match (true, true) {
        (_, true) if false => {} // Guarded patterns don't cover others
        (true, _) => {}
        //~^ NOTE matches some of the same values
        (false, _) => {}
        //~^ NOTE matches some of the same values
        (_, true) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE no value can reach this
        //~| NOTE multiple earlier patterns match some of the same values
        //~| NOTE collectively making this unreachable
    }

    match (true, true) {
        (true, _) => {}
        //~^ NOTE matches all the relevant values
        (false, _) => {}
        #[allow(unreachable_patterns)]
        (_, true) => {} // Doesn't cover below because it's already unreachable.
        (true, true) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE no value can reach this
    }

    // Despite skipping some irrelevant cases, we still report a set of rows that covers the
    // unreachable one.
    match (true, true, 0) {
        (true, _, _) => {}
        (_, true, 0..10) => {}
        //~^ NOTE matches all the relevant values
        (_, true, 10..) => {}
        (_, true, 3) => {}
        //~^ ERROR unreachable pattern
        //~| NOTE no value can reach this
        _ => {}
    }
}