Skip to content

Commit 0964b8d

Browse files
committed
Auto merge of #5046 - JohnTitor:order-nonminimal-bool, r=flip1995
Keep the ordering in `nonminimal_bool` lint I believe it shouldn't cause any regression but feel free to let me know if you have a doubtful example. Also, splits up `booleans` ui test. Fixes #5045 changelog: none
2 parents ee06aa9 + 3885033 commit 0964b8d

8 files changed

+339
-265
lines changed

clippy_lints/src/booleans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
207207
}
208208
},
209209
Or(v) => {
210-
for (index, inner) in v.iter().enumerate() {
210+
for (index, inner) in v.iter().rev().enumerate() {
211211
if index > 0 {
212212
self.output.push_str(" || ");
213213
}

tests/ui/booleans.stderr

-206
This file was deleted.

tests/ui/logic_bug.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![allow(unused, clippy::many_single_char_names)]
2+
#![warn(clippy::logic_bug)]
3+
4+
fn main() {
5+
let a: bool = unimplemented!();
6+
let b: bool = unimplemented!();
7+
let c: bool = unimplemented!();
8+
let d: bool = unimplemented!();
9+
let e: bool = unimplemented!();
10+
let _ = a && b || a;
11+
let _ = !(a && b);
12+
let _ = false && a;
13+
// don't lint on cfgs
14+
let _ = cfg!(you_shall_not_not_pass) && a;
15+
let _ = a || !b || !c || !d || !e;
16+
let _ = !(a && b || c);
17+
}
18+
19+
fn equality_stuff() {
20+
let a: i32 = unimplemented!();
21+
let b: i32 = unimplemented!();
22+
let _ = a == b && a != b;
23+
let _ = a < b && a >= b;
24+
let _ = a > b && a <= b;
25+
let _ = a > b && a == b;
26+
}

tests/ui/logic_bug.stderr

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error: this boolean expression contains a logic bug
2+
--> $DIR/logic_bug.rs:10:13
3+
|
4+
LL | let _ = a && b || a;
5+
| ^^^^^^^^^^^ help: it would look like the following: `a`
6+
|
7+
= note: `-D clippy::logic-bug` implied by `-D warnings`
8+
help: this expression can be optimized out by applying boolean operations to the outer expression
9+
--> $DIR/logic_bug.rs:10:18
10+
|
11+
LL | let _ = a && b || a;
12+
| ^
13+
14+
error: this boolean expression contains a logic bug
15+
--> $DIR/logic_bug.rs:12:13
16+
|
17+
LL | let _ = false && a;
18+
| ^^^^^^^^^^ help: it would look like the following: `false`
19+
|
20+
help: this expression can be optimized out by applying boolean operations to the outer expression
21+
--> $DIR/logic_bug.rs:12:22
22+
|
23+
LL | let _ = false && a;
24+
| ^
25+
26+
error: this boolean expression contains a logic bug
27+
--> $DIR/logic_bug.rs:22:13
28+
|
29+
LL | let _ = a == b && a != b;
30+
| ^^^^^^^^^^^^^^^^ help: it would look like the following: `false`
31+
|
32+
help: this expression can be optimized out by applying boolean operations to the outer expression
33+
--> $DIR/logic_bug.rs:22:13
34+
|
35+
LL | let _ = a == b && a != b;
36+
| ^^^^^^
37+
38+
error: this boolean expression contains a logic bug
39+
--> $DIR/logic_bug.rs:23:13
40+
|
41+
LL | let _ = a < b && a >= b;
42+
| ^^^^^^^^^^^^^^^ help: it would look like the following: `false`
43+
|
44+
help: this expression can be optimized out by applying boolean operations to the outer expression
45+
--> $DIR/logic_bug.rs:23:13
46+
|
47+
LL | let _ = a < b && a >= b;
48+
| ^^^^^
49+
50+
error: this boolean expression contains a logic bug
51+
--> $DIR/logic_bug.rs:24:13
52+
|
53+
LL | let _ = a > b && a <= b;
54+
| ^^^^^^^^^^^^^^^ help: it would look like the following: `false`
55+
|
56+
help: this expression can be optimized out by applying boolean operations to the outer expression
57+
--> $DIR/logic_bug.rs:24:13
58+
|
59+
LL | let _ = a > b && a <= b;
60+
| ^^^^^
61+
62+
error: aborting due to 5 previous errors
63+

tests/ui/nonminimal_bool.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![allow(unused, clippy::many_single_char_names)]
2+
#![warn(clippy::nonminimal_bool)]
3+
4+
fn main() {
5+
let a: bool = unimplemented!();
6+
let b: bool = unimplemented!();
7+
let c: bool = unimplemented!();
8+
let d: bool = unimplemented!();
9+
let e: bool = unimplemented!();
10+
let _ = !true;
11+
let _ = !false;
12+
let _ = !!a;
13+
let _ = false || a;
14+
// don't lint on cfgs
15+
let _ = cfg!(you_shall_not_not_pass) && a;
16+
let _ = a || !b || !c || !d || !e;
17+
let _ = !(!a && b);
18+
let _ = !(!a || b);
19+
let _ = !a && !(b && c);
20+
}
21+
22+
fn equality_stuff() {
23+
let a: i32 = unimplemented!();
24+
let b: i32 = unimplemented!();
25+
let c: i32 = unimplemented!();
26+
let d: i32 = unimplemented!();
27+
let _ = a == b && c == 5 && a == b;
28+
let _ = a == b || c == 5 || a == b;
29+
let _ = a == b && c == 5 && b == a;
30+
let _ = a != b || !(a != b || c == d);
31+
let _ = a != b && !(a != b && c == d);
32+
}
33+
34+
fn issue3847(a: u32, b: u32) -> bool {
35+
const THRESHOLD: u32 = 1_000;
36+
37+
if a < THRESHOLD && b >= THRESHOLD || a >= THRESHOLD && b < THRESHOLD {
38+
return false;
39+
}
40+
true
41+
}
42+
43+
fn issue4548() {
44+
fn f(_i: u32, _j: u32) -> u32 {
45+
unimplemented!();
46+
}
47+
48+
let i = 0;
49+
let j = 0;
50+
51+
if i != j && f(i, j) != 0 || i == j && f(i, j) != 1 {}
52+
}

0 commit comments

Comments
 (0)