Skip to content

Commit 6223391

Browse files
committed
Add tests for new lint (modulo_arithmetic)
1 parent f191e91 commit 6223391

6 files changed

+565
-0
lines changed

tests/ui/modulo_arithmetic_float.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![warn(clippy::modulo_arithmetic)]
2+
#![allow(
3+
unused,
4+
clippy::shadow_reuse,
5+
clippy::shadow_unrelated,
6+
clippy::no_effect,
7+
clippy::unnecessary_operation,
8+
clippy::modulo_one
9+
)]
10+
11+
fn main() {
12+
// Lint when both sides are const and of the opposite sign
13+
-1.6 % 2.1;
14+
1.6 % -2.1;
15+
(1.1 - 2.3) % (1.1 + 2.3);
16+
(1.1 + 2.3) % (1.1 - 2.3);
17+
18+
// Lint on floating point numbers
19+
let a_f32: f32 = -1.6;
20+
let mut b_f32: f32 = 2.1;
21+
a_f32 % b_f32;
22+
b_f32 % a_f32;
23+
b_f32 %= a_f32;
24+
25+
let a_f64: f64 = -1.6;
26+
let mut b_f64: f64 = 2.1;
27+
a_f64 % b_f64;
28+
b_f64 % a_f64;
29+
b_f64 %= a_f64;
30+
31+
// No lint when both sides are const and of the same sign
32+
1.6 % 2.1;
33+
-1.6 % -2.1;
34+
(1.1 + 2.3) % (-1.1 + 2.3);
35+
(-1.1 - 2.3) % (1.1 - 2.3);
36+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
error: you are using modulo operator on constants with different signs: `-1.600 % 2.100`
2+
--> $DIR/modulo_arithmetic_float.rs:13:5
3+
|
4+
LL | -1.6 % 2.1;
5+
| ^^^^^^^^^^
6+
|
7+
= note: `-D clippy::modulo-arithmetic` implied by `-D warnings`
8+
= note: double check for expected result especially when interoperating with different languages
9+
10+
error: you are using modulo operator on constants with different signs: `1.600 % -2.100`
11+
--> $DIR/modulo_arithmetic_float.rs:14:5
12+
|
13+
LL | 1.6 % -2.1;
14+
| ^^^^^^^^^^
15+
|
16+
= note: double check for expected result especially when interoperating with different languages
17+
18+
error: you are using modulo operator on constants with different signs: `-1.200 % 3.400`
19+
--> $DIR/modulo_arithmetic_float.rs:15:5
20+
|
21+
LL | (1.1 - 2.3) % (1.1 + 2.3);
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: double check for expected result especially when interoperating with different languages
25+
26+
error: you are using modulo operator on constants with different signs: `3.400 % -1.200`
27+
--> $DIR/modulo_arithmetic_float.rs:16:5
28+
|
29+
LL | (1.1 + 2.3) % (1.1 - 2.3);
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= note: double check for expected result especially when interoperating with different languages
33+
34+
error: you are using modulo operator on types that might have different signs
35+
--> $DIR/modulo_arithmetic_float.rs:21:5
36+
|
37+
LL | a_f32 % b_f32;
38+
| ^^^^^^^^^^^^^
39+
|
40+
= note: double check for expected result especially when interoperating with different languages
41+
42+
error: you are using modulo operator on types that might have different signs
43+
--> $DIR/modulo_arithmetic_float.rs:22:5
44+
|
45+
LL | b_f32 % a_f32;
46+
| ^^^^^^^^^^^^^
47+
|
48+
= note: double check for expected result especially when interoperating with different languages
49+
50+
error: you are using modulo operator on types that might have different signs
51+
--> $DIR/modulo_arithmetic_float.rs:23:5
52+
|
53+
LL | b_f32 %= a_f32;
54+
| ^^^^^^^^^^^^^^
55+
|
56+
= note: double check for expected result especially when interoperating with different languages
57+
58+
error: you are using modulo operator on types that might have different signs
59+
--> $DIR/modulo_arithmetic_float.rs:27:5
60+
|
61+
LL | a_f64 % b_f64;
62+
| ^^^^^^^^^^^^^
63+
|
64+
= note: double check for expected result especially when interoperating with different languages
65+
66+
error: you are using modulo operator on types that might have different signs
67+
--> $DIR/modulo_arithmetic_float.rs:28:5
68+
|
69+
LL | b_f64 % a_f64;
70+
| ^^^^^^^^^^^^^
71+
|
72+
= note: double check for expected result especially when interoperating with different languages
73+
74+
error: you are using modulo operator on types that might have different signs
75+
--> $DIR/modulo_arithmetic_float.rs:29:5
76+
|
77+
LL | b_f64 %= a_f64;
78+
| ^^^^^^^^^^^^^^
79+
|
80+
= note: double check for expected result especially when interoperating with different languages
81+
82+
error: aborting due to 10 previous errors
83+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#![warn(clippy::modulo_arithmetic)]
2+
#![allow(
3+
unused,
4+
clippy::shadow_reuse,
5+
clippy::shadow_unrelated,
6+
clippy::no_effect,
7+
clippy::unnecessary_operation,
8+
clippy::modulo_one
9+
)]
10+
11+
fn main() {
12+
// Lint on signed integral numbers
13+
let a = -1;
14+
let mut b = 2;
15+
a % b;
16+
b % a;
17+
b %= a;
18+
19+
let a_i8: i8 = 1;
20+
let mut b_i8: i8 = 2;
21+
a_i8 % b_i8;
22+
b_i8 %= a_i8;
23+
24+
let a_i16: i16 = 1;
25+
let mut b_i16: i16 = 2;
26+
a_i16 % b_i16;
27+
b_i16 %= a_i16;
28+
29+
let a_i32: i32 = 1;
30+
let mut b_i32: i32 = 2;
31+
a_i32 % b_i32;
32+
b_i32 %= a_i32;
33+
34+
let a_i64: i64 = 1;
35+
let mut b_i64: i64 = 2;
36+
a_i64 % b_i64;
37+
b_i64 %= a_i64;
38+
39+
let a_i128: i128 = 1;
40+
let mut b_i128: i128 = 2;
41+
a_i128 % b_i128;
42+
b_i128 %= a_i128;
43+
44+
let a_isize: isize = 1;
45+
let mut b_isize: isize = 2;
46+
a_isize % b_isize;
47+
b_isize %= a_isize;
48+
49+
let a = 1;
50+
let mut b = 2;
51+
a % b;
52+
b %= a;
53+
54+
// No lint on unsigned integral value
55+
let a_u8: u8 = 17;
56+
let b_u8: u8 = 3;
57+
a_u8 % b_u8;
58+
let mut a_u8: u8 = 1;
59+
a_u8 %= 2;
60+
61+
let a_u16: u16 = 17;
62+
let b_u16: u16 = 3;
63+
a_u16 % b_u16;
64+
let mut a_u16: u16 = 1;
65+
a_u16 %= 2;
66+
67+
let a_u32: u32 = 17;
68+
let b_u32: u32 = 3;
69+
a_u32 % b_u32;
70+
let mut a_u32: u32 = 1;
71+
a_u32 %= 2;
72+
73+
let a_u64: u64 = 17;
74+
let b_u64: u64 = 3;
75+
a_u64 % b_u64;
76+
let mut a_u64: u64 = 1;
77+
a_u64 %= 2;
78+
79+
let a_u128: u128 = 17;
80+
let b_u128: u128 = 3;
81+
a_u128 % b_u128;
82+
let mut a_u128: u128 = 1;
83+
a_u128 %= 2;
84+
85+
let a_usize: usize = 17;
86+
let b_usize: usize = 3;
87+
a_usize % b_usize;
88+
let mut a_usize: usize = 1;
89+
a_usize %= 2;
90+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
error: you are using modulo operator on types that might have different signs
2+
--> $DIR/modulo_arithmetic_integral.rs:15:5
3+
|
4+
LL | a % b;
5+
| ^^^^^
6+
|
7+
= note: `-D clippy::modulo-arithmetic` implied by `-D warnings`
8+
= note: double check for expected result especially when interoperating with different languages
9+
= note: or consider using `rem_euclid` or similar function
10+
11+
error: you are using modulo operator on types that might have different signs
12+
--> $DIR/modulo_arithmetic_integral.rs:16:5
13+
|
14+
LL | b % a;
15+
| ^^^^^
16+
|
17+
= note: double check for expected result especially when interoperating with different languages
18+
= note: or consider using `rem_euclid` or similar function
19+
20+
error: you are using modulo operator on types that might have different signs
21+
--> $DIR/modulo_arithmetic_integral.rs:17:5
22+
|
23+
LL | b %= a;
24+
| ^^^^^^
25+
|
26+
= note: double check for expected result especially when interoperating with different languages
27+
= note: or consider using `rem_euclid` or similar function
28+
29+
error: you are using modulo operator on types that might have different signs
30+
--> $DIR/modulo_arithmetic_integral.rs:21:5
31+
|
32+
LL | a_i8 % b_i8;
33+
| ^^^^^^^^^^^
34+
|
35+
= note: double check for expected result especially when interoperating with different languages
36+
= note: or consider using `rem_euclid` or similar function
37+
38+
error: you are using modulo operator on types that might have different signs
39+
--> $DIR/modulo_arithmetic_integral.rs:22:5
40+
|
41+
LL | b_i8 %= a_i8;
42+
| ^^^^^^^^^^^^
43+
|
44+
= note: double check for expected result especially when interoperating with different languages
45+
= note: or consider using `rem_euclid` or similar function
46+
47+
error: you are using modulo operator on types that might have different signs
48+
--> $DIR/modulo_arithmetic_integral.rs:26:5
49+
|
50+
LL | a_i16 % b_i16;
51+
| ^^^^^^^^^^^^^
52+
|
53+
= note: double check for expected result especially when interoperating with different languages
54+
= note: or consider using `rem_euclid` or similar function
55+
56+
error: you are using modulo operator on types that might have different signs
57+
--> $DIR/modulo_arithmetic_integral.rs:27:5
58+
|
59+
LL | b_i16 %= a_i16;
60+
| ^^^^^^^^^^^^^^
61+
|
62+
= note: double check for expected result especially when interoperating with different languages
63+
= note: or consider using `rem_euclid` or similar function
64+
65+
error: you are using modulo operator on types that might have different signs
66+
--> $DIR/modulo_arithmetic_integral.rs:31:5
67+
|
68+
LL | a_i32 % b_i32;
69+
| ^^^^^^^^^^^^^
70+
|
71+
= note: double check for expected result especially when interoperating with different languages
72+
= note: or consider using `rem_euclid` or similar function
73+
74+
error: you are using modulo operator on types that might have different signs
75+
--> $DIR/modulo_arithmetic_integral.rs:32:5
76+
|
77+
LL | b_i32 %= a_i32;
78+
| ^^^^^^^^^^^^^^
79+
|
80+
= note: double check for expected result especially when interoperating with different languages
81+
= note: or consider using `rem_euclid` or similar function
82+
83+
error: you are using modulo operator on types that might have different signs
84+
--> $DIR/modulo_arithmetic_integral.rs:36:5
85+
|
86+
LL | a_i64 % b_i64;
87+
| ^^^^^^^^^^^^^
88+
|
89+
= note: double check for expected result especially when interoperating with different languages
90+
= note: or consider using `rem_euclid` or similar function
91+
92+
error: you are using modulo operator on types that might have different signs
93+
--> $DIR/modulo_arithmetic_integral.rs:37:5
94+
|
95+
LL | b_i64 %= a_i64;
96+
| ^^^^^^^^^^^^^^
97+
|
98+
= note: double check for expected result especially when interoperating with different languages
99+
= note: or consider using `rem_euclid` or similar function
100+
101+
error: you are using modulo operator on types that might have different signs
102+
--> $DIR/modulo_arithmetic_integral.rs:41:5
103+
|
104+
LL | a_i128 % b_i128;
105+
| ^^^^^^^^^^^^^^^
106+
|
107+
= note: double check for expected result especially when interoperating with different languages
108+
= note: or consider using `rem_euclid` or similar function
109+
110+
error: you are using modulo operator on types that might have different signs
111+
--> $DIR/modulo_arithmetic_integral.rs:42:5
112+
|
113+
LL | b_i128 %= a_i128;
114+
| ^^^^^^^^^^^^^^^^
115+
|
116+
= note: double check for expected result especially when interoperating with different languages
117+
= note: or consider using `rem_euclid` or similar function
118+
119+
error: you are using modulo operator on types that might have different signs
120+
--> $DIR/modulo_arithmetic_integral.rs:46:5
121+
|
122+
LL | a_isize % b_isize;
123+
| ^^^^^^^^^^^^^^^^^
124+
|
125+
= note: double check for expected result especially when interoperating with different languages
126+
= note: or consider using `rem_euclid` or similar function
127+
128+
error: you are using modulo operator on types that might have different signs
129+
--> $DIR/modulo_arithmetic_integral.rs:47:5
130+
|
131+
LL | b_isize %= a_isize;
132+
| ^^^^^^^^^^^^^^^^^^
133+
|
134+
= note: double check for expected result especially when interoperating with different languages
135+
= note: or consider using `rem_euclid` or similar function
136+
137+
error: you are using modulo operator on types that might have different signs
138+
--> $DIR/modulo_arithmetic_integral.rs:51:5
139+
|
140+
LL | a % b;
141+
| ^^^^^
142+
|
143+
= note: double check for expected result especially when interoperating with different languages
144+
= note: or consider using `rem_euclid` or similar function
145+
146+
error: you are using modulo operator on types that might have different signs
147+
--> $DIR/modulo_arithmetic_integral.rs:52:5
148+
|
149+
LL | b %= a;
150+
| ^^^^^^
151+
|
152+
= note: double check for expected result especially when interoperating with different languages
153+
= note: or consider using `rem_euclid` or similar function
154+
155+
error: aborting due to 17 previous errors
156+

0 commit comments

Comments
 (0)