Skip to content

Commit e43c234

Browse files
committed
allow float_cmp when lhs is a custom type
1 parent 4be90d0 commit e43c234

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

clippy_lints/src/operators/float_cmp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) fn check<'tcx>(
1717
left: &'tcx Expr<'_>,
1818
right: &'tcx Expr<'_>,
1919
) {
20-
if (op == BinOpKind::Eq || op == BinOpKind::Ne) && (is_float(cx, left) || is_float(cx, right)) {
20+
if (op == BinOpKind::Eq || op == BinOpKind::Ne) && is_float(cx, left) {
2121
let left_is_local = match constant_with_source(cx, cx.typeck_results(), left) {
2222
Some((c, s)) if !is_allowed(&c) => s.is_local(),
2323
Some(_) => return,

tests/ui/float_cmp.rs

+13
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,26 @@ impl PartialEq for X {
4141
}
4242
}
4343

44+
impl PartialEq<f32> for X {
45+
fn eq(&self, o: &f32) -> bool {
46+
if self.val.is_nan() {
47+
o.is_nan()
48+
} else {
49+
self.val == *o // no error, inside "eq" fn
50+
}
51+
}
52+
}
53+
4454
fn main() {
4555
ZERO == 0f32; //no error, comparison with zero is ok
4656
1.0f32 != f32::INFINITY; // also comparison with infinity
4757
1.0f32 != f32::NEG_INFINITY; // and negative infinity
4858
ZERO == 0.0; //no error, comparison with zero is ok
4959
ZERO + ZERO != 1.0; //no error, comparison with zero is ok
5060

61+
let x = X { val: 1.0 };
62+
x == 1.0; // no error, custom type that implement PartialOrder for float is not checked
63+
5164
ONE == 1f32;
5265
ONE == 1.0 + 0.0;
5366
ONE + ONE == ZERO + ONE + ONE;

tests/ui/float_cmp.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: strict comparison of `f32` or `f64`
2-
--> $DIR/float_cmp.rs:57:5
2+
--> $DIR/float_cmp.rs:70:5
33
|
44
LL | ONE as f64 != 2.0;
55
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE as f64 - 2.0).abs() > error_margin`
@@ -8,39 +8,39 @@ LL | ONE as f64 != 2.0;
88
= note: `-D clippy::float-cmp` implied by `-D warnings`
99

1010
error: strict comparison of `f32` or `f64`
11-
--> $DIR/float_cmp.rs:64:5
11+
--> $DIR/float_cmp.rs:77:5
1212
|
1313
LL | x == 1.0;
1414
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 1.0).abs() < error_margin`
1515
|
1616
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
1717

1818
error: strict comparison of `f32` or `f64`
19-
--> $DIR/float_cmp.rs:69:5
19+
--> $DIR/float_cmp.rs:82:5
2020
|
2121
LL | twice(x) != twice(ONE as f64);
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(twice(x) - twice(ONE as f64)).abs() > error_margin`
2323
|
2424
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
2525

2626
error: strict comparison of `f32` or `f64`
27-
--> $DIR/float_cmp.rs:91:5
27+
--> $DIR/float_cmp.rs:104:5
2828
|
2929
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error_margin`
3131
|
3232
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
3333

3434
error: strict comparison of `f32` or `f64` arrays
35-
--> $DIR/float_cmp.rs:98:5
35+
--> $DIR/float_cmp.rs:111:5
3636
|
3737
LL | a1 == a2;
3838
| ^^^^^^^^
3939
|
4040
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
4141

4242
error: strict comparison of `f32` or `f64`
43-
--> $DIR/float_cmp.rs:101:5
43+
--> $DIR/float_cmp.rs:114:5
4444
|
4545
LL | a1[0] == a2[0];
4646
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(a1[0] - a2[0]).abs() < error_margin`

0 commit comments

Comments
 (0)