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 Mar 19, 2024
1 parent e05bd3a commit b88a7a9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
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 b88a7a9

Please sign in to comment.