From: mcarton Date: Wed, 29 Jun 2016 19:25:23 +0000 (+0200) Subject: Use `utils::sugg` in `FLOAT_CMP` X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=2e8edde6e9b42b3a8c7e8aefb2ad6c86be404915;p=rust.git Use `utils::sugg` in `FLOAT_CMP` --- diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index e57cd50899e..9c04ad6f3e4 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -13,6 +13,7 @@ get_item_name, get_parent_expr, implements_trait, in_macro, is_integer_literal, match_path, snippet, span_lint, span_lint_and_then, walk_ptrs_ty }; +use utils::sugg::Sugg; /// **What it does:** This lint checks for function arguments and let bindings denoted as `ref`. /// @@ -169,11 +170,12 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { expr.span, "strict comparison of f32 or f64", |db| { + let lhs = &Sugg::hir(cx, left, ".."); + let rhs = &Sugg::hir(cx, right, ".."); + db.span_suggestion(expr.span, "consider comparing them within some error", - format!("({} - {}).abs() < error", - snippet(cx, left.span, ".."), - snippet(cx, right.span, ".."))); + format!("({}).abs() < error", lhs - rhs)); db.span_note(expr.span, "std::f32::EPSILON and std::f64::EPSILON are available."); }); } diff --git a/tests/compile-fail/float_cmp.rs b/tests/compile-fail/float_cmp.rs index cf8cefb3af3..314cc721425 100644 --- a/tests/compile-fail/float_cmp.rs +++ b/tests/compile-fail/float_cmp.rs @@ -44,12 +44,12 @@ fn main() { //~^ ERROR strict comparison of f32 or f64 //~| HELP within some error //~| SUGGESTION (ONE - 1f32).abs() < error - ONE == (1.0 + 0.0); + ONE == 1.0 + 0.0; //~^ ERROR strict comparison of f32 or f64 //~| HELP within some error //~| SUGGESTION (ONE - (1.0 + 0.0)).abs() < error - ONE + ONE == (ZERO + ONE + ONE); + ONE + ONE == ZERO + ONE + ONE; //~^ ERROR strict comparison of f32 or f64 //~| HELP within some error //~| SUGGESTION (ONE + ONE - (ZERO + ONE + ONE)).abs() < error