Add ui regression tests for implicit_saturation_sub lint extension

This commit is contained in:
Guillaume Gomez 2024-03-19 16:59:08 +01:00
parent 74a2344dc1
commit 2622a87587
6 changed files with 183 additions and 0 deletions

View file

@ -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
};
}

View file

@ -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
};
}

View file

@ -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

View file

@ -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
}

View file

@ -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 };
}

View file

@ -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