about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-08-22 19:11:28 +0000
committerGitHub <noreply@github.com>2025-08-22 19:11:28 +0000
commitdf0499a5edbd668b7ba4c8666c11bb8a063fb3f7 (patch)
tree82e5c580560c29baebe4bac5c50ac5fbfce26390
parent4bb6a0ebe08068e7b640370a89e5c3417d9bb948 (diff)
parent7697b611543031a83e242dc3904debffb8023829 (diff)
downloadrust-df0499a5edbd668b7ba4c8666c11bb8a063fb3f7.tar.gz
rust-df0499a5edbd668b7ba4c8666c11bb8a063fb3f7.zip
bool_comparison: fix incorrect suggestion with `>`/`<` and macros (#15513)
Fixes https://github.com/rust-lang/rust-clippy/issues/15497

changelog: [`bool_comparison`]: fix incorrect suggestion with `>`/`<`
and macros
-rw-r--r--clippy_lints/src/needless_bool.rs5
-rw-r--r--tests/ui/bool_comparison.fixed19
-rw-r--r--tests/ui/bool_comparison.rs19
-rw-r--r--tests/ui/bool_comparison.stderr36
4 files changed, 59 insertions, 20 deletions
diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs
index 6ae26156bc4..d8d66837405 100644
--- a/clippy_lints/src/needless_bool.rs
+++ b/clippy_lints/src/needless_bool.rs
@@ -381,8 +381,9 @@ fn check_comparison<'a, 'tcx>(
                     suggest_bool_comparison(cx, binop_span, left_side, applicability, m, h);
                 }),
                 (None, None) => no_literal.map_or((), |(h, m)| {
-                    let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
-                    let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
+                    let left_side = Sugg::hir_with_context(cx, left_side, binop_span.ctxt(), "..", &mut applicability);
+                    let right_side =
+                        Sugg::hir_with_context(cx, right_side, binop_span.ctxt(), "..", &mut applicability);
                     span_lint_and_sugg(
                         cx,
                         BOOL_COMPARISON,
diff --git a/tests/ui/bool_comparison.fixed b/tests/ui/bool_comparison.fixed
index 166abbe549c..e8edc33b56a 100644
--- a/tests/ui/bool_comparison.fixed
+++ b/tests/ui/bool_comparison.fixed
@@ -91,7 +91,6 @@ fn main() {
     };
 }
 
-#[allow(dead_code)]
 fn issue3703() {
     struct Foo;
     impl PartialEq<bool> for Foo {
@@ -127,7 +126,6 @@ fn issue3703() {
     if false < Foo {}
 }
 
-#[allow(dead_code)]
 fn issue4983() {
     let a = true;
     let b = false;
@@ -157,7 +155,6 @@ fn func() -> bool {
     true
 }
 
-#[allow(dead_code)]
 fn issue3973() {
     // ok, don't lint on `cfg` invocation
     if false == cfg!(feature = "debugging") {}
@@ -199,3 +196,19 @@ fn issue9907() {
     let _ = ((1 < 2) != m!(func)) as usize;
     //~^ bool_comparison
 }
+
+fn issue15497() {
+    fn func() -> bool {
+        true
+    }
+
+    fn foo(x: bool) -> bool {
+        x & !m!(func)
+        //~^ bool_comparison
+    }
+
+    fn bar(x: bool) -> bool {
+        !x & m!(func)
+        //~^ bool_comparison
+    }
+}
diff --git a/tests/ui/bool_comparison.rs b/tests/ui/bool_comparison.rs
index 7265c2b4c5a..18004639029 100644
--- a/tests/ui/bool_comparison.rs
+++ b/tests/ui/bool_comparison.rs
@@ -91,7 +91,6 @@ fn main() {
     };
 }
 
-#[allow(dead_code)]
 fn issue3703() {
     struct Foo;
     impl PartialEq<bool> for Foo {
@@ -127,7 +126,6 @@ fn issue3703() {
     if false < Foo {}
 }
 
-#[allow(dead_code)]
 fn issue4983() {
     let a = true;
     let b = false;
@@ -157,7 +155,6 @@ fn func() -> bool {
     true
 }
 
-#[allow(dead_code)]
 fn issue3973() {
     // ok, don't lint on `cfg` invocation
     if false == cfg!(feature = "debugging") {}
@@ -199,3 +196,19 @@ fn issue9907() {
     let _ = ((1 < 2) == !m!(func)) as usize;
     //~^ bool_comparison
 }
+
+fn issue15497() {
+    fn func() -> bool {
+        true
+    }
+
+    fn foo(x: bool) -> bool {
+        x > m!(func)
+        //~^ bool_comparison
+    }
+
+    fn bar(x: bool) -> bool {
+        x < m!(func)
+        //~^ bool_comparison
+    }
+}
diff --git a/tests/ui/bool_comparison.stderr b/tests/ui/bool_comparison.stderr
index ddbb9ff78ed..a398870e7ee 100644
--- a/tests/ui/bool_comparison.stderr
+++ b/tests/ui/bool_comparison.stderr
@@ -86,70 +86,82 @@ LL |     if x > y {
    |        ^^^^^ help: try simplifying it as shown: `x & !y`
 
 error: this comparison might be written more concisely
-  --> tests/ui/bool_comparison.rs:135:8
+  --> tests/ui/bool_comparison.rs:133:8
    |
 LL |     if a == !b {};
    |        ^^^^^^^ help: try simplifying it as shown: `a != b`
 
 error: this comparison might be written more concisely
-  --> tests/ui/bool_comparison.rs:137:8
+  --> tests/ui/bool_comparison.rs:135:8
    |
 LL |     if !a == b {};
    |        ^^^^^^^ help: try simplifying it as shown: `a != b`
 
 error: this comparison might be written more concisely
-  --> tests/ui/bool_comparison.rs:142:8
+  --> tests/ui/bool_comparison.rs:140:8
    |
 LL |     if b == !a {};
    |        ^^^^^^^ help: try simplifying it as shown: `b != a`
 
 error: this comparison might be written more concisely
-  --> tests/ui/bool_comparison.rs:144:8
+  --> tests/ui/bool_comparison.rs:142:8
    |
 LL |     if !b == a {};
    |        ^^^^^^^ help: try simplifying it as shown: `b != a`
 
 error: equality checks against false can be replaced by a negation
-  --> tests/ui/bool_comparison.rs:169:8
+  --> tests/ui/bool_comparison.rs:166:8
    |
 LL |     if false == m!(func) {}
    |        ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
 
 error: equality checks against false can be replaced by a negation
-  --> tests/ui/bool_comparison.rs:171:8
+  --> tests/ui/bool_comparison.rs:168:8
    |
 LL |     if m!(func) == false {}
    |        ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
 
 error: equality checks against true are unnecessary
-  --> tests/ui/bool_comparison.rs:173:8
+  --> tests/ui/bool_comparison.rs:170:8
    |
 LL |     if true == m!(func) {}
    |        ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`
 
 error: equality checks against true are unnecessary
-  --> tests/ui/bool_comparison.rs:175:8
+  --> tests/ui/bool_comparison.rs:172:8
    |
 LL |     if m!(func) == true {}
    |        ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`
 
 error: equality checks against false can be replaced by a negation
-  --> tests/ui/bool_comparison.rs:193:14
+  --> tests/ui/bool_comparison.rs:190:14
    |
 LL |     let _ = ((1 < 2) == false) as usize;
    |              ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `1 >= 2`
 
 error: equality checks against false can be replaced by a negation
-  --> tests/ui/bool_comparison.rs:195:14
+  --> tests/ui/bool_comparison.rs:192:14
    |
 LL |     let _ = (false == m!(func)) as usize;
    |              ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
 
 error: this comparison might be written more concisely
-  --> tests/ui/bool_comparison.rs:199:14
+  --> tests/ui/bool_comparison.rs:196:14
    |
 LL |     let _ = ((1 < 2) == !m!(func)) as usize;
    |              ^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `(1 < 2) != m!(func)`
 
-error: aborting due to 25 previous errors
+error: order comparisons between booleans can be simplified
+  --> tests/ui/bool_comparison.rs:206:9
+   |
+LL |         x > m!(func)
+   |         ^^^^^^^^^^^^ help: try simplifying it as shown: `x & !m!(func)`
+
+error: order comparisons between booleans can be simplified
+  --> tests/ui/bool_comparison.rs:211:9
+   |
+LL |         x < m!(func)
+   |         ^^^^^^^^^^^^ help: try simplifying it as shown: `!x & m!(func)`
+
+error: aborting due to 27 previous errors