allow float_cmp when lhs is a custom type
This commit is contained in:
parent
4be90d0305
commit
e43c234168
@ -17,7 +17,7 @@ pub(crate) fn check<'tcx>(
|
||||
left: &'tcx Expr<'_>,
|
||||
right: &'tcx Expr<'_>,
|
||||
) {
|
||||
if (op == BinOpKind::Eq || op == BinOpKind::Ne) && (is_float(cx, left) || is_float(cx, right)) {
|
||||
if (op == BinOpKind::Eq || op == BinOpKind::Ne) && is_float(cx, left) {
|
||||
let left_is_local = match constant_with_source(cx, cx.typeck_results(), left) {
|
||||
Some((c, s)) if !is_allowed(&c) => s.is_local(),
|
||||
Some(_) => return,
|
||||
|
@ -41,6 +41,16 @@ fn eq(&self, o: &X) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<f32> for X {
|
||||
fn eq(&self, o: &f32) -> bool {
|
||||
if self.val.is_nan() {
|
||||
o.is_nan()
|
||||
} else {
|
||||
self.val == *o // no error, inside "eq" fn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
ZERO == 0f32; //no error, comparison with zero is ok
|
||||
1.0f32 != f32::INFINITY; // also comparison with infinity
|
||||
@ -48,6 +58,9 @@ fn main() {
|
||||
ZERO == 0.0; //no error, comparison with zero is ok
|
||||
ZERO + ZERO != 1.0; //no error, comparison with zero is ok
|
||||
|
||||
let x = X { val: 1.0 };
|
||||
x == 1.0; // no error, custom type that implement PartialOrder for float is not checked
|
||||
|
||||
ONE == 1f32;
|
||||
ONE == 1.0 + 0.0;
|
||||
ONE + ONE == ZERO + ONE + ONE;
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: strict comparison of `f32` or `f64`
|
||||
--> $DIR/float_cmp.rs:57:5
|
||||
--> $DIR/float_cmp.rs:70:5
|
||||
|
|
||||
LL | ONE as f64 != 2.0;
|
||||
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE as f64 - 2.0).abs() > error_margin`
|
||||
@ -8,7 +8,7 @@ LL | ONE as f64 != 2.0;
|
||||
= note: `-D clippy::float-cmp` implied by `-D warnings`
|
||||
|
||||
error: strict comparison of `f32` or `f64`
|
||||
--> $DIR/float_cmp.rs:64:5
|
||||
--> $DIR/float_cmp.rs:77:5
|
||||
|
|
||||
LL | x == 1.0;
|
||||
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 1.0).abs() < error_margin`
|
||||
@ -16,7 +16,7 @@ LL | x == 1.0;
|
||||
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
|
||||
|
||||
error: strict comparison of `f32` or `f64`
|
||||
--> $DIR/float_cmp.rs:69:5
|
||||
--> $DIR/float_cmp.rs:82:5
|
||||
|
|
||||
LL | twice(x) != twice(ONE as f64);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(twice(x) - twice(ONE as f64)).abs() > error_margin`
|
||||
@ -24,7 +24,7 @@ LL | twice(x) != twice(ONE as f64);
|
||||
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
|
||||
|
||||
error: strict comparison of `f32` or `f64`
|
||||
--> $DIR/float_cmp.rs:91:5
|
||||
--> $DIR/float_cmp.rs:104:5
|
||||
|
|
||||
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error_margin`
|
||||
@ -32,7 +32,7 @@ LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
|
||||
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
|
||||
|
||||
error: strict comparison of `f32` or `f64` arrays
|
||||
--> $DIR/float_cmp.rs:98:5
|
||||
--> $DIR/float_cmp.rs:111:5
|
||||
|
|
||||
LL | a1 == a2;
|
||||
| ^^^^^^^^
|
||||
@ -40,7 +40,7 @@ LL | a1 == a2;
|
||||
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
|
||||
|
||||
error: strict comparison of `f32` or `f64`
|
||||
--> $DIR/float_cmp.rs:101:5
|
||||
--> $DIR/float_cmp.rs:114:5
|
||||
|
|
||||
LL | a1[0] == a2[0];
|
||||
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(a1[0] - a2[0]).abs() < error_margin`
|
||||
|
Loading…
Reference in New Issue
Block a user