Skip to content

Commit

Permalink
Add ui test to ensure that if 0 is returned from both if and `els…
Browse files Browse the repository at this point in the history
…e`, it will not break clippy
  • Loading branch information
GuillaumeGomez committed Jul 19, 2024
1 parent ef6144b commit eb37b31
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
13 changes: 10 additions & 3 deletions tests/ui/implicit_saturating_sub.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
};
}
6 changes: 3 additions & 3 deletions tests/ui/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/manual_arithmetic_check.fixed
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 };
}
4 changes: 4 additions & 0 deletions tests/ui/manual_arithmetic_check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::implicit_saturating_sub)]
#![allow(clippy::if_same_then_else)]

fn main() {
let a = 12u32;
Expand All @@ -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 };
}
8 changes: 4 additions & 4 deletions tests/ui/manual_arithmetic_check.stderr
Original file line number Diff line number Diff line change
@@ -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)`
Expand All @@ -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)`
Expand Down

0 comments on commit eb37b31

Please sign in to comment.