Skip to content

Commit

Permalink
Auto merge of rust-lang#4214 - d-dorazio:4204-fix-float-cmp-neq, r=fl…
Browse files Browse the repository at this point in the history
…ip1995

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 rust-lang#4204.

changelog: fix suggestion for floating point inequality
  • Loading branch information
bors committed Jun 17, 2019
2 parents be5d17f + be14ea8 commit ab085d9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 5 additions & 1 deletion clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
db.span_suggestion(
expr.span,
"consider comparing them within some error",
format!("({}).abs() < error", lhs - rhs),
format!(
"({}).abs() {} error",
lhs - rhs,
if op == BinOpKind::Eq { '<' } else { '>' }
),
Applicability::MachineApplicable, // snippet
);
db.span_note(expr.span, "std::f32::EPSILON and std::f64::EPSILON are available.");
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/float_cmp.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: strict comparison of f32 or f64
--> $DIR/float_cmp.rs:60:5
|
LL | ONE as f64 != 2.0;
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE as f64 - 2.0).abs() < error`
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE as f64 - 2.0).abs() > error`
|
= note: `-D clippy::float-cmp` implied by `-D warnings`
note: std::f32::EPSILON and std::f64::EPSILON are available.
Expand All @@ -27,7 +27,7 @@ error: strict comparison of f32 or f64
--> $DIR/float_cmp.rs:68:5
|
LL | twice(x) != twice(ONE as f64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(twice(x) - twice(ONE as f64)).abs() < error`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(twice(x) - twice(ONE as f64)).abs() > error`
|
note: std::f32::EPSILON and std::f64::EPSILON are available.
--> $DIR/float_cmp.rs:68:5
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/float_cmp_const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ error: strict comparison of f32 or f64 constant
--> $DIR/float_cmp_const.rs:20:5
|
LL | TWO != ONE;
| ^^^^^^^^^^ help: consider comparing them within some error: `(TWO - ONE).abs() < error`
| ^^^^^^^^^^ help: consider comparing them within some error: `(TWO - ONE).abs() > error`
|
note: std::f32::EPSILON and std::f64::EPSILON are available.
--> $DIR/float_cmp_const.rs:20:5
Expand Down Expand Up @@ -75,7 +75,7 @@ error: strict comparison of f32 or f64 constant
--> $DIR/float_cmp_const.rs:27:5
|
LL | v != ONE;
| ^^^^^^^^ help: consider comparing them within some error: `(v - ONE).abs() < error`
| ^^^^^^^^ help: consider comparing them within some error: `(v - ONE).abs() > error`
|
note: std::f32::EPSILON and std::f64::EPSILON are available.
--> $DIR/float_cmp_const.rs:27:5
Expand Down

0 comments on commit ab085d9

Please sign in to comment.