Skip to content

Commit

Permalink
Add ui regression tests for implicit_saturation_sub lint extension
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jul 19, 2024
1 parent 6984930 commit ef6144b
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/ui/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
35 changes: 35 additions & 0 deletions tests/ui/manual_arithmetic_check-2.rs
Original file line number Diff line number Diff line change
@@ -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
};
}
76 changes: 76 additions & 0 deletions tests/ui/manual_arithmetic_check-2.stderr
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions tests/ui/manual_arithmetic_check.fixed
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions tests/ui/manual_arithmetic_check.rs
Original file line number Diff line number Diff line change
@@ -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 };
}
29 changes: 29 additions & 0 deletions tests/ui/manual_arithmetic_check.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ef6144b

Please sign in to comment.