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
This commit is contained in:
bors 2020-01-18 19:51:46 +00:00
commit 0964b8d192
8 changed files with 339 additions and 265 deletions

View File

@ -207,7 +207,7 @@ fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
}
},
Or(v) => {
for (index, inner) in v.iter().enumerate() {
for (index, inner) in v.iter().rev().enumerate() {
if index > 0 {
self.output.push_str(" || ");
}

View File

@ -1,206 +0,0 @@
error: this boolean expression contains a logic bug
--> $DIR/booleans.rs:10:13
|
LL | let _ = a && b || a;
| ^^^^^^^^^^^ help: it would look like the following: `a`
|
= note: `-D clippy::logic-bug` implied by `-D warnings`
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/booleans.rs:10:18
|
LL | let _ = a && b || a;
| ^
error: this boolean expression can be simplified
--> $DIR/booleans.rs:12:13
|
LL | let _ = !true;
| ^^^^^ help: try: `false`
|
= note: `-D clippy::nonminimal-bool` implied by `-D warnings`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:13:13
|
LL | let _ = !false;
| ^^^^^^ help: try: `true`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:14:13
|
LL | let _ = !!a;
| ^^^ help: try: `a`
error: this boolean expression contains a logic bug
--> $DIR/booleans.rs:15:13
|
LL | let _ = false && a;
| ^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/booleans.rs:15:22
|
LL | let _ = false && a;
| ^
error: this boolean expression can be simplified
--> $DIR/booleans.rs:16:13
|
LL | let _ = false || a;
| ^^^^^^^^^^ help: try: `a`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:21:13
|
LL | let _ = !(!a && b);
| ^^^^^^^^^^ help: try: `!b || a`
error: this boolean expression contains a logic bug
--> $DIR/booleans.rs:31:13
|
LL | let _ = a == b && a != b;
| ^^^^^^^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/booleans.rs:31:13
|
LL | let _ = a == b && a != b;
| ^^^^^^
error: this boolean expression can be simplified
--> $DIR/booleans.rs:32:13
|
LL | let _ = a == b && c == 5 && a == b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = a == b && c == 5;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(c != 5 || a != b);
| ^^^^^^^^^^^^^^^^^^^
error: this boolean expression can be simplified
--> $DIR/booleans.rs:33:13
|
LL | let _ = a == b && c == 5 && b == a;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = a == b && c == 5;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(c != 5 || a != b);
| ^^^^^^^^^^^^^^^^^^^
error: this boolean expression contains a logic bug
--> $DIR/booleans.rs:34:13
|
LL | let _ = a < b && a >= b;
| ^^^^^^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/booleans.rs:34:13
|
LL | let _ = a < b && a >= b;
| ^^^^^
error: this boolean expression contains a logic bug
--> $DIR/booleans.rs:35:13
|
LL | let _ = a > b && a <= b;
| ^^^^^^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/booleans.rs:35:13
|
LL | let _ = a > b && a <= b;
| ^^^^^
error: this boolean expression can be simplified
--> $DIR/booleans.rs:37:13
|
LL | let _ = a != b || !(a != b || c == d);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = c != d || a != b;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(a == b && c == d);
| ^^^^^^^^^^^^^^^^^^^
error: this boolean expression can be simplified
--> $DIR/booleans.rs:45:13
|
LL | let _ = !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:47:13
|
LL | let _ = !a.is_none();
| ^^^^^^^^^^^^ help: try: `a.is_some()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:49:13
|
LL | let _ = !b.is_err();
| ^^^^^^^^^^^ help: try: `b.is_ok()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:51:13
|
LL | let _ = !b.is_ok();
| ^^^^^^^^^^ help: try: `b.is_err()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:53:13
|
LL | let _ = !(a.is_some() && !c);
| ^^^^^^^^^^^^^^^^^^^^ help: try: `c || a.is_none()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:54:26
|
LL | let _ = !(!c ^ c) || !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:55:25
|
LL | let _ = (!c ^ c) || !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:56:23
|
LL | let _ = !c ^ c || !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:128:8
|
LL | if !res.is_ok() {}
| ^^^^^^^^^^^^ help: try: `res.is_err()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:129:8
|
LL | if !res.is_err() {}
| ^^^^^^^^^^^^^ help: try: `res.is_ok()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:132:8
|
LL | if !res.is_some() {}
| ^^^^^^^^^^^^^^ help: try: `res.is_none()`
error: this boolean expression can be simplified
--> $DIR/booleans.rs:133:8
|
LL | if !res.is_none() {}
| ^^^^^^^^^^^^^^ help: try: `res.is_some()`
error: aborting due to 25 previous errors

26
tests/ui/logic_bug.rs Normal file
View File

@ -0,0 +1,26 @@
#![allow(unused, clippy::many_single_char_names)]
#![warn(clippy::logic_bug)]
fn main() {
let a: bool = unimplemented!();
let b: bool = unimplemented!();
let c: bool = unimplemented!();
let d: bool = unimplemented!();
let e: bool = unimplemented!();
let _ = a && b || a;
let _ = !(a && b);
let _ = false && a;
// don't lint on cfgs
let _ = cfg!(you_shall_not_not_pass) && a;
let _ = a || !b || !c || !d || !e;
let _ = !(a && b || c);
}
fn equality_stuff() {
let a: i32 = unimplemented!();
let b: i32 = unimplemented!();
let _ = a == b && a != b;
let _ = a < b && a >= b;
let _ = a > b && a <= b;
let _ = a > b && a == b;
}

63
tests/ui/logic_bug.stderr Normal file
View File

@ -0,0 +1,63 @@
error: this boolean expression contains a logic bug
--> $DIR/logic_bug.rs:10:13
|
LL | let _ = a && b || a;
| ^^^^^^^^^^^ help: it would look like the following: `a`
|
= note: `-D clippy::logic-bug` implied by `-D warnings`
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/logic_bug.rs:10:18
|
LL | let _ = a && b || a;
| ^
error: this boolean expression contains a logic bug
--> $DIR/logic_bug.rs:12:13
|
LL | let _ = false && a;
| ^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/logic_bug.rs:12:22
|
LL | let _ = false && a;
| ^
error: this boolean expression contains a logic bug
--> $DIR/logic_bug.rs:22:13
|
LL | let _ = a == b && a != b;
| ^^^^^^^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/logic_bug.rs:22:13
|
LL | let _ = a == b && a != b;
| ^^^^^^
error: this boolean expression contains a logic bug
--> $DIR/logic_bug.rs:23:13
|
LL | let _ = a < b && a >= b;
| ^^^^^^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/logic_bug.rs:23:13
|
LL | let _ = a < b && a >= b;
| ^^^^^
error: this boolean expression contains a logic bug
--> $DIR/logic_bug.rs:24:13
|
LL | let _ = a > b && a <= b;
| ^^^^^^^^^^^^^^^ help: it would look like the following: `false`
|
help: this expression can be optimized out by applying boolean operations to the outer expression
--> $DIR/logic_bug.rs:24:13
|
LL | let _ = a > b && a <= b;
| ^^^^^
error: aborting due to 5 previous errors

View File

@ -0,0 +1,52 @@
#![allow(unused, clippy::many_single_char_names)]
#![warn(clippy::nonminimal_bool)]
fn main() {
let a: bool = unimplemented!();
let b: bool = unimplemented!();
let c: bool = unimplemented!();
let d: bool = unimplemented!();
let e: bool = unimplemented!();
let _ = !true;
let _ = !false;
let _ = !!a;
let _ = false || a;
// don't lint on cfgs
let _ = cfg!(you_shall_not_not_pass) && a;
let _ = a || !b || !c || !d || !e;
let _ = !(!a && b);
let _ = !(!a || b);
let _ = !a && !(b && c);
}
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;
let _ = a == b || c == 5 || a == b;
let _ = a == b && c == 5 && b == a;
let _ = a != b || !(a != b || c == d);
let _ = a != b && !(a != b && c == d);
}
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 {}
}

View File

@ -0,0 +1,111 @@
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:10:13
|
LL | let _ = !true;
| ^^^^^ help: try: `false`
|
= note: `-D clippy::nonminimal-bool` implied by `-D warnings`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:11:13
|
LL | let _ = !false;
| ^^^^^^ help: try: `true`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:12:13
|
LL | let _ = !!a;
| ^^^ help: try: `a`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:13:13
|
LL | let _ = false || a;
| ^^^^^^^^^^ help: try: `a`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:17:13
|
LL | let _ = !(!a && b);
| ^^^^^^^^^^ help: try: `a || !b`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:18:13
|
LL | let _ = !(!a || b);
| ^^^^^^^^^^ help: try: `a && !b`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:19:13
|
LL | let _ = !a && !(b && c);
| ^^^^^^^^^^^^^^^ help: try: `!(a || b && c)`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:27:13
|
LL | let _ = a == b && c == 5 && a == b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = a == b && c == 5;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(a != b || c != 5);
| ^^^^^^^^^^^^^^^^^^^
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:28:13
|
LL | let _ = a == b || c == 5 || a == b;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = a == b || c == 5;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(a != b && c != 5);
| ^^^^^^^^^^^^^^^^^^^
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:29:13
|
LL | let _ = a == b && c == 5 && b == a;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = a == b && c == 5;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(a != b || c != 5);
| ^^^^^^^^^^^^^^^^^^^
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:30:13
|
LL | let _ = a != b || !(a != b || c == d);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = a != b || c != d;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(a == b && c == d);
| ^^^^^^^^^^^^^^^^^^^
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool.rs:31:13
|
LL | let _ = a != b && !(a != b && c == d);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | let _ = a != b && c != d;
| ^^^^^^^^^^^^^^^^
LL | let _ = !(a == b || c == d);
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 12 previous errors

View File

@ -1,43 +1,6 @@
#![warn(clippy::nonminimal_bool, clippy::logic_bug)]
#![allow(unused, clippy::many_single_char_names)]
#![warn(clippy::nonminimal_bool)]
#[allow(unused, clippy::many_single_char_names)]
fn main() {
let a: bool = unimplemented!();
let b: bool = unimplemented!();
let c: bool = unimplemented!();
let d: bool = unimplemented!();
let e: bool = unimplemented!();
let _ = a && b || a;
let _ = !(a && b);
let _ = !true;
let _ = !false;
let _ = !!a;
let _ = false && a;
let _ = false || a;
// don't lint on cfgs
let _ = cfg!(you_shall_not_not_pass) && a;
let _ = a || !b || !c || !d || !e;
let _ = !(a && b || c);
let _ = !(!a && b);
}
#[allow(unused, clippy::many_single_char_names)]
fn equality_stuff() {
let a: i32 = unimplemented!();
let b: i32 = unimplemented!();
let c: i32 = unimplemented!();
let d: i32 = unimplemented!();
let e: i32 = unimplemented!();
let _ = a == b && a != b;
let _ = a == b && c == 5 && a == b;
let _ = a == b && c == 5 && b == a;
let _ = a < b && a >= b;
let _ = a > b && a <= b;
let _ = a > b && a == b;
let _ = a != b || !(a != b || c == d);
}
#[allow(unused, clippy::many_single_char_names)]
fn methods_with_negation() {
let a: Option<i32> = unimplemented!();
let b: Result<i32, i32> = unimplemented!();
@ -51,6 +14,7 @@ fn methods_with_negation() {
let _ = !b.is_ok();
let c = false;
let _ = !(a.is_some() && !c);
let _ = !(a.is_some() || !c);
let _ = !(!c ^ c) || !a.is_some();
let _ = (!c ^ c) || !a.is_some();
let _ = !c ^ c || !a.is_some();
@ -143,22 +107,4 @@ fn dont_warn_for_negated_partial_ord_comparison() {
let _ = !(a >= b);
}
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 main() {}

View File

@ -0,0 +1,82 @@
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:8:13
|
LL | let _ = !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
|
= note: `-D clippy::nonminimal-bool` implied by `-D warnings`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:10:13
|
LL | let _ = !a.is_none();
| ^^^^^^^^^^^^ help: try: `a.is_some()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:12:13
|
LL | let _ = !b.is_err();
| ^^^^^^^^^^^ help: try: `b.is_ok()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:14:13
|
LL | let _ = !b.is_ok();
| ^^^^^^^^^^ help: try: `b.is_err()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:16:13
|
LL | let _ = !(a.is_some() && !c);
| ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() || c`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:17:13
|
LL | let _ = !(a.is_some() || !c);
| ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() && c`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:18:26
|
LL | let _ = !(!c ^ c) || !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:19:25
|
LL | let _ = (!c ^ c) || !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:20:23
|
LL | let _ = !c ^ c || !a.is_some();
| ^^^^^^^^^^^^ help: try: `a.is_none()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:92:8
|
LL | if !res.is_ok() {}
| ^^^^^^^^^^^^ help: try: `res.is_err()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:93:8
|
LL | if !res.is_err() {}
| ^^^^^^^^^^^^^ help: try: `res.is_ok()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:96:8
|
LL | if !res.is_some() {}
| ^^^^^^^^^^^^^^ help: try: `res.is_none()`
error: this boolean expression can be simplified
--> $DIR/nonminimal_bool_methods.rs:97:8
|
LL | if !res.is_none() {}
| ^^^^^^^^^^^^^^ help: try: `res.is_some()`
error: aborting due to 13 previous errors