diff --git a/src/needless_bool.rs b/src/needless_bool.rs index a2a0d13e17c..a40b4b3a252 100644 --- a/src/needless_bool.rs +++ b/src/needless_bool.rs @@ -8,7 +8,7 @@ use rustc_front::hir::*; use syntax::ast::Lit_; use syntax::codemap::Spanned; -use utils::{span_lint, snippet}; +use utils::{span_lint, span_lint_and_then, snippet}; /// **What it does:** This lint checks for expressions of the form `if c { true } else { false }` (or vice versa) and suggest using the condition directly. /// @@ -109,34 +109,46 @@ impl LateLintPass for BoolComparison { (Some(true), None) => { let side_snip = snippet(cx, right_side.span, ".."); let hint = format!("`{}`", side_snip); - span_lint(cx, - BOOL_COMPARISON, - e.span, - &format!("you can simplify this boolean comparison to {}", hint)); + span_lint_and_then(cx, + BOOL_COMPARISON, + e.span, + "equality checks against booleans are unnecesary", + |db| { + db.span_suggestion(e.span, "try simplifying it:", hint); + }); } (None, Some(true)) => { let side_snip = snippet(cx, left_side.span, ".."); let hint = format!("`{}`", side_snip); - span_lint(cx, - BOOL_COMPARISON, - e.span, - &format!("you can simplify this boolean comparison to {}", hint)); + span_lint_and_then(cx, + BOOL_COMPARISON, + e.span, + "equality checks against booleans are unnecesary", + |db| { + db.span_suggestion(e.span, "try simplifying it:", hint); + }); } (Some(false), None) => { let side_snip = snippet(cx, right_side.span, ".."); let hint = format!("`!{}`", side_snip); - span_lint(cx, - BOOL_COMPARISON, - e.span, - &format!("you can simplify this boolean comparison to {}", hint)); + span_lint_and_then(cx, + BOOL_COMPARISON, + e.span, + "equality checks against booleans are unnecesary", + |db| { + db.span_suggestion(e.span, "try simplifying it:", hint); + }); } (None, Some(false)) => { let side_snip = snippet(cx, left_side.span, ".."); let hint = format!("`!{}`", side_snip); - span_lint(cx, - BOOL_COMPARISON, - e.span, - &format!("you can simplify this boolean comparison to {}", hint)); + span_lint_and_then(cx, + BOOL_COMPARISON, + e.span, + "equality checks against booleans are unnecesary", + |db| { + db.span_suggestion(e.span, "try simplifying it:", hint); + }); } _ => (), } diff --git a/tests/compile-fail/bool_comparison.rs b/tests/compile-fail/bool_comparison.rs index d2a362af2f4..468b8382e94 100644 --- a/tests/compile-fail/bool_comparison.rs +++ b/tests/compile-fail/bool_comparison.rs @@ -1,12 +1,23 @@ #![feature(plugin)] #![plugin(clippy)] -#[allow(needless_bool)] #[deny(bool_comparison)] fn main() { let x = true; - if x == true { true } else { false }; //~ERROR you can simplify this boolean comparison to `x` - if x == false { true } else { false }; //~ERROR you can simplify this boolean comparison to `!x` - if true == x { true } else { false }; //~ERROR you can simplify this boolean comparison to `x` - if false == x { true } else { false }; //~ERROR you can simplify this boolean comparison to `!x` + if x == true { "yes" } else { "no" }; + //~^ ERROR equality checks against booleans are unnecesary + //~| HELP try simplifying it: + //~| SUGGESTION x + if x == false { "yes" } else { "no" }; + //~^ ERROR equality checks against booleans are unnecesary + //~| HELP try simplifying it: + //~| SUGGESTION !x + if true == x { "yes" } else { "no" }; + //~^ ERROR equality checks against booleans are unnecesary + //~| HELP try simplifying it: + //~| SUGGESTION x + if false == x { "yes" } else { "no" }; + //~^ ERROR equality checks against booleans are unnecesary + //~| HELP try simplifying it: + //~| SUGGESTION !x }