Skip to content

Commit 5bb41b2

Browse files
committed
float_cmp: Ensure both sides are a float type.
1 parent fde377a commit 5bb41b2

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

clippy_lints/src/operators/float_cmp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) fn check<'tcx>(
2929
&& let left_reduced = peel_hir_expr_while(left, peel_expr)
3030
&& let right_reduced = peel_hir_expr_while(right, peel_expr)
3131
&& is_float(cx, left_reduced)
32+
&& is_float(cx, right_reduced)
3233
// Don't lint literal comparisons
3334
&& !(matches!(left_reduced.kind, ExprKind::Lit(_)) && matches!(right_reduced.kind, ExprKind::Lit(_)))
3435
// Allow comparing the results of signum()

tests/ui-toml/float_cmp/test.change_detect.stderr

+21-21
Original file line numberDiff line numberDiff line change
@@ -167,127 +167,127 @@ LL | let _ = C[1] == x;
167167
| ^^^^^^^^^ help: consider comparing them within some margin of error: `(C[1] - x).abs() < error_margin`
168168

169169
error: strict comparison of `f32` or `f64`
170-
--> tests/ui-toml/float_cmp/test.rs:262:21
170+
--> tests/ui-toml/float_cmp/test.rs:268:21
171171
|
172172
LL | let _ = x == x + 1.0;
173173
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - (x + 1.0)).abs() < error_margin`
174174

175175
error: strict comparison of `f32` or `f64`
176-
--> tests/ui-toml/float_cmp/test.rs:263:21
176+
--> tests/ui-toml/float_cmp/test.rs:269:21
177177
|
178178
LL | let _ = x + 1.0 == x;
179179
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x + 1.0 - x).abs() < error_margin`
180180

181181
error: strict comparison of `f32` or `f64`
182-
--> tests/ui-toml/float_cmp/test.rs:264:21
182+
--> tests/ui-toml/float_cmp/test.rs:270:21
183183
|
184184
LL | let _ = -x == -x + 1.0;
185185
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(-x - (-x + 1.0)).abs() < error_margin`
186186

187187
error: strict comparison of `f32` or `f64`
188-
--> tests/ui-toml/float_cmp/test.rs:265:21
188+
--> tests/ui-toml/float_cmp/test.rs:271:21
189189
|
190190
LL | let _ = -x + 1.0 == -x;
191191
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(-x + 1.0 - -x).abs() < error_margin`
192192

193193
error: strict comparison of `f32` or `f64`
194-
--> tests/ui-toml/float_cmp/test.rs:266:21
194+
--> tests/ui-toml/float_cmp/test.rs:272:21
195195
|
196196
LL | let _ = x == f1(x);
197197
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - f1(x)).abs() < error_margin`
198198

199199
error: strict comparison of `f32` or `f64`
200-
--> tests/ui-toml/float_cmp/test.rs:267:21
200+
--> tests/ui-toml/float_cmp/test.rs:273:21
201201
|
202202
LL | let _ = f1(x) == x;
203203
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(x) - x).abs() < error_margin`
204204

205205
error: strict comparison of `f32` or `f64`
206-
--> tests/ui-toml/float_cmp/test.rs:268:21
206+
--> tests/ui-toml/float_cmp/test.rs:274:21
207207
|
208208
LL | let _ = x == f2(x, y);
209209
| ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - f2(x, y)).abs() < error_margin`
210210

211211
error: strict comparison of `f32` or `f64`
212-
--> tests/ui-toml/float_cmp/test.rs:269:21
212+
--> tests/ui-toml/float_cmp/test.rs:275:21
213213
|
214214
LL | let _ = f2(x, y) == x;
215215
| ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f2(x, y) - x).abs() < error_margin`
216216

217217
error: strict comparison of `f32` or `f64`
218-
--> tests/ui-toml/float_cmp/test.rs:270:21
218+
--> tests/ui-toml/float_cmp/test.rs:276:21
219219
|
220220
LL | let _ = f1(f1(x)) == f1(x);
221221
| ^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(f1(x)) - f1(x)).abs() < error_margin`
222222

223223
error: strict comparison of `f32` or `f64`
224-
--> tests/ui-toml/float_cmp/test.rs:271:21
224+
--> tests/ui-toml/float_cmp/test.rs:277:21
225225
|
226226
LL | let _ = f1(x) == f1(f1(x));
227227
| ^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(x) - f1(f1(x))).abs() < error_margin`
228228

229229
error: strict comparison of `f32` or `f64`
230-
--> tests/ui-toml/float_cmp/test.rs:274:21
230+
--> tests/ui-toml/float_cmp/test.rs:280:21
231231
|
232232
LL | let _ = z.0 == z.0 + 1.0;
233233
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(z.0 - (z.0 + 1.0)).abs() < error_margin`
234234

235235
error: strict comparison of `f32` or `f64`
236-
--> tests/ui-toml/float_cmp/test.rs:275:21
236+
--> tests/ui-toml/float_cmp/test.rs:281:21
237237
|
238238
LL | let _ = z.0 + 1.0 == z.0;
239239
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(z.0 + 1.0 - z.0).abs() < error_margin`
240240

241241
error: strict comparison of `f32` or `f64`
242-
--> tests/ui-toml/float_cmp/test.rs:279:21
242+
--> tests/ui-toml/float_cmp/test.rs:285:21
243243
|
244244
LL | let _ = *x + 1.0 == *x;
245245
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x + 1.0 - *x).abs() < error_margin`
246246

247247
error: strict comparison of `f32` or `f64`
248-
--> tests/ui-toml/float_cmp/test.rs:280:21
248+
--> tests/ui-toml/float_cmp/test.rs:286:21
249249
|
250250
LL | let _ = *x == *x + 1.0;
251251
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x - (*x + 1.0)).abs() < error_margin`
252252

253253
error: strict comparison of `f32` or `f64`
254-
--> tests/ui-toml/float_cmp/test.rs:281:21
254+
--> tests/ui-toml/float_cmp/test.rs:287:21
255255
|
256256
LL | let _ = *x == f1(*x);
257257
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x - f1(*x)).abs() < error_margin`
258258

259259
error: strict comparison of `f32` or `f64`
260-
--> tests/ui-toml/float_cmp/test.rs:282:21
260+
--> tests/ui-toml/float_cmp/test.rs:288:21
261261
|
262262
LL | let _ = f1(*x) == *x;
263263
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(*x) - *x).abs() < error_margin`
264264

265265
error: strict comparison of `f32` or `f64`
266-
--> tests/ui-toml/float_cmp/test.rs:287:21
266+
--> tests/ui-toml/float_cmp/test.rs:293:21
267267
|
268268
LL | let _ = x.next().unwrap() == x.next().unwrap() + 1.0;
269269
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.next().unwrap() - (x.next().unwrap() + 1.0)).abs() < error_margin`
270270

271271
error: strict comparison of `f32` or `f64`
272-
--> tests/ui-toml/float_cmp/test.rs:303:21
272+
--> tests/ui-toml/float_cmp/test.rs:309:21
273273
|
274274
LL | let _ = x.f() + 1.0 == x.f();
275275
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() + 1.0 - x.f()).abs() < error_margin`
276276

277277
error: strict comparison of `f32` or `f64`
278-
--> tests/ui-toml/float_cmp/test.rs:304:21
278+
--> tests/ui-toml/float_cmp/test.rs:310:21
279279
|
280280
LL | let _ = x.f() == x.f() + 1.0;
281281
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() - (x.f() + 1.0)).abs() < error_margin`
282282

283283
error: strict comparison of `f32` or `f64`
284-
--> tests/ui-toml/float_cmp/test.rs:309:17
284+
--> tests/ui-toml/float_cmp/test.rs:315:17
285285
|
286286
LL | let _ = f(1.0) == f(1.0) + 1.0;
287287
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin`
288288

289289
error: strict comparison of `f32` or `f64`
290-
--> tests/ui-toml/float_cmp/test.rs:313:17
290+
--> tests/ui-toml/float_cmp/test.rs:319:17
291291
|
292292
LL | let _ = f(1.0) == f(1.0) + 1.0;
293293
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin`

tests/ui-toml/float_cmp/test.const_cmp.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,25 @@ LL | let _ = C[1] == x;
185185
| ^^^^^^^^^ help: consider comparing them within some margin of error: `(C[1] - x).abs() < error_margin`
186186

187187
error: strict comparison of `f32` or `f64`
188-
--> tests/ui-toml/float_cmp/test.rs:287:21
188+
--> tests/ui-toml/float_cmp/test.rs:293:21
189189
|
190190
LL | let _ = x.next().unwrap() == x.next().unwrap() + 1.0;
191191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.next().unwrap() - (x.next().unwrap() + 1.0)).abs() < error_margin`
192192

193193
error: strict comparison of `f32` or `f64`
194-
--> tests/ui-toml/float_cmp/test.rs:303:21
194+
--> tests/ui-toml/float_cmp/test.rs:309:21
195195
|
196196
LL | let _ = x.f() + 1.0 == x.f();
197197
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() + 1.0 - x.f()).abs() < error_margin`
198198

199199
error: strict comparison of `f32` or `f64`
200-
--> tests/ui-toml/float_cmp/test.rs:304:21
200+
--> tests/ui-toml/float_cmp/test.rs:310:21
201201
|
202202
LL | let _ = x.f() == x.f() + 1.0;
203203
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() - (x.f() + 1.0)).abs() < error_margin`
204204

205205
error: strict comparison of `f32` or `f64`
206-
--> tests/ui-toml/float_cmp/test.rs:313:17
206+
--> tests/ui-toml/float_cmp/test.rs:319:17
207207
|
208208
LL | let _ = f(1.0) == f(1.0) + 1.0;
209209
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin`

tests/ui-toml/float_cmp/test.named_const.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,25 @@ LL | let _ = C[1] == x;
227227
| ^^^^^^^^^ help: consider comparing them within some margin of error: `(C[1] - x).abs() < error_margin`
228228

229229
error: strict comparison of `f32` or `f64`
230-
--> tests/ui-toml/float_cmp/test.rs:287:21
230+
--> tests/ui-toml/float_cmp/test.rs:293:21
231231
|
232232
LL | let _ = x.next().unwrap() == x.next().unwrap() + 1.0;
233233
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.next().unwrap() - (x.next().unwrap() + 1.0)).abs() < error_margin`
234234

235235
error: strict comparison of `f32` or `f64`
236-
--> tests/ui-toml/float_cmp/test.rs:303:21
236+
--> tests/ui-toml/float_cmp/test.rs:309:21
237237
|
238238
LL | let _ = x.f() + 1.0 == x.f();
239239
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() + 1.0 - x.f()).abs() < error_margin`
240240

241241
error: strict comparison of `f32` or `f64`
242-
--> tests/ui-toml/float_cmp/test.rs:304:21
242+
--> tests/ui-toml/float_cmp/test.rs:310:21
243243
|
244244
LL | let _ = x.f() == x.f() + 1.0;
245245
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() - (x.f() + 1.0)).abs() < error_margin`
246246

247247
error: strict comparison of `f32` or `f64`
248-
--> tests/ui-toml/float_cmp/test.rs:313:17
248+
--> tests/ui-toml/float_cmp/test.rs:319:17
249249
|
250250
LL | let _ = f(1.0) == f(1.0) + 1.0;
251251
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin`

tests/ui-toml/float_cmp/test.rs

+6
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,15 @@ fn main() {
242242
false
243243
}
244244
}
245+
impl PartialEq<S> for f32 {
246+
fn eq(&self, _: &S) -> bool {
247+
false
248+
}
249+
}
245250

246251
fn _f(x: S, y: f32) {
247252
let _ = x == y;
253+
let _ = y == x;
248254
}
249255
}
250256

0 commit comments

Comments
 (0)