]> git.lizzy.rs Git - rust.git/commitdiff
fix suggestion for floating points inequality
authorDaniele D'Orazio <daniele@develer.com>
Mon, 17 Jun 2019 14:42:41 +0000 (16:42 +0200)
committerDaniele D'Orazio <daniele@develer.com>
Mon, 17 Jun 2019 14:42:43 +0000 (16:42 +0200)
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.

clippy_lints/src/misc.rs
tests/ui/float_cmp.stderr
tests/ui/float_cmp_const.stderr

index 74368446d991298e23906b4177afff5cb8f6060b..bb43b894cd0954b7626263b5025b9e4369120981 100644 (file)
@@ -387,7 +387,11 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
                         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.");
index ddaf4b82497c01e3c58589319f0da12be68ef188..5dc5fbf0f6e8aaeac281a667f5c6904b71458048 100644 (file)
@@ -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.
@@ -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
index 0c746e24d12482100cdba1326622217cd624dfe3..48e92fa44ab8e7f1c98a0d7e8e1386282c12b393 100644 (file)
@@ -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
@@ -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