about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/nonminimal_bool.rs
blob: 1eecc3dee3dc5eca8f27fb190cc53f4a10106398 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//@no-rustfix: overlapping suggestions
#![allow(
    unused,
    clippy::diverging_sub_expression,
    clippy::needless_if,
    clippy::redundant_pattern_matching
)]
#![warn(clippy::nonminimal_bool)]
#![allow(clippy::useless_vec)]

fn main() {
    let a: bool = unimplemented!();
    let b: bool = unimplemented!();
    let c: bool = unimplemented!();
    let d: bool = unimplemented!();
    let e: bool = unimplemented!();
    let _ = !true;
    //~^ nonminimal_bool

    let _ = !false;
    //~^ nonminimal_bool

    let _ = !!a;
    //~^ nonminimal_bool

    let _ = false || a;
    //~^ nonminimal_bool

    // don't lint on cfgs
    let _ = cfg!(you_shall_not_not_pass) && a;
    let _ = a || !b || !c || !d || !e;
    let _ = !(!a && b);
    //~^ nonminimal_bool

    let _ = !(!a || b);
    //~^ nonminimal_bool

    let _ = !a && !(b && c);
    //~^ nonminimal_bool
}

fn equality_stuff() {
    let a: i32 = unimplemented!();
    let b: i32 = unimplemented!();
    let c: i32 = unimplemented!();
    let d: i32 = unimplemented!();
    let _ = a == b && c == 5 && a == b;
    //~^ nonminimal_bool

    let _ = a == b || c == 5 || a == b;
    //~^ nonminimal_bool

    let _ = a == b && c == 5 && b == a;
    //~^ nonminimal_bool

    let _ = a != b || !(a != b || c == d);
    //~^ nonminimal_bool

    let _ = a != b && !(a != b && c == d);
    //~^ nonminimal_bool
}

fn issue3847(a: u32, b: u32) -> bool {
    const THRESHOLD: u32 = 1_000;

    if a < THRESHOLD && b >= THRESHOLD || a >= THRESHOLD && b < THRESHOLD {
        return false;
    }
    true
}

fn issue4548() {
    fn f(_i: u32, _j: u32) -> u32 {
        unimplemented!();
    }

    let i = 0;
    let j = 0;

    if i != j && f(i, j) != 0 || i == j && f(i, j) != 1 {}
}

fn check_expect() {
    let a: bool = unimplemented!();
    #[expect(clippy::nonminimal_bool)]
    let _ = !!a;
}

fn issue9428() {
    if matches!(true, true) && true {
        //~^ nonminimal_bool

        println!("foo");
    }
}

fn issue_10523() {
    macro_rules! a {
        ($v:expr) => {
            $v.is_some()
        };
    }
    let x: Option<u32> = None;
    if !a!(x) {}
}

fn issue_10523_1() {
    macro_rules! a {
        ($v:expr) => {
            !$v.is_some()
        };
    }
    let x: Option<u32> = None;
    if a!(x) {}
}

fn issue_10523_2() {
    macro_rules! a {
        () => {
            !None::<u32>.is_some()
        };
    }
    if a!() {}
}

fn issue_10435() {
    let x = vec![0];
    let y = vec![1];
    let z = vec![2];

    // vvv Should not lint
    #[allow(clippy::nonminimal_bool)]
    if !x.is_empty() && !(y.is_empty() || z.is_empty()) {
        println!("{}", line!());
    }

    // vvv Should not lint (#10435 talks about a bug where it lints)
    #[allow(clippy::nonminimal_bool)]
    if !(x == [0]) {
        println!("{}", line!());
    }
}

fn issue10836() {
    struct Foo(bool);
    impl std::ops::Not for Foo {
        type Output = bool;

        fn not(self) -> Self::Output {
            !self.0
        }
    }

    // Should not lint
    let _: bool = !!Foo(true);
}

fn issue11932() {
    let x: i32 = unimplemented!();

    #[allow(clippy::nonminimal_bool)]
    let _ = x % 2 == 0 || {
        // Should not lint
        assert!(x > 0);
        x % 3 == 0
    };
}

fn issue_5794() {
    let a = 0;
    if !(12 == a) {}
    //~^ nonminimal_bool
    if !(a == 12) {}
    //~^ nonminimal_bool
    if !(12 != a) {}
    //~^ nonminimal_bool
    if !(a != 12) {}
    //~^ nonminimal_bool

    let b = true;
    let c = false;
    if !b == true {}
    //~^ nonminimal_bool
    //~| bool_comparison
    //~| bool_comparison
    if !b != true {}
    //~^ nonminimal_bool
    //~| bool_comparison
    if true == !b {}
    //~^ nonminimal_bool
    //~| bool_comparison
    //~| bool_comparison
    if true != !b {}
    //~^ nonminimal_bool
    //~| bool_comparison
    if !b == !c {}
    //~^ nonminimal_bool
    if !b != !c {}
    //~^ nonminimal_bool
}

fn issue_12371(x: usize) -> bool {
    // Should not warn!
    !x != 0
}

// Not linted because it is slow to do so
// https://github.com/rust-lang/rust-clippy/issues/13206
fn many_ops(a: bool, b: bool, c: bool, d: bool, e: bool, f: bool) -> bool {
    (a && c && f) || (!a && b && !d) || (!b && !c && !e) || (d && e && !f)
}

fn issue14184(a: f32, b: bool) {
    if !(a < 2.0 && !b) {
        //~^ nonminimal_bool
        println!("Hi");
    }
}

mod issue14404 {
    enum TyKind {
        Ref(i32, i32, i32),
        Other,
    }

    struct Expr;

    fn is_mutable(expr: &Expr) -> bool {
        todo!()
    }

    fn should_not_give_macro(ty: TyKind, expr: Expr) {
        if !(matches!(ty, TyKind::Ref(_, _, _)) && !is_mutable(&expr)) {
            //~^ nonminimal_bool
            todo!()
        }
    }
}