From 73e525019db9c27a39134dc0cf965724ac6b6351 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 14 Jan 2020 07:08:45 +0900 Subject: [PATCH 1/2] Fix the ordering on `nonminimal_bool` --- clippy_lints/src/booleans.rs | 2 +- tests/ui/booleans.rs | 4 ++ tests/ui/booleans.stderr | 92 +++++++++++++++++++++++++----------- 3 files changed, 70 insertions(+), 28 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 12f11792716..60e62542a72 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -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(" || "); } diff --git a/tests/ui/booleans.rs b/tests/ui/booleans.rs index ece20fb1eab..6109ee1736d 100644 --- a/tests/ui/booleans.rs +++ b/tests/ui/booleans.rs @@ -19,6 +19,7 @@ fn main() { let _ = a || !b || !c || !d || !e; let _ = !(a && b || c); let _ = !(!a && b); + let _ = !(!a || b); } #[allow(unused, clippy::many_single_char_names)] @@ -30,11 +31,13 @@ fn equality_stuff() { let e: i32 = unimplemented!(); let _ = a == b && a != b; 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; let _ = a > b && a <= b; let _ = a > b && a == b; let _ = a != b || !(a != b || c == d); + let _ = a != b && !(a != b && c == d); } #[allow(unused, clippy::many_single_char_names)] @@ -51,6 +54,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(); diff --git a/tests/ui/booleans.stderr b/tests/ui/booleans.stderr index b83df6f8138..05ec6c03147 100644 --- a/tests/ui/booleans.stderr +++ b/tests/ui/booleans.stderr @@ -53,22 +53,28 @@ error: this boolean expression can be simplified --> $DIR/booleans.rs:21:13 | LL | let _ = !(!a && b); - | ^^^^^^^^^^ help: try: `!b || a` + | ^^^^^^^^^^ help: try: `a || !b` + +error: this boolean expression can be simplified + --> $DIR/booleans.rs:22:13 + | +LL | let _ = !(!a || b); + | ^^^^^^^^^^ help: try: `a && !b` error: this boolean expression contains a logic bug - --> $DIR/booleans.rs:31:13 + --> $DIR/booleans.rs:32: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 + --> $DIR/booleans.rs:32:13 | LL | let _ = a == b && a != b; | ^^^^^^ error: this boolean expression can be simplified - --> $DIR/booleans.rs:32:13 + --> $DIR/booleans.rs:33:13 | LL | let _ = a == b && c == 5 && a == b; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -77,11 +83,24 @@ help: try | LL | let _ = a == b && c == 5; | ^^^^^^^^^^^^^^^^ -LL | let _ = !(c != 5 || a != b); +LL | let _ = !(a != b || c != 5); | ^^^^^^^^^^^^^^^^^^^ error: this boolean expression can be simplified - --> $DIR/booleans.rs:33:13 + --> $DIR/booleans.rs:34: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/booleans.rs:35:13 | LL | let _ = a == b && c == 5 && b == a; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -90,117 +109,136 @@ help: try | LL | let _ = a == b && c == 5; | ^^^^^^^^^^^^^^^^ -LL | let _ = !(c != 5 || a != b); +LL | let _ = !(a != b || c != 5); | ^^^^^^^^^^^^^^^^^^^ error: this boolean expression contains a logic bug - --> $DIR/booleans.rs:34:13 + --> $DIR/booleans.rs:36: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 + --> $DIR/booleans.rs:36:13 | LL | let _ = a < b && a >= b; | ^^^^^ error: this boolean expression contains a logic bug - --> $DIR/booleans.rs:35:13 + --> $DIR/booleans.rs:37: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 + --> $DIR/booleans.rs:37:13 | LL | let _ = a > b && a <= b; | ^^^^^ error: this boolean expression can be simplified - --> $DIR/booleans.rs:37:13 + --> $DIR/booleans.rs:39:13 | LL | let _ = a != b || !(a != b || c == d); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: try | -LL | let _ = c != d || a != b; +LL | let _ = a != b || c != d; | ^^^^^^^^^^^^^^^^ LL | let _ = !(a == b && c == d); | ^^^^^^^^^^^^^^^^^^^ error: this boolean expression can be simplified - --> $DIR/booleans.rs:45:13 + --> $DIR/booleans.rs:40: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/booleans.rs:48:13 | LL | let _ = !a.is_some(); | ^^^^^^^^^^^^ help: try: `a.is_none()` error: this boolean expression can be simplified - --> $DIR/booleans.rs:47:13 + --> $DIR/booleans.rs:50:13 | LL | let _ = !a.is_none(); | ^^^^^^^^^^^^ help: try: `a.is_some()` error: this boolean expression can be simplified - --> $DIR/booleans.rs:49:13 + --> $DIR/booleans.rs:52:13 | LL | let _ = !b.is_err(); | ^^^^^^^^^^^ help: try: `b.is_ok()` error: this boolean expression can be simplified - --> $DIR/booleans.rs:51:13 + --> $DIR/booleans.rs:54:13 | LL | let _ = !b.is_ok(); | ^^^^^^^^^^ help: try: `b.is_err()` error: this boolean expression can be simplified - --> $DIR/booleans.rs:53:13 + --> $DIR/booleans.rs:56:13 | LL | let _ = !(a.is_some() && !c); - | ^^^^^^^^^^^^^^^^^^^^ help: try: `c || a.is_none()` + | ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() || c` error: this boolean expression can be simplified - --> $DIR/booleans.rs:54:26 + --> $DIR/booleans.rs:57:13 + | +LL | let _ = !(a.is_some() || !c); + | ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() && c` + +error: this boolean expression can be simplified + --> $DIR/booleans.rs:58: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 + --> $DIR/booleans.rs:59: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 + --> $DIR/booleans.rs:60: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 + --> $DIR/booleans.rs:132:8 | LL | if !res.is_ok() {} | ^^^^^^^^^^^^ help: try: `res.is_err()` error: this boolean expression can be simplified - --> $DIR/booleans.rs:129:8 + --> $DIR/booleans.rs:133:8 | LL | if !res.is_err() {} | ^^^^^^^^^^^^^ help: try: `res.is_ok()` error: this boolean expression can be simplified - --> $DIR/booleans.rs:132:8 + --> $DIR/booleans.rs:136:8 | LL | if !res.is_some() {} | ^^^^^^^^^^^^^^ help: try: `res.is_none()` error: this boolean expression can be simplified - --> $DIR/booleans.rs:133:8 + --> $DIR/booleans.rs:137:8 | LL | if !res.is_none() {} | ^^^^^^^^^^^^^^ help: try: `res.is_some()` -error: aborting due to 25 previous errors +error: aborting due to 29 previous errors From 3885033e5f161982766d75dee973c4999969ba11 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 14 Jan 2020 07:41:11 +0900 Subject: [PATCH 2/2] Split up `booleans` ui test --- tests/ui/booleans.stderr | 244 ------------------ tests/ui/logic_bug.rs | 26 ++ tests/ui/logic_bug.stderr | 63 +++++ tests/ui/nonminimal_bool.rs | 52 ++++ tests/ui/nonminimal_bool.stderr | 111 ++++++++ ...booleans.rs => nonminimal_bool_methods.rs} | 64 +---- tests/ui/nonminimal_bool_methods.stderr | 82 ++++++ 7 files changed, 337 insertions(+), 305 deletions(-) delete mode 100644 tests/ui/booleans.stderr create mode 100644 tests/ui/logic_bug.rs create mode 100644 tests/ui/logic_bug.stderr create mode 100644 tests/ui/nonminimal_bool.rs create mode 100644 tests/ui/nonminimal_bool.stderr rename tests/ui/{booleans.rs => nonminimal_bool_methods.rs} (60%) create mode 100644 tests/ui/nonminimal_bool_methods.stderr diff --git a/tests/ui/booleans.stderr b/tests/ui/booleans.stderr deleted file mode 100644 index 05ec6c03147..00000000000 --- a/tests/ui/booleans.stderr +++ /dev/null @@ -1,244 +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: `a || !b` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:22:13 - | -LL | let _ = !(!a || b); - | ^^^^^^^^^^ help: try: `a && !b` - -error: this boolean expression contains a logic bug - --> $DIR/booleans.rs:32: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:32:13 - | -LL | let _ = a == b && a != b; - | ^^^^^^ - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:33: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/booleans.rs:34: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/booleans.rs:35: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 contains a logic bug - --> $DIR/booleans.rs:36: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:36:13 - | -LL | let _ = a < b && a >= b; - | ^^^^^ - -error: this boolean expression contains a logic bug - --> $DIR/booleans.rs:37: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:37:13 - | -LL | let _ = a > b && a <= b; - | ^^^^^ - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:39: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/booleans.rs:40: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/booleans.rs:48:13 - | -LL | let _ = !a.is_some(); - | ^^^^^^^^^^^^ help: try: `a.is_none()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:50:13 - | -LL | let _ = !a.is_none(); - | ^^^^^^^^^^^^ help: try: `a.is_some()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:52:13 - | -LL | let _ = !b.is_err(); - | ^^^^^^^^^^^ help: try: `b.is_ok()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:54:13 - | -LL | let _ = !b.is_ok(); - | ^^^^^^^^^^ help: try: `b.is_err()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:56:13 - | -LL | let _ = !(a.is_some() && !c); - | ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() || c` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:57:13 - | -LL | let _ = !(a.is_some() || !c); - | ^^^^^^^^^^^^^^^^^^^^ help: try: `a.is_none() && c` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:58:26 - | -LL | let _ = !(!c ^ c) || !a.is_some(); - | ^^^^^^^^^^^^ help: try: `a.is_none()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:59:25 - | -LL | let _ = (!c ^ c) || !a.is_some(); - | ^^^^^^^^^^^^ help: try: `a.is_none()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:60:23 - | -LL | let _ = !c ^ c || !a.is_some(); - | ^^^^^^^^^^^^ help: try: `a.is_none()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:132:8 - | -LL | if !res.is_ok() {} - | ^^^^^^^^^^^^ help: try: `res.is_err()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:133:8 - | -LL | if !res.is_err() {} - | ^^^^^^^^^^^^^ help: try: `res.is_ok()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:136:8 - | -LL | if !res.is_some() {} - | ^^^^^^^^^^^^^^ help: try: `res.is_none()` - -error: this boolean expression can be simplified - --> $DIR/booleans.rs:137:8 - | -LL | if !res.is_none() {} - | ^^^^^^^^^^^^^^ help: try: `res.is_some()` - -error: aborting due to 29 previous errors - diff --git a/tests/ui/logic_bug.rs b/tests/ui/logic_bug.rs new file mode 100644 index 00000000000..b4163d776e7 --- /dev/null +++ b/tests/ui/logic_bug.rs @@ -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; +} diff --git a/tests/ui/logic_bug.stderr b/tests/ui/logic_bug.stderr new file mode 100644 index 00000000000..8f55e1c8ad8 --- /dev/null +++ b/tests/ui/logic_bug.stderr @@ -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 + diff --git a/tests/ui/nonminimal_bool.rs b/tests/ui/nonminimal_bool.rs new file mode 100644 index 00000000000..7ea154cb9b0 --- /dev/null +++ b/tests/ui/nonminimal_bool.rs @@ -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 {} +} diff --git a/tests/ui/nonminimal_bool.stderr b/tests/ui/nonminimal_bool.stderr new file mode 100644 index 00000000000..d34d106cb2f --- /dev/null +++ b/tests/ui/nonminimal_bool.stderr @@ -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 + diff --git a/tests/ui/booleans.rs b/tests/ui/nonminimal_bool_methods.rs similarity index 60% rename from tests/ui/booleans.rs rename to tests/ui/nonminimal_bool_methods.rs index 6109ee1736d..4de48cd0879 100644 --- a/tests/ui/booleans.rs +++ b/tests/ui/nonminimal_bool_methods.rs @@ -1,46 +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); - 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 || 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); - let _ = a != b && !(a != b && c == d); -} - -#[allow(unused, clippy::many_single_char_names)] fn methods_with_negation() { let a: Option = unimplemented!(); let b: Result = unimplemented!(); @@ -147,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() {} diff --git a/tests/ui/nonminimal_bool_methods.stderr b/tests/ui/nonminimal_bool_methods.stderr new file mode 100644 index 00000000000..a2df889d623 --- /dev/null +++ b/tests/ui/nonminimal_bool_methods.stderr @@ -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 +