Use utils::sugg in FLOAT_CMP

This commit is contained in:
mcarton 2016-06-29 21:25:23 +02:00
parent 8d58a928e5
commit 2e8edde6e9
No known key found for this signature in database
GPG Key ID: 5E427C794CBA45E8
2 changed files with 7 additions and 5 deletions

View File

@ -13,6 +13,7 @@ use utils::{
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 @@ impl LateLintPass for FloatCmp {
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.");
});
}

View File

@ -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