about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2025-04-17 02:15:21 +0000
committerGitHub <noreply@github.com>2025-04-17 02:15:21 +0000
commit949b3bcfb50d90888a21366d81577ffbd1779405 (patch)
treedef1896b2dbb4e7971a10da15ef78b7f5932782f /tests
parent222660bb36f3ea6a2e43221373269d5bddbf68e6 (diff)
parent0bfcd0d3bc02d138168b08688fc902894496173c (diff)
downloadrust-949b3bcfb50d90888a21366d81577ffbd1779405.tar.gz
rust-949b3bcfb50d90888a21366d81577ffbd1779405.zip
`bool_to_int_with_if`: properly handle macros (#14629)
- Do not replace macro results in then/else branches
- Extract condition snippet from the right context
- Make suggestion `MaybeIncorrect` if it would lead to losing comments

changelog: [`bool_to_int_with_if`]: properly handle macros

Fixes rust-lang/rust-clippy#14628
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/bool_to_int_with_if.fixed24
-rw-r--r--tests/ui/bool_to_int_with_if.rs24
-rw-r--r--tests/ui/bool_to_int_with_if.stderr18
3 files changed, 65 insertions, 1 deletions
diff --git a/tests/ui/bool_to_int_with_if.fixed b/tests/ui/bool_to_int_with_if.fixed
index 0080801d46b..ed6141244b4 100644
--- a/tests/ui/bool_to_int_with_if.fixed
+++ b/tests/ui/bool_to_int_with_if.fixed
@@ -117,3 +117,27 @@ fn if_let(a: Enum, b: Enum) {
         0
     };
 }
+
+fn issue14628() {
+    macro_rules! mac {
+        (if $cond:expr, $then:expr, $else:expr) => {
+            if $cond { $then } else { $else }
+        };
+        (zero) => {
+            0
+        };
+        (one) => {
+            1
+        };
+    }
+
+    let _ = i32::from(dbg!(4 > 0));
+    //~^ bool_to_int_with_if
+
+    let _ = dbg!(i32::from(4 > 0));
+    //~^ bool_to_int_with_if
+
+    let _ = mac!(if 4 > 0, 1, 0);
+    let _ = if 4 > 0 { mac!(one) } else { 0 };
+    let _ = if 4 > 0 { 1 } else { mac!(zero) };
+}
diff --git a/tests/ui/bool_to_int_with_if.rs b/tests/ui/bool_to_int_with_if.rs
index 72c7e2c71c5..3f1f1c766e4 100644
--- a/tests/ui/bool_to_int_with_if.rs
+++ b/tests/ui/bool_to_int_with_if.rs
@@ -157,3 +157,27 @@ fn if_let(a: Enum, b: Enum) {
         0
     };
 }
+
+fn issue14628() {
+    macro_rules! mac {
+        (if $cond:expr, $then:expr, $else:expr) => {
+            if $cond { $then } else { $else }
+        };
+        (zero) => {
+            0
+        };
+        (one) => {
+            1
+        };
+    }
+
+    let _ = if dbg!(4 > 0) { 1 } else { 0 };
+    //~^ bool_to_int_with_if
+
+    let _ = dbg!(if 4 > 0 { 1 } else { 0 });
+    //~^ bool_to_int_with_if
+
+    let _ = mac!(if 4 > 0, 1, 0);
+    let _ = if 4 > 0 { mac!(one) } else { 0 };
+    let _ = if 4 > 0 { 1 } else { mac!(zero) };
+}
diff --git a/tests/ui/bool_to_int_with_if.stderr b/tests/ui/bool_to_int_with_if.stderr
index 415e80f8d73..94089bc6dc8 100644
--- a/tests/ui/bool_to_int_with_if.stderr
+++ b/tests/ui/bool_to_int_with_if.stderr
@@ -114,5 +114,21 @@ LL |     if a { 1 } else { 0 }
    |
    = note: `a as u8` or `a.into()` can also be valid options
 
-error: aborting due to 9 previous errors
+error: boolean to int conversion using if
+  --> tests/ui/bool_to_int_with_if.rs:174:13
+   |
+LL |     let _ = if dbg!(4 > 0) { 1 } else { 0 };
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `i32::from(dbg!(4 > 0))`
+   |
+   = note: `dbg!(4 > 0) as i32` or `dbg!(4 > 0).into()` can also be valid options
+
+error: boolean to int conversion using if
+  --> tests/ui/bool_to_int_with_if.rs:177:18
+   |
+LL |     let _ = dbg!(if 4 > 0 { 1 } else { 0 });
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `i32::from(4 > 0)`
+   |
+   = note: `(4 > 0) as i32` or `(4 > 0).into()` can also be valid options
+
+error: aborting due to 11 previous errors