Skip to content

Commit 4e66671

Browse files
committed
Add note about rem_euclid function for integral types
1 parent f1eda39 commit 4e66671

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

clippy_lints/src/modulo_arithmetic.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ModuloArithmetic {
9696
MODULO_ARITHMETIC,
9797
expr.span,
9898
&format!(
99-
"you are using modulo operator on constants with different signs: `{} % {}`", lhs_operand.string_representation.unwrap(), rhs_operand.string_representation.unwrap()
99+
"you are using modulo operator on constants with different signs: `{} % {}`",
100+
lhs_operand.string_representation.unwrap(),
101+
rhs_operand.string_representation.unwrap()
100102
),
101103
expr.span,
102-
"double check for expected result especially when interoperating with different languages",
104+
&format!(
105+
"double check for expected result especially when interoperating with different languages{}",
106+
if lhs_operand.is_integral {" or consider using `rem_euclid` or similar function"} else {""}
107+
),
103108
);
104109
}
105110
}
@@ -113,7 +118,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ModuloArithmetic {
113118
expr.span,
114119
"you are using modulo operator on types that might have different signs",
115120
expr.span,
116-
"double check for expected result especially when interoperating with different languages",
121+
&format!(
122+
"double check for expected result especially when interoperating with different languages{}",
123+
if lhs_type.is_integral() {" or consider using `rem_euclid` or similar function"} else {""}
124+
),
117125
);
118126
}
119127
}

tests/ui/modulo_arithmetic.stderr

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ LL | -1 % 2;
55
| ^^^^^^
66
|
77
= note: `-D clippy::modulo-arithmetic` implied by `-D warnings`
8-
= note: double check for expected result especially when interoperating with different languages
8+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
99

1010
error: you are using modulo operator on constants with different signs: `1 % -2`
1111
--> $DIR/modulo_arithmetic.rs:14:5
1212
|
1313
LL | 1 % -2;
1414
| ^^^^^^
1515
|
16-
= note: double check for expected result especially when interoperating with different languages
16+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
1717

1818
error: you are using modulo operator on constants with different signs: `-1.600 % 2.100`
1919
--> $DIR/modulo_arithmetic.rs:15:5
@@ -37,15 +37,15 @@ error: you are using modulo operator on constants with different signs: `-1 % 3`
3737
LL | (1 - 2) % (1 + 2);
3838
| ^^^^^^^^^^^^^^^^^
3939
|
40-
= note: double check for expected result especially when interoperating with different languages
40+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
4141

4242
error: you are using modulo operator on constants with different signs: `3 % -1`
4343
--> $DIR/modulo_arithmetic.rs:18:5
4444
|
4545
LL | (1 + 2) % (1 - 2);
4646
| ^^^^^^^^^^^^^^^^^
4747
|
48-
= note: double check for expected result especially when interoperating with different languages
48+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
4949

5050
error: you are using modulo operator on constants with different signs: `-1.200 % 3.400`
5151
--> $DIR/modulo_arithmetic.rs:19:5
@@ -69,127 +69,127 @@ error: you are using modulo operator on constants with different signs: `-35 % 3
6969
LL | 35 * (7 - 4 * 2) % (-500 * -600);
7070
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7171
|
72-
= note: double check for expected result especially when interoperating with different languages
72+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
7373

7474
error: you are using modulo operator on constants with different signs: `-1 % 2`
7575
--> $DIR/modulo_arithmetic.rs:23:5
7676
|
7777
LL | -1i8 % 2i8;
7878
| ^^^^^^^^^^
7979
|
80-
= note: double check for expected result especially when interoperating with different languages
80+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
8181

8282
error: you are using modulo operator on constants with different signs: `1 % -2`
8383
--> $DIR/modulo_arithmetic.rs:24:5
8484
|
8585
LL | 1i8 % -2i8;
8686
| ^^^^^^^^^^
8787
|
88-
= note: double check for expected result especially when interoperating with different languages
88+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
8989

9090
error: you are using modulo operator on constants with different signs: `-1 % 2`
9191
--> $DIR/modulo_arithmetic.rs:25:5
9292
|
9393
LL | -1i16 % 2i16;
9494
| ^^^^^^^^^^^^
9595
|
96-
= note: double check for expected result especially when interoperating with different languages
96+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
9797

9898
error: you are using modulo operator on constants with different signs: `1 % -2`
9999
--> $DIR/modulo_arithmetic.rs:26:5
100100
|
101101
LL | 1i16 % -2i16;
102102
| ^^^^^^^^^^^^
103103
|
104-
= note: double check for expected result especially when interoperating with different languages
104+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
105105

106106
error: you are using modulo operator on constants with different signs: `-1 % 2`
107107
--> $DIR/modulo_arithmetic.rs:27:5
108108
|
109109
LL | -1i32 % 2i32;
110110
| ^^^^^^^^^^^^
111111
|
112-
= note: double check for expected result especially when interoperating with different languages
112+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
113113

114114
error: you are using modulo operator on constants with different signs: `1 % -2`
115115
--> $DIR/modulo_arithmetic.rs:28:5
116116
|
117117
LL | 1i32 % -2i32;
118118
| ^^^^^^^^^^^^
119119
|
120-
= note: double check for expected result especially when interoperating with different languages
120+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
121121

122122
error: you are using modulo operator on constants with different signs: `-1 % 2`
123123
--> $DIR/modulo_arithmetic.rs:29:5
124124
|
125125
LL | -1i64 % 2i64;
126126
| ^^^^^^^^^^^^
127127
|
128-
= note: double check for expected result especially when interoperating with different languages
128+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
129129

130130
error: you are using modulo operator on constants with different signs: `1 % -2`
131131
--> $DIR/modulo_arithmetic.rs:30:5
132132
|
133133
LL | 1i64 % -2i64;
134134
| ^^^^^^^^^^^^
135135
|
136-
= note: double check for expected result especially when interoperating with different languages
136+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
137137

138138
error: you are using modulo operator on constants with different signs: `-1 % 2`
139139
--> $DIR/modulo_arithmetic.rs:31:5
140140
|
141141
LL | -1i128 % 2i128;
142142
| ^^^^^^^^^^^^^^
143143
|
144-
= note: double check for expected result especially when interoperating with different languages
144+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
145145

146146
error: you are using modulo operator on constants with different signs: `1 % -2`
147147
--> $DIR/modulo_arithmetic.rs:32:5
148148
|
149149
LL | 1i128 % -2i128;
150150
| ^^^^^^^^^^^^^^
151151
|
152-
= note: double check for expected result especially when interoperating with different languages
152+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
153153

154154
error: you are using modulo operator on constants with different signs: `-1 % 2`
155155
--> $DIR/modulo_arithmetic.rs:33:5
156156
|
157157
LL | -1isize % 2isize;
158158
| ^^^^^^^^^^^^^^^^
159159
|
160-
= note: double check for expected result especially when interoperating with different languages
160+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
161161

162162
error: you are using modulo operator on constants with different signs: `1 % -2`
163163
--> $DIR/modulo_arithmetic.rs:34:5
164164
|
165165
LL | 1isize % -2isize;
166166
| ^^^^^^^^^^^^^^^^
167167
|
168-
= note: double check for expected result especially when interoperating with different languages
168+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
169169

170170
error: you are using modulo operator on types that might have different signs
171171
--> $DIR/modulo_arithmetic.rs:39:5
172172
|
173173
LL | a % b;
174174
| ^^^^^
175175
|
176-
= note: double check for expected result especially when interoperating with different languages
176+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
177177

178178
error: you are using modulo operator on types that might have different signs
179179
--> $DIR/modulo_arithmetic.rs:40:5
180180
|
181181
LL | b % a;
182182
| ^^^^^
183183
|
184-
= note: double check for expected result especially when interoperating with different languages
184+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
185185

186186
error: you are using modulo operator on types that might have different signs
187187
--> $DIR/modulo_arithmetic.rs:41:5
188188
|
189189
LL | b %= a;
190190
| ^^^^^^
191191
|
192-
= note: double check for expected result especially when interoperating with different languages
192+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
193193

194194
error: you are using modulo operator on types that might have different signs
195195
--> $DIR/modulo_arithmetic.rs:45:5
@@ -245,15 +245,15 @@ error: you are using modulo operator on types that might have different signs
245245
LL | a % b;
246246
| ^^^^^
247247
|
248-
= note: double check for expected result especially when interoperating with different languages
248+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
249249

250250
error: you are using modulo operator on types that might have different signs
251251
--> $DIR/modulo_arithmetic.rs:58:5
252252
|
253253
LL | b %= a;
254254
| ^^^^^^
255255
|
256-
= note: double check for expected result especially when interoperating with different languages
256+
= note: double check for expected result especially when interoperating with different languages or consider using `rem_euclid` or similar function
257257

258258
error: aborting due to 32 previous errors
259259

0 commit comments

Comments
 (0)