Add ui test to ensure that if 0 is returned from both if and else, it will not break clippy

This commit is contained in:
Guillaume Gomez 2024-03-19 17:00:31 +01:00
parent 2622a87587
commit 27c6343365
5 changed files with 29 additions and 10 deletions

View file

@ -184,18 +184,18 @@ fn main() {
let mut m = Mock;
let mut u_32 = 3000;
let a = 200;
let mut _b = 8;
let mut b = 8;
if m != 0 {
m -= 1;
}
if a > 0 {
_b -= 1;
b -= 1;
}
if 0 > a {
_b -= 1;
b -= 1;
}
if u_32 > 0 {
@ -214,4 +214,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

@ -230,18 +230,18 @@ fn main() {
let mut m = Mock;
let mut u_32 = 3000;
let a = 200;
let mut _b = 8;
let mut b = 8;
if m != 0 {
m -= 1;
}
if a > 0 {
_b -= 1;
b -= 1;
}
if 0 > a {
_b -= 1;
b -= 1;
}
if u_32 > 0 {

View file

@ -1,8 +1,10 @@
#![warn(clippy::implicit_saturating_sub)]
#![allow(clippy::if_same_then_else)]
fn main() {
let a = 12u32;
let b = 13u32;
let c = 8u32;
let result = a.saturating_sub(b);
//~^ ERROR: manual arithmetic check found
@ -13,4 +15,10 @@ fn main() {
//~^ ERROR: manual arithmetic check found
let result = a.saturating_sub(b);
//~^ ERROR: manual arithmetic check found
// Should not warn!
let result = if a > b { a - b } else { a - c };
// Just to check it won't break clippy.
let result = if b > a { 0 } else { 0 };
}

View file

@ -1,4 +1,5 @@
#![warn(clippy::implicit_saturating_sub)]
#![allow(clippy::if_same_then_else)]
fn main() {
let a = 12u32;
@ -17,4 +18,7 @@ fn main() {
// Should not warn!
let result = if a > b { a - b } else { a - c };
// Just to check it won't break clippy.
let result = if b > a { 0 } else { 0 };
}

View file

@ -1,5 +1,5 @@
error: manual arithmetic check found
--> tests/ui/manual_arithmetic_check.rs:7:18
--> tests/ui/manual_arithmetic_check.rs:9:18
|
LL | let result = if a > b { a - b } else { 0 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
@ -8,19 +8,19 @@ LL | let result = if a > b { a - b } else { 0 };
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
error: manual arithmetic check found
--> tests/ui/manual_arithmetic_check.rs:9:18
--> tests/ui/manual_arithmetic_check.rs:11: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
--> tests/ui/manual_arithmetic_check.rs:14: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
--> tests/ui/manual_arithmetic_check.rs:16:18
|
LL | let result = if b > a { 0 } else { a - b };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`