about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-14 19:56:12 +0000
committerbors <bors@rust-lang.org>2022-09-14 19:56:12 +0000
commitbae4699a9f6afc1c3c2e295e2ffa7d309f96f84d (patch)
treec3434ddecda069bc3abcb7c2efd48678df97eb32 /tests
parent2ddbc86bef837b1072159c020c35940ce52ae696 (diff)
parentdd97c1ed205f47d13dc5bc482a73c0c76383d159 (diff)
downloadrust-bae4699a9f6afc1c3c2e295e2ffa7d309f96f84d.tar.gz
rust-bae4699a9f6afc1c3c2e295e2ffa7d309f96f84d.zip
Auto merge of #9476 - Xaeroxe:bool-to-int-inverted, r=xFrednet
`bool_to_int_with_if` inverse case patch

Enhances `bool_to_int_with_if` such that it can also catch an inverse bool int conversion scenario, and makes the right suggestion for converting to int with a prefixed negation operator.

changelog: [`bool_to_int_with_if`]: Now correctly detects the inverse case, `if bool { 0 } else { 1 }`
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/bool_to_int_with_if.fixed8
-rw-r--r--tests/ui/bool_to_int_with_if.rs14
-rw-r--r--tests/ui/bool_to_int_with_if.stderr41
-rw-r--r--tests/ui/len_without_is_empty.rs2
4 files changed, 55 insertions, 10 deletions
diff --git a/tests/ui/bool_to_int_with_if.fixed b/tests/ui/bool_to_int_with_if.fixed
index 9c1098dc4c1..2c8339cdd7f 100644
--- a/tests/ui/bool_to_int_with_if.fixed
+++ b/tests/ui/bool_to_int_with_if.fixed
@@ -14,6 +14,7 @@ fn main() {
     // precedence
     i32::from(a);
     i32::from(!a);
+    i32::from(!a);
     i32::from(a || b);
     i32::from(cond(a, b));
     i32::from(x + y < 4);
@@ -21,7 +22,12 @@ fn main() {
     // if else if
     if a {
         123
-    } else {i32::from(b)};
+    } else { i32::from(b) };
+
+    // if else if inverted
+    if a {
+        123
+    } else { i32::from(!b) };
 
     // Shouldn't lint
 
diff --git a/tests/ui/bool_to_int_with_if.rs b/tests/ui/bool_to_int_with_if.rs
index 0c967dac6e2..5d9496f0177 100644
--- a/tests/ui/bool_to_int_with_if.rs
+++ b/tests/ui/bool_to_int_with_if.rs
@@ -17,6 +17,11 @@ fn main() {
     } else {
         0
     };
+    if a {
+        0
+    } else {
+        1
+    };
     if !a {
         1
     } else {
@@ -47,6 +52,15 @@ fn main() {
         0
     };
 
+    // if else if inverted
+    if a {
+        123
+    } else if b {
+        0
+    } else {
+        1
+    };
+
     // Shouldn't lint
 
     if a {
diff --git a/tests/ui/bool_to_int_with_if.stderr b/tests/ui/bool_to_int_with_if.stderr
index 8647a9cffbe..e695440f668 100644
--- a/tests/ui/bool_to_int_with_if.stderr
+++ b/tests/ui/bool_to_int_with_if.stderr
@@ -14,6 +14,18 @@ LL | |     };
 error: boolean to int conversion using if
   --> $DIR/bool_to_int_with_if.rs:20:5
    |
+LL | /     if a {
+LL | |         0
+LL | |     } else {
+LL | |         1
+LL | |     };
+   | |_____^ help: replace with from: `i32::from(!a)`
+   |
+   = note: `!a as i32` or `(!a).into()` can also be valid options
+
+error: boolean to int conversion using if
+  --> $DIR/bool_to_int_with_if.rs:25:5
+   |
 LL | /     if !a {
 LL | |         1
 LL | |     } else {
@@ -21,10 +33,10 @@ LL | |         0
 LL | |     };
    | |_____^ help: replace with from: `i32::from(!a)`
    |
-   = note: `!a as i32` or `!a.into()` can also be valid options
+   = note: `!a as i32` or `(!a).into()` can also be valid options
 
 error: boolean to int conversion using if
-  --> $DIR/bool_to_int_with_if.rs:25:5
+  --> $DIR/bool_to_int_with_if.rs:30:5
    |
 LL | /     if a || b {
 LL | |         1
@@ -36,7 +48,7 @@ LL | |     };
    = note: `(a || b) as i32` or `(a || b).into()` can also be valid options
 
 error: boolean to int conversion using if
-  --> $DIR/bool_to_int_with_if.rs:30:5
+  --> $DIR/bool_to_int_with_if.rs:35:5
    |
 LL | /     if cond(a, b) {
 LL | |         1
@@ -48,7 +60,7 @@ LL | |     };
    = note: `cond(a, b) as i32` or `cond(a, b).into()` can also be valid options
 
 error: boolean to int conversion using if
-  --> $DIR/bool_to_int_with_if.rs:35:5
+  --> $DIR/bool_to_int_with_if.rs:40:5
    |
 LL | /     if x + y < 4 {
 LL | |         1
@@ -60,7 +72,7 @@ LL | |     };
    = note: `(x + y < 4) as i32` or `(x + y < 4).into()` can also be valid options
 
 error: boolean to int conversion using if
-  --> $DIR/bool_to_int_with_if.rs:44:12
+  --> $DIR/bool_to_int_with_if.rs:49:12
    |
 LL |       } else if b {
    |  ____________^
@@ -68,17 +80,30 @@ LL | |         1
 LL | |     } else {
 LL | |         0
 LL | |     };
-   | |_____^ help: replace with from: `{i32::from(b)}`
+   | |_____^ help: replace with from: `{ i32::from(b) }`
    |
    = note: `b as i32` or `b.into()` can also be valid options
 
 error: boolean to int conversion using if
-  --> $DIR/bool_to_int_with_if.rs:102:5
+  --> $DIR/bool_to_int_with_if.rs:58:12
+   |
+LL |       } else if b {
+   |  ____________^
+LL | |         0
+LL | |     } else {
+LL | |         1
+LL | |     };
+   | |_____^ help: replace with from: `{ i32::from(!b) }`
+   |
+   = note: `!b as i32` or `(!b).into()` can also be valid options
+
+error: boolean to int conversion using if
+  --> $DIR/bool_to_int_with_if.rs:116:5
    |
 LL |     if a { 1 } else { 0 }
    |     ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)`
    |
    = note: `a as u8` or `a.into()` can also be valid options
 
-error: aborting due to 7 previous errors
+error: aborting due to 9 previous errors
 
diff --git a/tests/ui/len_without_is_empty.rs b/tests/ui/len_without_is_empty.rs
index 1e938e72b57..78397c2af34 100644
--- a/tests/ui/len_without_is_empty.rs
+++ b/tests/ui/len_without_is_empty.rs
@@ -274,7 +274,7 @@ impl AsyncLen {
     }
 
     pub async fn len(&self) -> usize {
-        if self.async_task().await { 0 } else { 1 }
+        usize::from(!self.async_task().await)
     }
 
     pub async fn is_empty(&self) -> bool {