Skip to content

Commit 1429267

Browse files
author
Joshua Holmer
committed
display suggestion separately from lint
1 parent 56b3e7b commit 1429267

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

src/needless_bool.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_front::hir::*;
88
use syntax::ast::Lit_;
99
use syntax::codemap::Spanned;
1010

11-
use utils::{span_lint, snippet};
11+
use utils::{span_lint, span_lint_and_then, snippet};
1212

1313
/// **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.
1414
///
@@ -109,34 +109,46 @@ impl LateLintPass for BoolComparison {
109109
(Some(true), None) => {
110110
let side_snip = snippet(cx, right_side.span, "..");
111111
let hint = format!("`{}`", side_snip);
112-
span_lint(cx,
113-
BOOL_COMPARISON,
114-
e.span,
115-
&format!("you can simplify this boolean comparison to {}", hint));
112+
span_lint_and_then(cx,
113+
BOOL_COMPARISON,
114+
e.span,
115+
"equality checks against booleans are unnecesary",
116+
|db| {
117+
db.span_suggestion(e.span, "try simplifying it:", hint);
118+
});
116119
}
117120
(None, Some(true)) => {
118121
let side_snip = snippet(cx, left_side.span, "..");
119122
let hint = format!("`{}`", side_snip);
120-
span_lint(cx,
121-
BOOL_COMPARISON,
122-
e.span,
123-
&format!("you can simplify this boolean comparison to {}", hint));
123+
span_lint_and_then(cx,
124+
BOOL_COMPARISON,
125+
e.span,
126+
"equality checks against booleans are unnecesary",
127+
|db| {
128+
db.span_suggestion(e.span, "try simplifying it:", hint);
129+
});
124130
}
125131
(Some(false), None) => {
126132
let side_snip = snippet(cx, right_side.span, "..");
127133
let hint = format!("`!{}`", side_snip);
128-
span_lint(cx,
129-
BOOL_COMPARISON,
130-
e.span,
131-
&format!("you can simplify this boolean comparison to {}", hint));
134+
span_lint_and_then(cx,
135+
BOOL_COMPARISON,
136+
e.span,
137+
"equality checks against booleans are unnecesary",
138+
|db| {
139+
db.span_suggestion(e.span, "try simplifying it:", hint);
140+
});
132141
}
133142
(None, Some(false)) => {
134143
let side_snip = snippet(cx, left_side.span, "..");
135144
let hint = format!("`!{}`", side_snip);
136-
span_lint(cx,
137-
BOOL_COMPARISON,
138-
e.span,
139-
&format!("you can simplify this boolean comparison to {}", hint));
145+
span_lint_and_then(cx,
146+
BOOL_COMPARISON,
147+
e.span,
148+
"equality checks against booleans are unnecesary",
149+
|db| {
150+
db.span_suggestion(e.span, "try simplifying it:", hint);
151+
});
140152
}
141153
_ => (),
142154
}

tests/compile-fail/bool_comparison.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
#![feature(plugin)]
22
#![plugin(clippy)]
33

4-
#[allow(needless_bool)]
54
#[deny(bool_comparison)]
65
fn main() {
76
let x = true;
8-
if x == true { true } else { false }; //~ERROR you can simplify this boolean comparison to `x`
9-
if x == false { true } else { false }; //~ERROR you can simplify this boolean comparison to `!x`
10-
if true == x { true } else { false }; //~ERROR you can simplify this boolean comparison to `x`
11-
if false == x { true } else { false }; //~ERROR you can simplify this boolean comparison to `!x`
7+
if x == true { "yes" } else { "no" };
8+
//~^ ERROR equality checks against booleans are unnecesary
9+
//~| HELP try simplifying it:
10+
//~| SUGGESTION x
11+
if x == false { "yes" } else { "no" };
12+
//~^ ERROR equality checks against booleans are unnecesary
13+
//~| HELP try simplifying it:
14+
//~| SUGGESTION !x
15+
if true == x { "yes" } else { "no" };
16+
//~^ ERROR equality checks against booleans are unnecesary
17+
//~| HELP try simplifying it:
18+
//~| SUGGESTION x
19+
if false == x { "yes" } else { "no" };
20+
//~^ ERROR equality checks against booleans are unnecesary
21+
//~| HELP try simplifying it:
22+
//~| SUGGESTION !x
1223
}

0 commit comments

Comments
 (0)