summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/redundant_guards.rs
blob: c0206b4cec75f8c9f6a5df68ceaf7fb9798bcf47 (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
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
188
189
190
191
192
193
194
195
//@aux-build:proc_macros.rs
#![feature(if_let_guard)]
#![allow(clippy::no_effect, unused)]
#![warn(clippy::redundant_guards)]

#[macro_use]
extern crate proc_macros;

struct A(u32);

struct B {
    e: Option<A>,
}

struct C(u32, u32);

#[derive(PartialEq)]
struct FloatWrapper(f32);
fn issue11304() {
    match 0.1 {
        x if x == 0.0 => todo!(),
        _ => todo!(),
    }
    match FloatWrapper(0.1) {
        x if x == FloatWrapper(0.0) => todo!(),
        _ => todo!(),
    }
}

fn main() {
    let c = C(1, 2);
    match c {
        C(x, y) if let 1 = y => ..,
        _ => todo!(),
    };

    let x = Some(Some(1));
    match x {
        Some(x) if matches!(x, Some(1) if true) => ..,
        Some(x) if matches!(x, Some(1)) => {
            println!("a");
            ..
        },
        Some(x) if let Some(1) = x => ..,
        Some(x) if x == Some(2) => ..,
        Some(x) if Some(2) == x => ..,
        // Don't lint, since x is used in the body
        Some(x) if let Some(1) = x => {
            x;
            ..
        }
        _ => todo!(),
    };
    let y = 1;
    match x {
        // Don't inline these, since y is not from the pat
        Some(x) if matches!(y, 1 if true) => ..,
        Some(x) if let 1 = y => ..,
        Some(x) if y == 2 => ..,
        Some(x) if 2 == y => ..,
        _ => todo!(),
    };
    let a = A(1);
    match a {
        _ if a.0 == 1 => {},
        _ if 1 == a.0 => {},
        _ => todo!(),
    }
    let b = B { e: Some(A(0)) };
    match b {
        B { e } if matches!(e, Some(A(2))) => ..,
        _ => todo!(),
    };
    // Do not lint, since we cannot represent this as a pattern (at least, without a conversion)
    let v = Some(vec![1u8, 2, 3]);
    match v {
        Some(x) if x == [1] => {},
        _ => {},
    }

    external! {
        let x = Some(Some(1));
        match x {
            Some(x) if let Some(1) = x => ..,
            _ => todo!(),
        };
    }
    with_span! {
        span
        let x = Some(Some(1));
        match x {
            Some(x) if let Some(1) = x => ..,
            _ => todo!(),
        };
    }
}

enum E {
    A(&'static str),
    B(&'static str),
    C(&'static str),
}

fn i() {
    match E::A("") {
        // Do not lint
        E::A(x) | E::B(x) | E::C(x) if x == "from an or pattern" => {},
        E::A(y) if y == "not from an or pattern" => {},
        _ => {},
    };
}

fn h(v: Option<u32>) {
    match v {
        x if matches!(x, Some(0)) => ..,
        _ => ..,
    };
}

// Do not lint

fn f(s: Option<std::ffi::OsString>) {
    match s {
        Some(x) if x == "a" => {},
        Some(x) if "a" == x => {},
        _ => {},
    }
}

struct S {
    a: usize,
}

impl PartialEq for S {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}

impl Eq for S {}

static CONST_S: S = S { a: 1 };

fn g(opt_s: Option<S>) {
    match opt_s {
        Some(x) if x == CONST_S => {},
        Some(x) if CONST_S == x => {},
        _ => {},
    }
}

mod issue11465 {
    enum A {
        Foo([u8; 3]),
    }

    struct B {
        b: String,
        c: i32,
    }

    fn issue11465() {
        let c = Some(1);
        match c {
            Some(ref x) if x == &1 => {},
            Some(ref x) if &1 == x => {},
            Some(ref x) if let &2 = x => {},
            Some(ref x) if matches!(x, &3) => {},
            _ => {},
        };

        let enum_a = A::Foo([98, 97, 114]);
        match enum_a {
            A::Foo(ref arr) if arr == b"foo" => {},
            A::Foo(ref arr) if b"foo" == arr => {},
            A::Foo(ref arr) if let b"bar" = arr => {},
            A::Foo(ref arr) if matches!(arr, b"baz") => {},
            _ => {},
        };

        let struct_b = B {
            b: "bar".to_string(),
            c: 42,
        };
        match struct_b {
            B { ref b, .. } if b == "bar" => {},
            B { ref b, .. } if "bar" == b => {},
            B { ref c, .. } if c == &1 => {},
            B { ref c, .. } if &1 == c => {},
            B { ref c, .. } if let &1 = c => {},
            B { ref c, .. } if matches!(c, &1) => {},
            _ => {},
        }
    }
}