Skip to content

Commit 2622a87

Browse files
Add ui regression tests for implicit_saturation_sub lint extension
1 parent 74a2344 commit 2622a87

6 files changed

+183
-0
lines changed

tests/ui/implicit_saturating_sub.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,11 @@ fn main() {
260260
} else if u_32 > 0 {
261261
u_32 -= 1;
262262
}
263+
264+
let result = if a < b {
265+
println!("we shouldn't remove this");
266+
0
267+
} else {
268+
a - b
269+
};
263270
}

tests/ui/manual_arithmetic_check-2.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@no-rustfix
2+
#![warn(clippy::implicit_saturating_sub)]
3+
#![allow(arithmetic_overflow)]
4+
5+
fn main() {
6+
let a = 12u32;
7+
let b = 13u32;
8+
9+
let result = if a > b { b - a } else { 0 };
10+
//~^ ERROR: inverted arithmetic check before subtraction
11+
let result = if b < a { b - a } else { 0 };
12+
//~^ ERROR: inverted arithmetic check before subtraction
13+
14+
let result = if a > b { 0 } else { a - b };
15+
//~^ ERROR: inverted arithmetic check before subtraction
16+
let result = if a >= b { 0 } else { a - b };
17+
//~^ ERROR: inverted arithmetic check before subtraction
18+
let result = if b < a { 0 } else { a - b };
19+
//~^ ERROR: inverted arithmetic check before subtraction
20+
let result = if b <= a { 0 } else { a - b };
21+
//~^ ERROR: inverted arithmetic check before subtraction
22+
23+
let af = 12f32;
24+
let bf = 13f32;
25+
// Should not lint!
26+
let result = if bf < af { 0. } else { af - bf };
27+
28+
// Should not lint!
29+
let result = if a < b {
30+
println!("we shouldn't remove this");
31+
0
32+
} else {
33+
a - b
34+
};
35+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
error: inverted arithmetic check before subtraction
2+
--> tests/ui/manual_arithmetic_check-2.rs:9:23
3+
|
4+
LL | let result = if a > b { b - a } else { 0 };
5+
| ^ ----- help: try replacing it with: `a - b`
6+
|
7+
note: this subtraction underflows when `b < a`
8+
--> tests/ui/manual_arithmetic_check-2.rs:9:29
9+
|
10+
LL | let result = if a > b { b - a } else { 0 };
11+
| ^^^^^
12+
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
14+
15+
error: inverted arithmetic check before subtraction
16+
--> tests/ui/manual_arithmetic_check-2.rs:11:23
17+
|
18+
LL | let result = if b < a { b - a } else { 0 };
19+
| ^ ----- help: try replacing it with: `a - b`
20+
|
21+
note: this subtraction underflows when `b < a`
22+
--> tests/ui/manual_arithmetic_check-2.rs:11:29
23+
|
24+
LL | let result = if b < a { b - a } else { 0 };
25+
| ^^^^^
26+
27+
error: inverted arithmetic check before subtraction
28+
--> tests/ui/manual_arithmetic_check-2.rs:14:23
29+
|
30+
LL | let result = if a > b { 0 } else { a - b };
31+
| ^ ----- help: try replacing it with: `b - a`
32+
|
33+
note: this subtraction underflows when `a < b`
34+
--> tests/ui/manual_arithmetic_check-2.rs:14:40
35+
|
36+
LL | let result = if a > b { 0 } else { a - b };
37+
| ^^^^^
38+
39+
error: inverted arithmetic check before subtraction
40+
--> tests/ui/manual_arithmetic_check-2.rs:16:23
41+
|
42+
LL | let result = if a >= b { 0 } else { a - b };
43+
| ^^ ----- help: try replacing it with: `b - a`
44+
|
45+
note: this subtraction underflows when `a < b`
46+
--> tests/ui/manual_arithmetic_check-2.rs:16:41
47+
|
48+
LL | let result = if a >= b { 0 } else { a - b };
49+
| ^^^^^
50+
51+
error: inverted arithmetic check before subtraction
52+
--> tests/ui/manual_arithmetic_check-2.rs:18:23
53+
|
54+
LL | let result = if b < a { 0 } else { a - b };
55+
| ^ ----- help: try replacing it with: `b - a`
56+
|
57+
note: this subtraction underflows when `a < b`
58+
--> tests/ui/manual_arithmetic_check-2.rs:18:40
59+
|
60+
LL | let result = if b < a { 0 } else { a - b };
61+
| ^^^^^
62+
63+
error: inverted arithmetic check before subtraction
64+
--> tests/ui/manual_arithmetic_check-2.rs:20:23
65+
|
66+
LL | let result = if b <= a { 0 } else { a - b };
67+
| ^^ ----- help: try replacing it with: `b - a`
68+
|
69+
note: this subtraction underflows when `a < b`
70+
--> tests/ui/manual_arithmetic_check-2.rs:20:41
71+
|
72+
LL | let result = if b <= a { 0 } else { a - b };
73+
| ^^^^^
74+
75+
error: aborting due to 6 previous errors
76+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![warn(clippy::implicit_saturating_sub)]
2+
3+
fn main() {
4+
let a = 12u32;
5+
let b = 13u32;
6+
7+
let result = a.saturating_sub(b);
8+
//~^ ERROR: manual arithmetic check found
9+
let result = a.saturating_sub(b);
10+
//~^ ERROR: manual arithmetic check found
11+
12+
let result = a.saturating_sub(b);
13+
//~^ ERROR: manual arithmetic check found
14+
let result = a.saturating_sub(b);
15+
//~^ ERROR: manual arithmetic check found
16+
}

tests/ui/manual_arithmetic_check.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![warn(clippy::implicit_saturating_sub)]
2+
3+
fn main() {
4+
let a = 12u32;
5+
let b = 13u32;
6+
let c = 8u32;
7+
8+
let result = if a > b { a - b } else { 0 };
9+
//~^ ERROR: manual arithmetic check found
10+
let result = if b < a { a - b } else { 0 };
11+
//~^ ERROR: manual arithmetic check found
12+
13+
let result = if a < b { 0 } else { a - b };
14+
//~^ ERROR: manual arithmetic check found
15+
let result = if b > a { 0 } else { a - b };
16+
//~^ ERROR: manual arithmetic check found
17+
18+
// Should not warn!
19+
let result = if a > b { a - b } else { a - c };
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: manual arithmetic check found
2+
--> tests/ui/manual_arithmetic_check.rs:7:18
3+
|
4+
LL | let result = if a > b { a - b } else { 0 };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
6+
|
7+
= note: `-D clippy::implicit-saturating-sub` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::implicit_saturating_sub)]`
9+
10+
error: manual arithmetic check found
11+
--> tests/ui/manual_arithmetic_check.rs:9:18
12+
|
13+
LL | let result = if b < a { a - b } else { 0 };
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
15+
16+
error: manual arithmetic check found
17+
--> tests/ui/manual_arithmetic_check.rs:12:18
18+
|
19+
LL | let result = if a < b { 0 } else { a - b };
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
21+
22+
error: manual arithmetic check found
23+
--> tests/ui/manual_arithmetic_check.rs:14:18
24+
|
25+
LL | let result = if b > a { 0 } else { a - b };
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `a.saturating_sub(b)`
27+
28+
error: aborting due to 4 previous errors
29+

0 commit comments

Comments
 (0)