Skip to content

Commit

Permalink
display suggestion separately from lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Holmer committed Feb 9, 2016
1 parent 56b3e7b commit 1429267
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
46 changes: 29 additions & 17 deletions src/needless_bool.rs
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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);
});
}
_ => (),
}
Expand Down
21 changes: 16 additions & 5 deletions 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
}

0 comments on commit 1429267

Please sign in to comment.