Changed the location of the suggestion as well as the way the suggestion is assembled

This commit is contained in:
Bastian 2020-08-26 16:39:30 +02:00
parent 179df0bd15
commit 2d853148d7
2 changed files with 47 additions and 27 deletions

@ -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 if_chain::if_chain;
use rustc_ast::util::parser::AssocOp;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
@ -84,27 +85,24 @@ impl<'tcx> LateLintPass<'tcx> for FloatEqualityWithoutAbs {
if let ty::Float(_) = t_val_r.kind; if let ty::Float(_) = t_val_r.kind;
then { then {
// get the snippet string let sug_l = sugg::Sugg::hir(cx, &val_l, "..");
let lhs_string = snippet( let sug_r = sugg::Sugg::hir(cx, &val_r, "..");
cx,
lhs.span,
"(...)",
);
// format the suggestion // format the suggestion
let suggestion = if lhs_string.starts_with('(') { let suggestion = format!("{}.abs()", sugg::make_assoc(AssocOp::Subtract, &sug_l, &sug_r).maybe_par());
format!("{}.abs()", lhs_string)
} else {
format!("({}).abs()", lhs_string)
};
// spans the lint // spans the lint
span_lint_and_sugg( span_lint_and_then(
cx, cx,
FLOAT_EQUALITY_WITHOUT_ABS, FLOAT_EQUALITY_WITHOUT_ABS,
expr.span, expr.span,
"float equality check without `.abs()`", "float equality check without `.abs()`",
"add `.abs()`", | diag | {
suggestion, diag.span_suggestion(
Applicability::MaybeIncorrect, lhs.span,
"add `.abs()`",
suggestion,
Applicability::MaybeIncorrect,
);
}
); );
} }
} }

@ -2,7 +2,9 @@ error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:4:5 --> $DIR/float_equality_without_abs.rs:4:5
| |
LL | (a - b) < f32::EPSILON 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` = 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 --> $DIR/float_equality_without_abs.rs:13:13
| |
LL | let _ = (a - b) < f32::EPSILON; LL | let _ = (a - b) < f32::EPSILON;
| ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()` | -------^^^^^^^^^^^^^^^
| |
| help: add `.abs()`: `(a - b).abs()`
error: float equality check without `.abs()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:14:13 --> $DIR/float_equality_without_abs.rs:14:13
| |
LL | let _ = a - b < f32::EPSILON; LL | let _ = a - b < f32::EPSILON;
| ^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()` | -----^^^^^^^^^^^^^^^
| |
| help: add `.abs()`: `(a - b).abs()`
error: float equality check without `.abs()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:15:13 --> $DIR/float_equality_without_abs.rs:15:13
| |
LL | let _ = a - b.abs() < f32::EPSILON; 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()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:16:13 --> $DIR/float_equality_without_abs.rs:16:13
| |
LL | let _ = (a as f64 - b as f64) < f64::EPSILON; 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()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:17:13 --> $DIR/float_equality_without_abs.rs:17:13
| |
LL | let _ = 1.0 - 2.0 < f32::EPSILON; 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()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:19:13 --> $DIR/float_equality_without_abs.rs:19:13
| |
LL | let _ = f32::EPSILON > (a - b); LL | let _ = f32::EPSILON > (a - b);
| ^^^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()` | ^^^^^^^^^^^^^^^-------
| |
| help: add `.abs()`: `(a - b).abs()`
error: float equality check without `.abs()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:20:13 --> $DIR/float_equality_without_abs.rs:20:13
| |
LL | let _ = f32::EPSILON > a - b; LL | let _ = f32::EPSILON > a - b;
| ^^^^^^^^^^^^^^^^^^^^ help: add `.abs()`: `(a - b).abs()` | ^^^^^^^^^^^^^^^-----
| |
| help: add `.abs()`: `(a - b).abs()`
error: float equality check without `.abs()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:21:13 --> $DIR/float_equality_without_abs.rs:21:13
| |
LL | let _ = f32::EPSILON > a - b.abs(); 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()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:22:13 --> $DIR/float_equality_without_abs.rs:22:13
| |
LL | let _ = f64::EPSILON > (a as f64 - b as f64); 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()` error: float equality check without `.abs()`
--> $DIR/float_equality_without_abs.rs:23:13 --> $DIR/float_equality_without_abs.rs:23:13
| |
LL | let _ = f32::EPSILON > 1.0 - 2.0; 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 error: aborting due to 11 previous errors