From 580ae5a879073d535c60857b1fbc5204ad2c810e Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 29 Jun 2016 19:47:51 +0200 Subject: [PATCH] Use `span_suggestion` in `FLOAT_CMP` --- clippy_lints/src/misc.rs | 21 +++++++++------- tests/compile-fail/float_cmp.rs | 44 +++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index 49532576469..e57cd50899e 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -164,15 +164,18 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { return; } } - span_lint(cx, - FLOAT_CMP, - expr.span, - &format!("{}-comparison of f32 or f64 detected. Consider changing this to `({} - {}).abs() < \ - epsilon` for some suitable value of epsilon. \ - std::f32::EPSILON and std::f64::EPSILON are available.", - op.as_str(), - snippet(cx, left.span, ".."), - snippet(cx, right.span, ".."))); + span_lint_and_then(cx, + FLOAT_CMP, + expr.span, + "strict comparison of f32 or f64", + |db| { + db.span_suggestion(expr.span, + "consider comparing them within some error", + format!("({} - {}).abs() < error", + snippet(cx, left.span, ".."), + snippet(cx, right.span, ".."))); + 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 9f611dd3fd9..cf8cefb3af3 100644 --- a/tests/compile-fail/float_cmp.rs +++ b/tests/compile-fail/float_cmp.rs @@ -40,23 +40,47 @@ fn main() { ZERO == 0.0; //no error, comparison with zero is ok ZERO + ZERO != 1.0; //no error, comparison with zero is ok - ONE == 1f32; //~ERROR ==-comparison of f32 or f64 - ONE == (1.0 + 0.0); //~ERROR ==-comparison of f32 or f64 - - ONE + ONE == (ZERO + ONE + ONE); //~ERROR ==-comparison of f32 or f64 - - ONE != 2.0; //~ERROR !=-comparison of f32 or f64 + ONE == 1f32; + //~^ ERROR strict comparison of f32 or f64 + //~| HELP within some error + //~| SUGGESTION (ONE - 1f32).abs() < error + 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); + //~^ ERROR strict comparison of f32 or f64 + //~| HELP within some error + //~| SUGGESTION (ONE + ONE - (ZERO + ONE + ONE)).abs() < error + + ONE != 2.0; + //~^ ERROR strict comparison of f32 or f64 + //~| HELP within some error + //~| SUGGESTION (ONE - 2.0).abs() < error ONE != 0.0; // no error, comparison with zero is ok - twice(ONE) != ONE; //~ERROR !=-comparison of f32 or f64 - ONE as f64 != 2.0; //~ERROR !=-comparison of f32 or f64 + twice(ONE) != ONE; + //~^ ERROR strict comparison of f32 or f64 + //~| HELP within some error + //~| SUGGESTION (twice(ONE) - ONE).abs() < error + ONE as f64 != 2.0; + //~^ ERROR strict comparison of f32 or f64 + //~| HELP within some error + //~| SUGGESTION (ONE as f64 - 2.0).abs() < error ONE as f64 != 0.0; // no error, comparison with zero is ok let x : f64 = 1.0; - x == 1.0; //~ERROR ==-comparison of f32 or f64 + x == 1.0; + //~^ ERROR strict comparison of f32 or f64 + //~| HELP within some error + //~| SUGGESTION (x - 1.0).abs() < error x != 0f64; // no error, comparison with zero is ok - twice(x) != twice(ONE as f64); //~ERROR !=-comparison of f32 or f64 + twice(x) != twice(ONE as f64); + //~^ ERROR strict comparison of f32 or f64 + //~| HELP within some error + //~| SUGGESTION (twice(x) - twice(ONE as f64)).abs() < error x < 0.0; // no errors, lower or greater comparisons need no fuzzyness -- 2.44.0