about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/wild_in_or_pats.rs
blob: ced13cc50c5d5ad54cc74a76cd008c3b60de533d (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
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
#![warn(clippy::wildcard_in_or_patterns)]

fn main() {
    match "foo" {
        "a" => {
            dbg!("matched a");
        },
        "bar" | _ => {
            //~^ wildcard_in_or_patterns

            dbg!("matched (bar or) wild");
        },
    };
    match "foo" {
        "a" => {
            dbg!("matched a");
        },
        "bar" | "bar2" | _ => {
            //~^ wildcard_in_or_patterns

            dbg!("matched (bar or bar2 or) wild");
        },
    };
    match "foo" {
        "a" => {
            dbg!("matched a");
        },
        _ | "bar" | _ => {
            //~^ wildcard_in_or_patterns

            dbg!("matched (bar or) wild");
        },
    };
    match "foo" {
        "a" => {
            dbg!("matched a");
        },
        _ | "bar" => {
            //~^ wildcard_in_or_patterns

            dbg!("matched (bar or) wild");
        },
    };

    // shouldn't lint
    #[non_exhaustive]
    pub enum NonExhaustiveEnum<'a> {
        Message(&'a str),
        Quit(&'a str),
        Other,
    }

    match NonExhaustiveEnum::Message("Pass") {
        NonExhaustiveEnum::Message(_) => dbg!("message"),
        NonExhaustiveEnum::Quit(_) => dbg!("quit"),
        NonExhaustiveEnum::Other | _ => dbg!("wildcard"),
    };

    // should lint
    enum ExhaustiveEnum {
        Quit,
        Write(String),
        ChangeColor(i32, i32, i32),
    }

    match ExhaustiveEnum::ChangeColor(0, 160, 255) {
        ExhaustiveEnum::Write(text) => {
            dbg!("Write");
        },
        ExhaustiveEnum::ChangeColor(r, g, b) => {
            dbg!("Change the color");
        },
        ExhaustiveEnum::Quit | _ => {
            //~^ wildcard_in_or_patterns
            dbg!("Quit or other");
        },
    };

    // shouldn't lint
    #[non_exhaustive]
    struct NonExhaustiveStruct {
        a: u32,
        b: u32,
        c: u64,
    }

    let b = NonExhaustiveStruct { a: 5, b: 42, c: 342 };

    match b {
        NonExhaustiveStruct { a: 5, b: 42, .. } => {},
        NonExhaustiveStruct { a: 0, b: 0, c: 128 } => {},
        NonExhaustiveStruct { a: 0, b: 0, c: 128, .. } | _ => {},
    }

    // should lint
    struct ExhaustiveStruct {
        x: i32,
        y: i32,
    }

    let p = ExhaustiveStruct { x: 0, y: 7 };
    match p {
        ExhaustiveStruct { x: 0, y: 0 } => {
            dbg!("On the x axis at {x}");
        },
        ExhaustiveStruct { x: 0, y: 1 } => {
            dbg!("On the y axis at {y}");
        },
        ExhaustiveStruct { x: 1, y: 1 } | _ => {
            //~^ wildcard_in_or_patterns
            dbg!("On neither axis: ({x}, {y})");
        },
    }
}