Skip to content

Commit ab085d9

Browse files
committed
Auto merge of #4214 - d-dorazio:4204-fix-float-cmp-neq, r=flip1995
fix suggestion for floating point inequality It should be of the form `(a - b).abs() > error` whereas it was `(a - b).abs() < error` that is instead what should be used for equality. fixes #4204. changelog: fix suggestion for floating point inequality
2 parents be5d17f + be14ea8 commit ab085d9

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

clippy_lints/src/misc.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
387387
db.span_suggestion(
388388
expr.span,
389389
"consider comparing them within some error",
390-
format!("({}).abs() < error", lhs - rhs),
390+
format!(
391+
"({}).abs() {} error",
392+
lhs - rhs,
393+
if op == BinOpKind::Eq { '<' } else { '>' }
394+
),
391395
Applicability::MachineApplicable, // snippet
392396
);
393397
db.span_note(expr.span, "std::f32::EPSILON and std::f64::EPSILON are available.");

tests/ui/float_cmp.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: strict comparison of f32 or f64
22
--> $DIR/float_cmp.rs:60:5
33
|
44
LL | ONE as f64 != 2.0;
5-
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE as f64 - 2.0).abs() < error`
5+
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE as f64 - 2.0).abs() > error`
66
|
77
= note: `-D clippy::float-cmp` implied by `-D warnings`
88
note: std::f32::EPSILON and std::f64::EPSILON are available.
@@ -27,7 +27,7 @@ error: strict comparison of f32 or f64
2727
--> $DIR/float_cmp.rs:68:5
2828
|
2929
LL | twice(x) != twice(ONE as f64);
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(twice(x) - twice(ONE as f64)).abs() < error`
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(twice(x) - twice(ONE as f64)).abs() > error`
3131
|
3232
note: std::f32::EPSILON and std::f64::EPSILON are available.
3333
--> $DIR/float_cmp.rs:68:5

tests/ui/float_cmp_const.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ error: strict comparison of f32 or f64 constant
2727
--> $DIR/float_cmp_const.rs:20:5
2828
|
2929
LL | TWO != ONE;
30-
| ^^^^^^^^^^ help: consider comparing them within some error: `(TWO - ONE).abs() < error`
30+
| ^^^^^^^^^^ help: consider comparing them within some error: `(TWO - ONE).abs() > error`
3131
|
3232
note: std::f32::EPSILON and std::f64::EPSILON are available.
3333
--> $DIR/float_cmp_const.rs:20:5
@@ -75,7 +75,7 @@ error: strict comparison of f32 or f64 constant
7575
--> $DIR/float_cmp_const.rs:27:5
7676
|
7777
LL | v != ONE;
78-
| ^^^^^^^^ help: consider comparing them within some error: `(v - ONE).abs() < error`
78+
| ^^^^^^^^ help: consider comparing them within some error: `(v - ONE).abs() > error`
7979
|
8080
note: std::f32::EPSILON and std::f64::EPSILON are available.
8181
--> $DIR/float_cmp_const.rs:27:5

0 commit comments

Comments
 (0)