Improve testing and suggestion messages on bool_comparison

This commit is contained in:
Joshua Holmer 2016-02-09 15:44:07 -05:00
parent 2687a3f6b5
commit 7e06737d6f
2 changed files with 24 additions and 28 deletions

View file

@ -107,47 +107,43 @@ impl LateLintPass for BoolComparison {
if let ExprBinary(Spanned{ node: BiEq, .. }, ref left_side, ref right_side) = e.node { if let ExprBinary(Spanned{ node: BiEq, .. }, ref left_side, ref right_side) = e.node {
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) { match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
(Some(true), None) => { (Some(true), None) => {
let side_snip = snippet(cx, right_side.span, ".."); let hint = format!("{}", snippet(cx, right_side.span, ".."));
let hint = format!("`{}`", side_snip);
span_lint_and_then(cx, span_lint_and_then(cx,
BOOL_COMPARISON, BOOL_COMPARISON,
e.span, e.span,
"equality checks against booleans are unnecesary", "equality checks against true are unnecesary",
|db| { |db| {
db.span_suggestion(e.span, "try simplifying it:", hint); db.span_suggestion(e.span, "try simplifying it as shown:", hint);
}); });
} }
(None, Some(true)) => { (None, Some(true)) => {
let side_snip = snippet(cx, left_side.span, ".."); let hint = format!("{}", snippet(cx, left_side.span, ".."));
let hint = format!("`{}`", side_snip);
span_lint_and_then(cx, span_lint_and_then(cx,
BOOL_COMPARISON, BOOL_COMPARISON,
e.span, e.span,
"equality checks against booleans are unnecesary", "equality checks against true are unnecesary",
|db| { |db| {
db.span_suggestion(e.span, "try simplifying it:", hint); db.span_suggestion(e.span, "try simplifying it as shown:", hint);
}); });
} }
(Some(false), None) => { (Some(false), None) => {
let side_snip = snippet(cx, right_side.span, ".."); let hint = format!("!{}", snippet(cx, right_side.span, ".."));
let hint = format!("`!{}`", side_snip);
span_lint_and_then(cx, span_lint_and_then(cx,
BOOL_COMPARISON, BOOL_COMPARISON,
e.span, e.span,
"equality checks against booleans are unnecesary", "equality checks against false can be replaced by a negation",
|db| { |db| {
db.span_suggestion(e.span, "try simplifying it:", hint); db.span_suggestion(e.span, "try simplifying it as shown:", hint);
}); });
} }
(None, Some(false)) => { (None, Some(false)) => {
let side_snip = snippet(cx, left_side.span, ".."); let hint = format!("!{}", snippet(cx, left_side.span, ".."));
let hint = format!("`!{}`", side_snip);
span_lint_and_then(cx, span_lint_and_then(cx,
BOOL_COMPARISON, BOOL_COMPARISON,
e.span, e.span,
"equality checks against booleans are unnecesary", "equality checks against false can be replaced by a negation",
|db| { |db| {
db.span_suggestion(e.span, "try simplifying it:", hint); db.span_suggestion(e.span, "try simplifying it as shown:", hint);
}); });
} }
_ => (), _ => (),

View file

@ -5,19 +5,19 @@
fn main() { fn main() {
let x = true; let x = true;
if x == true { "yes" } else { "no" }; if x == true { "yes" } else { "no" };
//~^ ERROR equality checks against booleans are unnecesary //~^ ERROR equality checks against true are unnecesary
//~| HELP try simplifying it: //~| HELP try simplifying it as shown:
//~| SUGGESTION x //~| SUGGESTION if x { "yes" } else { "no" };
if x == false { "yes" } else { "no" }; if x == false { "yes" } else { "no" };
//~^ ERROR equality checks against booleans are unnecesary //~^ ERROR equality checks against false can be replaced by a negation
//~| HELP try simplifying it: //~| HELP try simplifying it as shown:
//~| SUGGESTION !x //~| SUGGESTION if !x { "yes" } else { "no" };
if true == x { "yes" } else { "no" }; if true == x { "yes" } else { "no" };
//~^ ERROR equality checks against booleans are unnecesary //~^ ERROR equality checks against true are unnecesary
//~| HELP try simplifying it: //~| HELP try simplifying it as shown:
//~| SUGGESTION x //~| SUGGESTION if x { "yes" } else { "no" };
if false == x { "yes" } else { "no" }; if false == x { "yes" } else { "no" };
//~^ ERROR equality checks against booleans are unnecesary //~^ ERROR equality checks against false can be replaced by a negation
//~| HELP try simplifying it: //~| HELP try simplifying it as shown:
//~| SUGGESTION !x //~| SUGGESTION if !x { "yes" } else { "no" };
} }