Skip to content

Commit 0ce4af5

Browse files
Add ui tests for manual_arithmetic_check
1 parent 17d2ec8 commit 0ce4af5

5 files changed

+150
-0
lines changed

tests/ui/manual_arithmetic_check-2.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@no-rustfix
2+
#![warn(clippy::manual_arithmetic_check)]
3+
4+
fn main() {
5+
let a = 12;
6+
let b = 13;
7+
8+
let result = if a > b { b - a } else { 0 };
9+
//~^ ERROR: reverted arithmetic check before subtraction
10+
let result = if b < a { b - a } else { 0 };
11+
//~^ ERROR: reverted arithmetic check before subtraction
12+
13+
let result = if a > b { 0 } else { a - b };
14+
//~^ ERROR: reverted arithmetic check before subtraction
15+
let result = if b < a { 0 } else { a - b };
16+
//~^ ERROR: reverted arithmetic check before subtraction
17+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
error: reverted arithmetic check before subtraction
2+
--> tests/ui/manual_arithmetic_check-2.rs:8:21
3+
|
4+
LL | let result = if a > b { b - a } else { 0 };
5+
| ^^^^^ ----- help: try replacing it with: `a - b`
6+
|
7+
note: you check that `a` is bigger than `b`
8+
--> tests/ui/manual_arithmetic_check-2.rs:8:21
9+
|
10+
LL | let result = if a > b { b - a } else { 0 };
11+
| ^^^^^
12+
note: but then you subtract `a` from `b`
13+
--> tests/ui/manual_arithmetic_check-2.rs:8:29
14+
|
15+
LL | let result = if a > b { b - a } else { 0 };
16+
| ^^^^^
17+
= note: `-D clippy::manual-arithmetic-check` implied by `-D warnings`
18+
= help: to override `-D warnings` add `#[allow(clippy::manual_arithmetic_check)]`
19+
20+
error: reverted arithmetic check before subtraction
21+
--> tests/ui/manual_arithmetic_check-2.rs:10:21
22+
|
23+
LL | let result = if b < a { b - a } else { 0 };
24+
| ^^^^^ ----- help: try replacing it with: `a - b`
25+
|
26+
note: you check that `a` is bigger than `b`
27+
--> tests/ui/manual_arithmetic_check-2.rs:10:21
28+
|
29+
LL | let result = if b < a { b - a } else { 0 };
30+
| ^^^^^
31+
note: but then you subtract `a` from `b`
32+
--> tests/ui/manual_arithmetic_check-2.rs:10:29
33+
|
34+
LL | let result = if b < a { b - a } else { 0 };
35+
| ^^^^^
36+
37+
error: reverted arithmetic check before subtraction
38+
--> tests/ui/manual_arithmetic_check-2.rs:13:21
39+
|
40+
LL | let result = if a > b { 0 } else { a - b };
41+
| ^^^^^ ----- help: try replacing it with: `b - a`
42+
|
43+
note: you check that `b` is bigger than `a`
44+
--> tests/ui/manual_arithmetic_check-2.rs:13:21
45+
|
46+
LL | let result = if a > b { 0 } else { a - b };
47+
| ^^^^^
48+
note: but then you subtract `b` from `a`
49+
--> tests/ui/manual_arithmetic_check-2.rs:13:40
50+
|
51+
LL | let result = if a > b { 0 } else { a - b };
52+
| ^^^^^
53+
54+
error: reverted arithmetic check before subtraction
55+
--> tests/ui/manual_arithmetic_check-2.rs:15:21
56+
|
57+
LL | let result = if b < a { 0 } else { a - b };
58+
| ^^^^^ ----- help: try replacing it with: `b - a`
59+
|
60+
note: you check that `b` is bigger than `a`
61+
--> tests/ui/manual_arithmetic_check-2.rs:15:21
62+
|
63+
LL | let result = if b < a { 0 } else { a - b };
64+
| ^^^^^
65+
note: but then you subtract `b` from `a`
66+
--> tests/ui/manual_arithmetic_check-2.rs:15:40
67+
|
68+
LL | let result = if b < a { 0 } else { a - b };
69+
| ^^^^^
70+
71+
error: aborting due to 4 previous errors
72+
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![warn(clippy::manual_arithmetic_check)]
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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![warn(clippy::manual_arithmetic_check)]
2+
3+
fn main() {
4+
let a = 12u32;
5+
let b = 13u32;
6+
7+
let result = if a > b { a - b } else { 0 };
8+
//~^ ERROR: manual arithmetic check found
9+
let result = if b < a { a - b } else { 0 };
10+
//~^ ERROR: manual arithmetic check found
11+
12+
let result = if a < b { 0 } else { a - b };
13+
//~^ ERROR: manual arithmetic check found
14+
let result = if b > a { 0 } else { a - b };
15+
//~^ ERROR: manual arithmetic check found
16+
}
+29
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::manual-arithmetic-check` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::manual_arithmetic_check)]`
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)