about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--tests/ui/implicit_saturating_sub.rs7
-rw-r--r--tests/ui/manual_arithmetic_check-2.rs35
-rw-r--r--tests/ui/manual_arithmetic_check-2.stderr76
-rw-r--r--tests/ui/manual_arithmetic_check.fixed16
-rw-r--r--tests/ui/manual_arithmetic_check.rs20
-rw-r--r--tests/ui/manual_arithmetic_check.stderr29
6 files changed, 183 insertions, 0 deletions
diff --git a/tests/ui/implicit_saturating_sub.rs b/tests/ui/implicit_saturating_sub.rs
index 5d7b95d2c65..05f7d852383 100644
--- a/tests/ui/implicit_saturating_sub.rs
+++ b/tests/ui/implicit_saturating_sub.rs
@@ -260,4 +260,11 @@ fn main() {
     } else if u_32 > 0 {
         u_32 -= 1;
     }
+
+    let result = if a < b {
+        println!("we shouldn't remove this");
+        0
+    } else {
+        a - b
+    };
 }
diff --git a/tests/ui/manual_arithmetic_check-2.rs b/tests/ui/manual_arithmetic_check-2.rs
new file mode 100644
index 00000000000..e97e3bdfef7
--- /dev/null
+++ b/tests/ui/manual_arithmetic_check-2.rs
@@ -0,0 +1,35 @@
+//@no-rustfix
+#![warn(clippy::implicit_saturating_sub)]
+#![allow(arithmetic_overflow)]
+
+fn main() {
+    let a = 12u32;
+    let b = 13u32;
+
+    let result = if a > b { b - a } else { 0 };
+    //~^ ERROR: inverted arithmetic check before subtraction
+    let result = if b < a { b - a } else { 0 };
+    //~^ ERROR: inverted arithmetic check before subtraction
+
+    let result = if a > b { 0 } else { a - b };
+    //~^ ERROR: inverted arithmetic check before subtraction
+    let result = if a >= b { 0 } else { a - b };
+    //~^ ERROR: inverted arithmetic check before subtraction
+    let result = if b < a { 0 } else { a - b };
+    //~^ ERROR: inverted arithmetic check before subtraction
+    let result = if b <= a { 0 } else { a - b };
+    //~^ ERROR: inverted arithmetic check before subtraction
+
+    let af = 12f32;
+    let bf = 13f32;
+    // Should not lint!
+    let result = if bf < af { 0. } else { af - bf };
+
+    // Should not lint!
+    let result = if a < b {
+        println!("we shouldn't remove this");
+        0
+    } else {
+        a - b
+    };
+}
diff --git a/tests/ui/manual_arithmetic_check-2.stderr b/tests/ui/manual_arithmetic_check-2.stderr
new file mode 100644
index 00000000000..d49e536ce3f
--- /dev/null
+++ b/tests/ui/manual_arithmetic_check-2.stderr
@@ -0,0 +1,76 @@
+error: inverted arithmetic check before subtraction
+  --> tests/ui/manual_arithmetic_check-2.rs:9:23
+   |
+LL |     let result = if a > b { b - a } else { 0 };
+   |                       ^     ----- help: try replacing it with: `a - b`
+   |
+note: this subtraction underflows when `b < a`
+  --> tests/ui/manual_arithmetic_check-2.rs:9:29
+   |
+LL |     let result = if a > b { b - a } else { 0 };
+   |                             ^^^^^
+   = note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
+
+error: inverted arithmetic check before subtraction
+  --> tests/ui/manual_arithmetic_check-2.rs:11:23
+   |
+LL |     let result = if b < a { b - a } else { 0 };
+   |                       ^     ----- help: try replacing it with: `a - b`
+   |
+note: this subtraction underflows when `b < a`
+  --> tests/ui/manual_arithmetic_check-2.rs:11:29
+   |
+LL |     let result = if b < a { b - a } else { 0 };
+   |                             ^^^^^
+
+error: inverted arithmetic check before subtraction
+  --> tests/ui/manual_arithmetic_check-2.rs:14:23
+   |
+LL |     let result = if a > b { 0 } else { a - b };
+   |                       ^                ----- help: try replacing it with: `b - a`
+   |
+note: this subtraction underflows when `a < b`
+  --> tests/ui/manual_arithmetic_check-2.rs:14:40
+   |
+LL |     let result = if a > b { 0 } else { a - b };
+   |                                        ^^^^^
+
+error: inverted arithmetic check before subtraction
+  --> tests/ui/manual_arithmetic_check-2.rs:16:23
+   |
+LL |     let result = if a >= b { 0 } else { a - b };
+   |                       ^^                ----- help: try replacing it with: `b - a`
+   |
+note: this subtraction underflows when `a < b`
+  --> tests/ui/manual_arithmetic_check-2.rs:16:41
+   |
+LL |     let result = if a >= b { 0 } else { a - b };
+   |                                         ^^^^^
+
+error: inverted arithmetic check before subtraction
+  --> tests/ui/manual_arithmetic_check-2.rs:18:23
+   |
+LL |     let result = if b < a { 0 } else { a - b };
+   |                       ^                ----- help: try replacing it with: `b - a`
+   |
+note: this subtraction underflows when `a < b`
+  --> tests/ui/manual_arithmetic_check-2.rs:18:40
+   |
+LL |     let result = if b < a { 0 } else { a - b };
+   |                                        ^^^^^
+
+error: inverted arithmetic check before subtraction
+  --> tests/ui/manual_arithmetic_check-2.rs:20:23
+   |
+LL |     let result = if b <= a { 0 } else { a - b };
+   |                       ^^                ----- help: try replacing it with: `b - a`
+   |
+note: this subtraction underflows when `a < b`
+  --> tests/ui/manual_arithmetic_check-2.rs:20:41
+   |
+LL |     let result = if b <= a { 0 } else { a - b };
+   |                                         ^^^^^
+
+error: aborting due to 6 previous errors
+
diff --git a/tests/ui/manual_arithmetic_check.fixed b/tests/ui/manual_arithmetic_check.fixed
new file mode 100644
index 00000000000..afb2abcbe90
--- /dev/null
+++ b/tests/ui/manual_arithmetic_check.fixed
@@ -0,0 +1,16 @@
+#![warn(clippy::implicit_saturating_sub)]
+
+fn main() {
+    let a = 12u32;
+    let b = 13u32;
+
+    let result = a.saturating_sub(b);
+    //~^ ERROR: manual arithmetic check found
+    let result = a.saturating_sub(b);
+    //~^ ERROR: manual arithmetic check found
+
+    let result = a.saturating_sub(b);
+    //~^ ERROR: manual arithmetic check found
+    let result = a.saturating_sub(b);
+    //~^ ERROR: manual arithmetic check found
+}
diff --git a/tests/ui/manual_arithmetic_check.rs b/tests/ui/manual_arithmetic_check.rs
new file mode 100644
index 00000000000..c11ac7097fc
--- /dev/null
+++ b/tests/ui/manual_arithmetic_check.rs
@@ -0,0 +1,20 @@
+#![warn(clippy::implicit_saturating_sub)]
+
+fn main() {
+    let a = 12u32;
+    let b = 13u32;
+    let c = 8u32;
+
+    let result = if a > b { a - b } else { 0 };
+    //~^ ERROR: manual arithmetic check found
+    let result = if b < a { a - b } else { 0 };
+    //~^ ERROR: manual arithmetic check found
+
+    let result = if a < b { 0 } else { a - b };
+    //~^ ERROR: manual arithmetic check found
+    let result = if b > a { 0 } else { a - b };
+    //~^ ERROR: manual arithmetic check found
+
+    // Should not warn!
+    let result = if a > b { a - b } else { a - c };
+}
diff --git a/tests/ui/manual_arithmetic_check.stderr b/tests/ui/manual_arithmetic_check.stderr
new file mode 100644
index 00000000000..153a8bb5734
--- /dev/null
+++ b/tests/ui/manual_arithmetic_check.stderr
@@ -0,0 +1,29 @@
+error: manual arithmetic check found
+  --> tests/ui/manual_arithmetic_check.rs:7:18
+   |
+LL |     let result = if a > b { a - b } else { 0 };
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
+   |
+   = note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
+
+error: manual arithmetic check found
+  --> tests/ui/manual_arithmetic_check.rs:9:18
+   |
+LL |     let result = if b < a { a - b } else { 0 };
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
+
+error: manual arithmetic check found
+  --> tests/ui/manual_arithmetic_check.rs:12:18
+   |
+LL |     let result = if a < b { 0 } else { a - b };
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
+
+error: manual arithmetic check found
+  --> tests/ui/manual_arithmetic_check.rs:14:18
+   |
+LL |     let result = if b > a { 0 } else { a - b };
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
+
+error: aborting due to 4 previous errors
+