mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 05:03:21 +00:00
Improve testing and suggestion messages on bool_comparison
This commit is contained in:
parent
2687a3f6b5
commit
7e06737d6f
2 changed files with 24 additions and 28 deletions
|
@ -107,47 +107,43 @@ impl LateLintPass for BoolComparison {
|
|||
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)) {
|
||||
(Some(true), None) => {
|
||||
let side_snip = snippet(cx, right_side.span, "..");
|
||||
let hint = format!("`{}`", side_snip);
|
||||
let hint = format!("{}", snippet(cx, right_side.span, ".."));
|
||||
span_lint_and_then(cx,
|
||||
BOOL_COMPARISON,
|
||||
e.span,
|
||||
"equality checks against booleans are unnecesary",
|
||||
"equality checks against true are unnecesary",
|
||||
|db| {
|
||||
db.span_suggestion(e.span, "try simplifying it:", hint);
|
||||
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
|
||||
});
|
||||
}
|
||||
(None, Some(true)) => {
|
||||
let side_snip = snippet(cx, left_side.span, "..");
|
||||
let hint = format!("`{}`", side_snip);
|
||||
let hint = format!("{}", snippet(cx, left_side.span, ".."));
|
||||
span_lint_and_then(cx,
|
||||
BOOL_COMPARISON,
|
||||
e.span,
|
||||
"equality checks against booleans are unnecesary",
|
||||
"equality checks against true are unnecesary",
|
||||
|db| {
|
||||
db.span_suggestion(e.span, "try simplifying it:", hint);
|
||||
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
|
||||
});
|
||||
}
|
||||
(Some(false), None) => {
|
||||
let side_snip = snippet(cx, right_side.span, "..");
|
||||
let hint = format!("`!{}`", side_snip);
|
||||
let hint = format!("!{}", snippet(cx, right_side.span, ".."));
|
||||
span_lint_and_then(cx,
|
||||
BOOL_COMPARISON,
|
||||
e.span,
|
||||
"equality checks against booleans are unnecesary",
|
||||
"equality checks against false can be replaced by a negation",
|
||||
|db| {
|
||||
db.span_suggestion(e.span, "try simplifying it:", hint);
|
||||
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
|
||||
});
|
||||
}
|
||||
(None, Some(false)) => {
|
||||
let side_snip = snippet(cx, left_side.span, "..");
|
||||
let hint = format!("`!{}`", side_snip);
|
||||
let hint = format!("!{}", snippet(cx, left_side.span, ".."));
|
||||
span_lint_and_then(cx,
|
||||
BOOL_COMPARISON,
|
||||
e.span,
|
||||
"equality checks against booleans are unnecesary",
|
||||
"equality checks against false can be replaced by a negation",
|
||||
|db| {
|
||||
db.span_suggestion(e.span, "try simplifying it:", hint);
|
||||
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
|
||||
});
|
||||
}
|
||||
_ => (),
|
||||
|
|
|
@ -5,19 +5,19 @@
|
|||
fn main() {
|
||||
let x = true;
|
||||
if x == true { "yes" } else { "no" };
|
||||
//~^ ERROR equality checks against booleans are unnecesary
|
||||
//~| HELP try simplifying it:
|
||||
//~| SUGGESTION x
|
||||
//~^ ERROR equality checks against true are unnecesary
|
||||
//~| HELP try simplifying it as shown:
|
||||
//~| SUGGESTION if x { "yes" } else { "no" };
|
||||
if x == false { "yes" } else { "no" };
|
||||
//~^ ERROR equality checks against booleans are unnecesary
|
||||
//~| HELP try simplifying it:
|
||||
//~| SUGGESTION !x
|
||||
//~^ ERROR equality checks against false can be replaced by a negation
|
||||
//~| HELP try simplifying it as shown:
|
||||
//~| SUGGESTION if !x { "yes" } else { "no" };
|
||||
if true == x { "yes" } else { "no" };
|
||||
//~^ ERROR equality checks against booleans are unnecesary
|
||||
//~| HELP try simplifying it:
|
||||
//~| SUGGESTION x
|
||||
//~^ ERROR equality checks against true are unnecesary
|
||||
//~| HELP try simplifying it as shown:
|
||||
//~| SUGGESTION if x { "yes" } else { "no" };
|
||||
if false == x { "yes" } else { "no" };
|
||||
//~^ ERROR equality checks against booleans are unnecesary
|
||||
//~| HELP try simplifying it:
|
||||
//~| SUGGESTION !x
|
||||
//~^ ERROR equality checks against false can be replaced by a negation
|
||||
//~| HELP try simplifying it as shown:
|
||||
//~| SUGGESTION if !x { "yes" } else { "no" };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue