Skip to content

Commit 6acaf81

Browse files
Add ui test to ensure that if 0 is returned from both if and else, it will not break clippy
1 parent 01f0542 commit 6acaf81

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

tests/ui/manual_arithmetic_check.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![warn(clippy::implicit_saturating_sub)]
2+
#![allow(clippy::if_same_then_else)]
23

34
fn main() {
45
let a = 12u32;
56
let b = 13u32;
7+
let c = 8u32;
68

79
let result = a.saturating_sub(b);
810
//~^ ERROR: manual arithmetic check found
@@ -13,4 +15,10 @@ fn main() {
1315
//~^ ERROR: manual arithmetic check found
1416
let result = a.saturating_sub(b);
1517
//~^ ERROR: manual arithmetic check found
18+
19+
// Should not warn!
20+
let result = if a > b { a - b } else { a - c };
21+
22+
// Just to check it won't break clippy.
23+
let result = if b > a { 0 } else { 0 };
1624
}

tests/ui/manual_arithmetic_check.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::implicit_saturating_sub)]
2+
#![allow(clippy::if_same_then_else)]
23

34
fn main() {
45
let a = 12u32;
@@ -17,4 +18,7 @@ fn main() {
1718

1819
// Should not warn!
1920
let result = if a > b { a - b } else { a - c };
21+
22+
// Just to check it won't break clippy.
23+
let result = if b > a { 0 } else { 0 };
2024
}

tests/ui/manual_arithmetic_check.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: manual arithmetic check found
2-
--> tests/ui/manual_arithmetic_check.rs:7:18
2+
--> tests/ui/manual_arithmetic_check.rs:9:18
33
|
44
LL | let result = if a > b { a - b } else { 0 };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
@@ -8,19 +8,19 @@ LL | let result = if a > b { a - b } else { 0 };
88
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
99

1010
error: manual arithmetic check found
11-
--> tests/ui/manual_arithmetic_check.rs:9:18
11+
--> tests/ui/manual_arithmetic_check.rs:11:18
1212
|
1313
LL | let result = if b < a { a - b } else { 0 };
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
1515

1616
error: manual arithmetic check found
17-
--> tests/ui/manual_arithmetic_check.rs:12:18
17+
--> tests/ui/manual_arithmetic_check.rs:14:18
1818
|
1919
LL | let result = if a < b { 0 } else { a - b };
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
2121

2222
error: manual arithmetic check found
23-
--> tests/ui/manual_arithmetic_check.rs:14:18
23+
--> tests/ui/manual_arithmetic_check.rs:16:18
2424
|
2525
LL | let result = if b > a { 0 } else { a - b };
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`

0 commit comments

Comments
 (0)