Skip to content

Commit 7e06737

Browse files
author
Joshua Holmer
committed
Improve testing and suggestion messages on bool_comparison
1 parent 2687a3f commit 7e06737

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

src/needless_bool.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,47 +107,43 @@ impl LateLintPass for BoolComparison {
107107
if let ExprBinary(Spanned{ node: BiEq, .. }, ref left_side, ref right_side) = e.node {
108108
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
109109
(Some(true), None) => {
110-
let side_snip = snippet(cx, right_side.span, "..");
111-
let hint = format!("`{}`", side_snip);
110+
let hint = format!("{}", snippet(cx, right_side.span, ".."));
112111
span_lint_and_then(cx,
113112
BOOL_COMPARISON,
114113
e.span,
115-
"equality checks against booleans are unnecesary",
114+
"equality checks against true are unnecesary",
116115
|db| {
117-
db.span_suggestion(e.span, "try simplifying it:", hint);
116+
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
118117
});
119118
}
120119
(None, Some(true)) => {
121-
let side_snip = snippet(cx, left_side.span, "..");
122-
let hint = format!("`{}`", side_snip);
120+
let hint = format!("{}", snippet(cx, left_side.span, ".."));
123121
span_lint_and_then(cx,
124122
BOOL_COMPARISON,
125123
e.span,
126-
"equality checks against booleans are unnecesary",
124+
"equality checks against true are unnecesary",
127125
|db| {
128-
db.span_suggestion(e.span, "try simplifying it:", hint);
126+
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
129127
});
130128
}
131129
(Some(false), None) => {
132-
let side_snip = snippet(cx, right_side.span, "..");
133-
let hint = format!("`!{}`", side_snip);
130+
let hint = format!("!{}", snippet(cx, right_side.span, ".."));
134131
span_lint_and_then(cx,
135132
BOOL_COMPARISON,
136133
e.span,
137-
"equality checks against booleans are unnecesary",
134+
"equality checks against false can be replaced by a negation",
138135
|db| {
139-
db.span_suggestion(e.span, "try simplifying it:", hint);
136+
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
140137
});
141138
}
142139
(None, Some(false)) => {
143-
let side_snip = snippet(cx, left_side.span, "..");
144-
let hint = format!("`!{}`", side_snip);
140+
let hint = format!("!{}", snippet(cx, left_side.span, ".."));
145141
span_lint_and_then(cx,
146142
BOOL_COMPARISON,
147143
e.span,
148-
"equality checks against booleans are unnecesary",
144+
"equality checks against false can be replaced by a negation",
149145
|db| {
150-
db.span_suggestion(e.span, "try simplifying it:", hint);
146+
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
151147
});
152148
}
153149
_ => (),

tests/compile-fail/bool_comparison.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
fn main() {
66
let x = true;
77
if x == true { "yes" } else { "no" };
8-
//~^ ERROR equality checks against booleans are unnecesary
9-
//~| HELP try simplifying it:
10-
//~| SUGGESTION x
8+
//~^ ERROR equality checks against true are unnecesary
9+
//~| HELP try simplifying it as shown:
10+
//~| SUGGESTION if x { "yes" } else { "no" };
1111
if x == false { "yes" } else { "no" };
12-
//~^ ERROR equality checks against booleans are unnecesary
13-
//~| HELP try simplifying it:
14-
//~| SUGGESTION !x
12+
//~^ ERROR equality checks against false can be replaced by a negation
13+
//~| HELP try simplifying it as shown:
14+
//~| SUGGESTION if !x { "yes" } else { "no" };
1515
if true == x { "yes" } else { "no" };
16-
//~^ ERROR equality checks against booleans are unnecesary
17-
//~| HELP try simplifying it:
18-
//~| SUGGESTION x
16+
//~^ ERROR equality checks against true are unnecesary
17+
//~| HELP try simplifying it as shown:
18+
//~| SUGGESTION if x { "yes" } else { "no" };
1919
if false == x { "yes" } else { "no" };
20-
//~^ ERROR equality checks against booleans are unnecesary
21-
//~| HELP try simplifying it:
22-
//~| SUGGESTION !x
20+
//~^ ERROR equality checks against false can be replaced by a negation
21+
//~| HELP try simplifying it as shown:
22+
//~| SUGGESTION if !x { "yes" } else { "no" };
2323
}

0 commit comments

Comments
 (0)