about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-08-26 18:15:42 +0000
committerbors <bors@rust-lang.org>2020-08-26 18:15:42 +0000
commit64c4bb0d2bac5f7810d7d0fa6ad846417b3ca60a (patch)
treeef8d2c80f564b3f3123ee6e38da6ecd70f79d6f5
parent894581b872ef8f87074815454f25a262d4e699f4 (diff)
parent2d853148d72d49956052cf1cdb5f3be18c85a9fc (diff)
downloadrust-64c4bb0d2bac5f7810d7d0fa6ad846417b3ca60a.tar.gz
rust-64c4bb0d2bac5f7810d7d0fa6ad846417b3ca60a.zip
Auto merge of #5966 - 1c3t3a:1c3t3a-dev-5819-fix, r=Manishearth
Corrects the float_equality_without_abs lint

Fixes an issue in the `float_equality_without_abs` lint. The lint suggestion was configured in a way that it lints the whole error and not just the subtraction part. In the current configuration the lint would suggest to change the expression in a wrong way, e.g.
```rust
let _ = (a - b) < f32::EPSILON; // before
let _ = (a - b).abs(); // after
```
This was dicovered by @flip1995. (See discussion of PR #5952).

Also the suggestion is now formatted via `utils::sugg`.
changelog: none
-rw-r--r--clippy_lints/src/float_equality_without_abs.rs30
-rw-r--r--tests/ui/float_equality_without_abs.stderr44
2 files changed, 47 insertions, 27 deletions
diff --git a/clippy_lints/src/float_equality_without_abs.rs b/clippy_lints/src/float_equality_without_abs.rs
index dc1c3bfc9ff..9ac5a45eb45 100644
--- a/clippy_lints/src/float_equality_without_abs.rs
+++ b/clippy_lints/src/float_equality_without_abs.rs
@@ -1,5 +1,6 @@
-use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
+use crate::utils::{match_qpath, paths, span_lint_and_then, sugg};
 use if_chain::if_chain;
+use rustc_ast::util::parser::AssocOp;
 use rustc_errors::Applicability;
 use rustc_hir::{BinOpKind, Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
@@ -84,27 +85,24 @@ impl<'tcx> LateLintPass<'tcx> for FloatEqualityWithoutAbs {
             if let ty::Float(_) = t_val_r.kind;
 
             then {
-                // get the snippet string
-                let lhs_string = snippet(
-                    cx,
-                    lhs.span,
-                    "(...)",
-                );
+                let sug_l = sugg::Sugg::hir(cx, &val_l, "..");
+                let sug_r = sugg::Sugg::hir(cx, &val_r, "..");
                 // format the suggestion
-                let suggestion = if lhs_string.starts_with('(') {
-                    format!("{}.abs()", lhs_string)
-                } else {
-                    format!("({}).abs()", lhs_string)
-                };
+                let suggestion = format!("{}.abs()", sugg::make_assoc(AssocOp::Subtract, &sug_l, &sug_r).maybe_par());
                 // spans the lint
-                span_lint_and_sugg(
+                span_lint_and_then(
                     cx,
                     FLOAT_EQUALITY_WITHOUT_ABS,
                     expr.span,
                     "float equality check without `.abs()`",
-                    "add `.abs()`",
-                    suggestion,
-                    Applicability::MaybeIncorrect,
+                    | diag | {
+                        diag.span_suggestion(
+                            lhs.span,
+                            "add `.abs()`",
+                            suggestion,
+                            Applicability::MaybeIncorrect,
+                        );
+                    }
                 );
             }
         }
diff --git a/tests/ui/float_equality_without_abs.stderr b/tests/ui/float_equality_without_abs.stderr
index 74b9078afe8..b34c8159da0 100644
--- a/tests/ui/float_equality_without_abs.stderr
+++ b/tests/ui/float_equality_without_abs.stderr
@@ -2,7 +2,9 @@ error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:4:5
    |
 LL |     (a - b) < f32::EPSILON
-   |     ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
+   |     -------^^^^^^^^^^^^^^^
+   |     |
+   |     help: add `.abs()`: `(a - b).abs()`
    |
    = note: `-D clippy::float-equality-without-abs` implied by `-D warnings`
 
@@ -10,61 +12,81 @@ error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:13:13
    |
 LL |     let _ = (a - b) < f32::EPSILON;
-   |             ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
+   |             -------^^^^^^^^^^^^^^^
+   |             |
+   |             help: add `.abs()`: `(a - b).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:14:13
    |
 LL |     let _ = a - b < f32::EPSILON;
-   |             ^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
+   |             -----^^^^^^^^^^^^^^^
+   |             |
+   |             help: add `.abs()`: `(a - b).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:15:13
    |
 LL |     let _ = a - b.abs() < f32::EPSILON;
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b.abs()).abs()`
+   |             -----------^^^^^^^^^^^^^^^
+   |             |
+   |             help: add `.abs()`: `(a - b.abs()).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:16:13
    |
 LL |     let _ = (a as f64 - b as f64) < f64::EPSILON;
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a as f64 - b as f64).abs()`
+   |             ---------------------^^^^^^^^^^^^^^^
+   |             |
+   |             help: add `.abs()`: `(a as f64 - b as f64).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:17:13
    |
 LL |     let _ = 1.0 - 2.0 < f32::EPSILON;
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(1.0 - 2.0).abs()`
+   |             ---------^^^^^^^^^^^^^^^
+   |             |
+   |             help: add `.abs()`: `(1.0 - 2.0).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:19:13
    |
 LL |     let _ = f32::EPSILON > (a - b);
-   |             ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
+   |             ^^^^^^^^^^^^^^^-------
+   |                            |
+   |                            help: add `.abs()`: `(a - b).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:20:13
    |
 LL |     let _ = f32::EPSILON > a - b;
-   |             ^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()`
+   |             ^^^^^^^^^^^^^^^-----
+   |                            |
+   |                            help: add `.abs()`: `(a - b).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:21:13
    |
 LL |     let _ = f32::EPSILON > a - b.abs();
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b.abs()).abs()`
+   |             ^^^^^^^^^^^^^^^-----------
+   |                            |
+   |                            help: add `.abs()`: `(a - b.abs()).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:22:13
    |
 LL |     let _ = f64::EPSILON > (a as f64 - b as f64);
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a as f64 - b as f64).abs()`
+   |             ^^^^^^^^^^^^^^^---------------------
+   |                            |
+   |                            help: add `.abs()`: `(a as f64 - b as f64).abs()`
 
 error: float equality check without `.abs()`
   --> $DIR/float_equality_without_abs.rs:23:13
    |
 LL |     let _ = f32::EPSILON > 1.0 - 2.0;
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(1.0 - 2.0).abs()`
+   |             ^^^^^^^^^^^^^^^---------
+   |                            |
+   |                            help: add `.abs()`: `(1.0 - 2.0).abs()`
 
 error: aborting due to 11 previous errors