allow float_cmp when lhs is a custom type

This commit is contained in:
Meng Huang 2023-08-23 19:58:59 +08:00 committed by Meng Huang
parent 4be90d0305
commit e43c234168
No known key found for this signature in database
GPG key ID: 7763A3875644AD8A
3 changed files with 20 additions and 7 deletions

View file

@ -17,7 +17,7 @@ pub(crate) fn check<'tcx>(
left: &'tcx Expr<'_>, left: &'tcx Expr<'_>,
right: &'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) { let left_is_local = match constant_with_source(cx, cx.typeck_results(), left) {
Some((c, s)) if !is_allowed(&c) => s.is_local(), Some((c, s)) if !is_allowed(&c) => s.is_local(),
Some(_) => return, Some(_) => return,

View file

@ -41,6 +41,16 @@ impl PartialEq for X {
} }
} }
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() { fn main() {
ZERO == 0f32; //no error, comparison with zero is ok ZERO == 0f32; //no error, comparison with zero is ok
1.0f32 != f32::INFINITY; // also comparison with infinity 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 == 0.0; //no error, comparison with zero is ok
ZERO + ZERO != 1.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 == 1f32;
ONE == 1.0 + 0.0; ONE == 1.0 + 0.0;
ONE + ONE == ZERO + ONE + ONE; ONE + ONE == ZERO + ONE + ONE;

View file

@ -1,5 +1,5 @@
error: strict comparison of `f32` or `f64` 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; LL | ONE as f64 != 2.0;
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE as f64 - 2.0).abs() > error_margin` | ^^^^^^^^^^^^^^^^^ 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` = note: `-D clippy::float-cmp` implied by `-D warnings`
error: strict comparison of `f32` or `f64` error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:64:5 --> $DIR/float_cmp.rs:77:5
| |
LL | x == 1.0; LL | x == 1.0;
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 1.0).abs() < error_margin` | ^^^^^^^^ 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` = note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
error: strict comparison of `f32` or `f64` 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); 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` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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` = note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
error: strict comparison of `f32` or `f64` 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]; 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` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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` = note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
error: strict comparison of `f32` or `f64` arrays error: strict comparison of `f32` or `f64` arrays
--> $DIR/float_cmp.rs:98:5 --> $DIR/float_cmp.rs:111:5
| |
LL | a1 == a2; LL | a1 == a2;
| ^^^^^^^^ | ^^^^^^^^
@ -40,7 +40,7 @@ LL | a1 == a2;
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin` = note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
error: strict comparison of `f32` or `f64` error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:101:5 --> $DIR/float_cmp.rs:114:5
| |
LL | a1[0] == a2[0]; LL | a1[0] == a2[0];
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(a1[0] - a2[0]).abs() < error_margin` | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(a1[0] - a2[0]).abs() < error_margin`