about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/implicit_saturating_sub.rs2
-rw-r--r--clippy_lints/src/lib.register_all.rs1
-rw-r--r--clippy_lints/src/lib.register_pedantic.rs1
-rw-r--r--clippy_lints/src/lib.register_style.rs1
-rw-r--r--tests/ui/implicit_saturating_sub.fixed50
-rw-r--r--tests/ui/implicit_saturating_sub.rs50
-rw-r--r--tests/ui/implicit_saturating_sub.stderr46
7 files changed, 126 insertions, 25 deletions
diff --git a/clippy_lints/src/implicit_saturating_sub.rs b/clippy_lints/src/implicit_saturating_sub.rs
index 48edbf6ae57..29d59c26d92 100644
--- a/clippy_lints/src/implicit_saturating_sub.rs
+++ b/clippy_lints/src/implicit_saturating_sub.rs
@@ -35,7 +35,7 @@ declare_clippy_lint! {
     /// ```
     #[clippy::version = "1.44.0"]
     pub IMPLICIT_SATURATING_SUB,
-    pedantic,
+    style,
     "Perform saturating subtraction instead of implicitly checking lower bound of data type"
 }
 
diff --git a/clippy_lints/src/lib.register_all.rs b/clippy_lints/src/lib.register_all.rs
index 04f0da4b2fe..b5f0e5fd7d6 100644
--- a/clippy_lints/src/lib.register_all.rs
+++ b/clippy_lints/src/lib.register_all.rs
@@ -89,6 +89,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
     LintId::of(functions::TOO_MANY_ARGUMENTS),
     LintId::of(if_let_mutex::IF_LET_MUTEX),
     LintId::of(implicit_saturating_add::IMPLICIT_SATURATING_ADD),
+    LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB),
     LintId::of(indexing_slicing::OUT_OF_BOUNDS_INDEXING),
     LintId::of(infinite_iter::INFINITE_ITER),
     LintId::of(inherent_to_string::INHERENT_TO_STRING),
diff --git a/clippy_lints/src/lib.register_pedantic.rs b/clippy_lints/src/lib.register_pedantic.rs
index 1b9d575f3e4..2ead71a68fa 100644
--- a/clippy_lints/src/lib.register_pedantic.rs
+++ b/clippy_lints/src/lib.register_pedantic.rs
@@ -33,7 +33,6 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
     LintId::of(functions::TOO_MANY_LINES),
     LintId::of(if_not_else::IF_NOT_ELSE),
     LintId::of(implicit_hasher::IMPLICIT_HASHER),
-    LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB),
     LintId::of(inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR),
     LintId::of(infinite_iter::MAYBE_INFINITE_ITER),
     LintId::of(invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS),
diff --git a/clippy_lints/src/lib.register_style.rs b/clippy_lints/src/lib.register_style.rs
index 81fefb84e0d..6894d69e928 100644
--- a/clippy_lints/src/lib.register_style.rs
+++ b/clippy_lints/src/lib.register_style.rs
@@ -32,6 +32,7 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![
     LintId::of(functions::MUST_USE_UNIT),
     LintId::of(functions::RESULT_UNIT_ERR),
     LintId::of(implicit_saturating_add::IMPLICIT_SATURATING_ADD),
+    LintId::of(implicit_saturating_sub::IMPLICIT_SATURATING_SUB),
     LintId::of(inherent_to_string::INHERENT_TO_STRING),
     LintId::of(init_numbered_fields::INIT_NUMBERED_FIELDS),
     LintId::of(len_zero::COMPARISON_TO_EMPTY),
diff --git a/tests/ui/implicit_saturating_sub.fixed b/tests/ui/implicit_saturating_sub.fixed
index e6f57e9267e..93df81b1a7f 100644
--- a/tests/ui/implicit_saturating_sub.fixed
+++ b/tests/ui/implicit_saturating_sub.fixed
@@ -2,6 +2,21 @@
 #![allow(unused_assignments, unused_mut, clippy::assign_op_pattern)]
 #![warn(clippy::implicit_saturating_sub)]
 
+use std::cmp::PartialEq;
+use std::ops::SubAssign;
+// Mock type
+struct Mock;
+
+impl PartialEq<u32> for Mock {
+    fn eq(&self, _: &u32) -> bool {
+        true
+    }
+}
+
+impl SubAssign<u32> for Mock {
+    fn sub_assign(&mut self, _: u32) {}
+}
+
 fn main() {
     // Tests for unsigned integers
 
@@ -165,4 +180,39 @@ fn main() {
     } else {
         println!("side effect");
     }
+
+    // Extended tests
+    let mut m = Mock;
+    let mut u_32 = 3000;
+    let a = 200;
+    let mut _b = 8;
+
+    if m != 0 {
+        m -= 1;
+    }
+
+    if a > 0 {
+        _b -= 1;
+    }
+
+    if 0 > a {
+        _b -= 1;
+    }
+
+    if u_32 > 0 {
+        u_32 -= 1;
+    } else {
+        println!("don't lint this");
+    }
+
+    if u_32 > 0 {
+        println!("don't lint this");
+        u_32 -= 1;
+    }
+
+    if u_32 > 42 {
+        println!("brace yourself!");
+    } else if u_32 > 0 {
+        u_32 -= 1;
+    }
 }
diff --git a/tests/ui/implicit_saturating_sub.rs b/tests/ui/implicit_saturating_sub.rs
index 8bb28d149c6..8340bc8264d 100644
--- a/tests/ui/implicit_saturating_sub.rs
+++ b/tests/ui/implicit_saturating_sub.rs
@@ -2,6 +2,21 @@
 #![allow(unused_assignments, unused_mut, clippy::assign_op_pattern)]
 #![warn(clippy::implicit_saturating_sub)]
 
+use std::cmp::PartialEq;
+use std::ops::SubAssign;
+// Mock type
+struct Mock;
+
+impl PartialEq<u32> for Mock {
+    fn eq(&self, _: &u32) -> bool {
+        true
+    }
+}
+
+impl SubAssign<u32> for Mock {
+    fn sub_assign(&mut self, _: u32) {}
+}
+
 fn main() {
     // Tests for unsigned integers
 
@@ -211,4 +226,39 @@ fn main() {
     } else {
         println!("side effect");
     }
+
+    // Extended tests
+    let mut m = Mock;
+    let mut u_32 = 3000;
+    let a = 200;
+    let mut _b = 8;
+
+    if m != 0 {
+        m -= 1;
+    }
+
+    if a > 0 {
+        _b -= 1;
+    }
+
+    if 0 > a {
+        _b -= 1;
+    }
+
+    if u_32 > 0 {
+        u_32 -= 1;
+    } else {
+        println!("don't lint this");
+    }
+
+    if u_32 > 0 {
+        println!("don't lint this");
+        u_32 -= 1;
+    }
+
+    if u_32 > 42 {
+        println!("brace yourself!");
+    } else if u_32 > 0 {
+        u_32 -= 1;
+    }
 }
diff --git a/tests/ui/implicit_saturating_sub.stderr b/tests/ui/implicit_saturating_sub.stderr
index 5bb9a606422..5e589d931e4 100644
--- a/tests/ui/implicit_saturating_sub.stderr
+++ b/tests/ui/implicit_saturating_sub.stderr
@@ -1,5 +1,5 @@
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:13:5
+  --> $DIR/implicit_saturating_sub.rs:28:5
    |
 LL | /     if u_8 > 0 {
 LL | |         u_8 = u_8 - 1;
@@ -9,7 +9,7 @@ LL | |     }
    = note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:20:13
+  --> $DIR/implicit_saturating_sub.rs:35:13
    |
 LL | /             if u_8 > 0 {
 LL | |                 u_8 -= 1;
@@ -17,7 +17,7 @@ LL | |             }
    | |_____________^ help: try: `u_8 = u_8.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:34:5
+  --> $DIR/implicit_saturating_sub.rs:49:5
    |
 LL | /     if u_16 > 0 {
 LL | |         u_16 -= 1;
@@ -25,7 +25,7 @@ LL | |     }
    | |_____^ help: try: `u_16 = u_16.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:44:5
+  --> $DIR/implicit_saturating_sub.rs:59:5
    |
 LL | /     if u_32 != 0 {
 LL | |         u_32 -= 1;
@@ -33,7 +33,7 @@ LL | |     }
    | |_____^ help: try: `u_32 = u_32.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:65:5
+  --> $DIR/implicit_saturating_sub.rs:80:5
    |
 LL | /     if u_64 > 0 {
 LL | |         u_64 -= 1;
@@ -41,7 +41,7 @@ LL | |     }
    | |_____^ help: try: `u_64 = u_64.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:70:5
+  --> $DIR/implicit_saturating_sub.rs:85:5
    |
 LL | /     if 0 < u_64 {
 LL | |         u_64 -= 1;
@@ -49,7 +49,7 @@ LL | |     }
    | |_____^ help: try: `u_64 = u_64.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:75:5
+  --> $DIR/implicit_saturating_sub.rs:90:5
    |
 LL | /     if 0 != u_64 {
 LL | |         u_64 -= 1;
@@ -57,7 +57,7 @@ LL | |     }
    | |_____^ help: try: `u_64 = u_64.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:96:5
+  --> $DIR/implicit_saturating_sub.rs:111:5
    |
 LL | /     if u_usize > 0 {
 LL | |         u_usize -= 1;
@@ -65,7 +65,7 @@ LL | |     }
    | |_____^ help: try: `u_usize = u_usize.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:108:5
+  --> $DIR/implicit_saturating_sub.rs:123:5
    |
 LL | /     if i_8 > i8::MIN {
 LL | |         i_8 -= 1;
@@ -73,7 +73,7 @@ LL | |     }
    | |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:113:5
+  --> $DIR/implicit_saturating_sub.rs:128:5
    |
 LL | /     if i_8 > i8::MIN {
 LL | |         i_8 -= 1;
@@ -81,7 +81,7 @@ LL | |     }
    | |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:118:5
+  --> $DIR/implicit_saturating_sub.rs:133:5
    |
 LL | /     if i_8 != i8::MIN {
 LL | |         i_8 -= 1;
@@ -89,7 +89,7 @@ LL | |     }
    | |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:123:5
+  --> $DIR/implicit_saturating_sub.rs:138:5
    |
 LL | /     if i_8 != i8::MIN {
 LL | |         i_8 -= 1;
@@ -97,7 +97,7 @@ LL | |     }
    | |_____^ help: try: `i_8 = i_8.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:133:5
+  --> $DIR/implicit_saturating_sub.rs:148:5
    |
 LL | /     if i_16 > i16::MIN {
 LL | |         i_16 -= 1;
@@ -105,7 +105,7 @@ LL | |     }
    | |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:138:5
+  --> $DIR/implicit_saturating_sub.rs:153:5
    |
 LL | /     if i_16 > i16::MIN {
 LL | |         i_16 -= 1;
@@ -113,7 +113,7 @@ LL | |     }
    | |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:143:5
+  --> $DIR/implicit_saturating_sub.rs:158:5
    |
 LL | /     if i_16 != i16::MIN {
 LL | |         i_16 -= 1;
@@ -121,7 +121,7 @@ LL | |     }
    | |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:148:5
+  --> $DIR/implicit_saturating_sub.rs:163:5
    |
 LL | /     if i_16 != i16::MIN {
 LL | |         i_16 -= 1;
@@ -129,7 +129,7 @@ LL | |     }
    | |_____^ help: try: `i_16 = i_16.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:158:5
+  --> $DIR/implicit_saturating_sub.rs:173:5
    |
 LL | /     if i_32 > i32::MIN {
 LL | |         i_32 -= 1;
@@ -137,7 +137,7 @@ LL | |     }
    | |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:163:5
+  --> $DIR/implicit_saturating_sub.rs:178:5
    |
 LL | /     if i_32 > i32::MIN {
 LL | |         i_32 -= 1;
@@ -145,7 +145,7 @@ LL | |     }
    | |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:168:5
+  --> $DIR/implicit_saturating_sub.rs:183:5
    |
 LL | /     if i_32 != i32::MIN {
 LL | |         i_32 -= 1;
@@ -153,7 +153,7 @@ LL | |     }
    | |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:173:5
+  --> $DIR/implicit_saturating_sub.rs:188:5
    |
 LL | /     if i_32 != i32::MIN {
 LL | |         i_32 -= 1;
@@ -161,7 +161,7 @@ LL | |     }
    | |_____^ help: try: `i_32 = i_32.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:183:5
+  --> $DIR/implicit_saturating_sub.rs:198:5
    |
 LL | /     if i64::MIN < i_64 {
 LL | |         i_64 -= 1;
@@ -169,7 +169,7 @@ LL | |     }
    | |_____^ help: try: `i_64 = i_64.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:188:5
+  --> $DIR/implicit_saturating_sub.rs:203:5
    |
 LL | /     if i64::MIN != i_64 {
 LL | |         i_64 -= 1;
@@ -177,7 +177,7 @@ LL | |     }
    | |_____^ help: try: `i_64 = i_64.saturating_sub(1);`
 
 error: implicitly performing saturating subtraction
-  --> $DIR/implicit_saturating_sub.rs:193:5
+  --> $DIR/implicit_saturating_sub.rs:208:5
    |
 LL | /     if i64::MIN < i_64 {
 LL | |         i_64 -= 1;