Skip to content

Commit 9b79b10

Browse files
committed
Fix suggestions for needless_bool
1 parent 7778f31 commit 9b79b10

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use rustc::lint::*;
66
use rustc::hir::*;
77
use syntax::ast::LitKind;
88
use syntax::codemap::Spanned;
9-
use utils::{span_lint, span_lint_and_then, snippet, snippet_opt};
9+
use utils::{span_lint, span_lint_and_then, snippet};
10+
use utils::sugg::Sugg;
1011

1112
/// **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.
1213
///
@@ -49,11 +50,20 @@ impl LateLintPass for NeedlessBool {
4950
fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
5051
use self::Expression::*;
5152
if let ExprIf(ref pred, ref then_block, Some(ref else_expr)) = e.node {
52-
let reduce = |hint: &str, not| {
53-
let hint = match snippet_opt(cx, pred.span) {
54-
Some(pred_snip) => format!("`{}{}`", not, pred_snip),
55-
None => hint.into(),
53+
let reduce = |ret, not| {
54+
let snip = Sugg::hir(cx, pred, "<predicate>");
55+
let snip = if not {
56+
!snip
57+
} else {
58+
snip
59+
};
60+
61+
let hint = if ret {
62+
format!("return {};", snip)
63+
} else {
64+
snip.to_string()
5665
};
66+
5767
span_lint_and_then(cx,
5868
NEEDLESS_BOOL,
5969
e.span,
@@ -77,10 +87,10 @@ impl LateLintPass for NeedlessBool {
7787
e.span,
7888
"this if-then-else expression will always return false");
7989
}
80-
(RetBool(true), RetBool(false)) => reduce("its predicate", "return "),
81-
(Bool(true), Bool(false)) => reduce("its predicate", ""),
82-
(RetBool(false), RetBool(true)) => reduce("`!` and its predicate", "return !"),
83-
(Bool(false), Bool(true)) => reduce("`!` and its predicate", "!"),
90+
(RetBool(true), RetBool(false)) => reduce(true, false),
91+
(Bool(true), Bool(false)) => reduce(false, false),
92+
(RetBool(false), RetBool(true)) => reduce(true, true),
93+
(Bool(false), Bool(true)) => reduce(false, true),
8494
_ => (),
8595
}
8696
}
@@ -122,23 +132,23 @@ impl LateLintPass for BoolComparison {
122132
});
123133
}
124134
(Bool(false), Other) => {
125-
let hint = format!("!{}", snippet(cx, right_side.span, ".."));
135+
let hint = Sugg::hir(cx, right_side, "..");
126136
span_lint_and_then(cx,
127137
BOOL_COMPARISON,
128138
e.span,
129139
"equality checks against false can be replaced by a negation",
130140
|db| {
131-
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
141+
db.span_suggestion(e.span, "try simplifying it as shown:", (!hint).to_string());
132142
});
133143
}
134144
(Other, Bool(false)) => {
135-
let hint = format!("!{}", snippet(cx, left_side.span, ".."));
145+
let hint = Sugg::hir(cx, left_side, "..");
136146
span_lint_and_then(cx,
137147
BOOL_COMPARISON,
138148
e.span,
139149
"equality checks against false can be replaced by a negation",
140150
|db| {
141-
db.span_suggestion(e.span, "try simplifying it as shown:", hint);
151+
db.span_suggestion(e.span, "try simplifying it as shown:", (!hint).to_string());
142152
});
143153
}
144154
_ => (),

tests/compile-fail/needless_bool.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@
55
#[allow(if_same_then_else)]
66
fn main() {
77
let x = true;
8+
let y = false;
89
if x { true } else { true }; //~ERROR this if-then-else expression will always return true
910
if x { false } else { false }; //~ERROR this if-then-else expression will always return false
1011
if x { true } else { false };
1112
//~^ ERROR this if-then-else expression returns a bool literal
1213
//~| HELP you can reduce it to
13-
//~| SUGGESTION `x`
14+
//~| SUGGESTION x
1415
if x { false } else { true };
1516
//~^ ERROR this if-then-else expression returns a bool literal
1617
//~| HELP you can reduce it to
17-
//~| SUGGESTION `!x`
18+
//~| SUGGESTION !x
19+
if x && y { false } else { true };
20+
//~^ ERROR this if-then-else expression returns a bool literal
21+
//~| HELP you can reduce it to
22+
//~| SUGGESTION !(x && y)
1823
if x { x } else { false }; // would also be questionable, but we don't catch this yet
1924
bool_ret(x);
2025
bool_ret2(x);
2126
bool_ret3(x);
27+
bool_ret5(x, x);
2228
bool_ret4(x);
29+
bool_ret6(x, x);
2330
}
2431

2532
#[allow(if_same_then_else, needless_return)]
@@ -39,13 +46,29 @@ fn bool_ret3(x: bool) -> bool {
3946
if x { return true } else { return false };
4047
//~^ ERROR this if-then-else expression returns a bool literal
4148
//~| HELP you can reduce it to
42-
//~| SUGGESTION `return x`
49+
//~| SUGGESTION return x
50+
}
51+
52+
#[allow(needless_return)]
53+
fn bool_ret5(x: bool, y: bool) -> bool {
54+
if x && y { return true } else { return false };
55+
//~^ ERROR this if-then-else expression returns a bool literal
56+
//~| HELP you can reduce it to
57+
//~| SUGGESTION return x && y
4358
}
4459

4560
#[allow(needless_return)]
4661
fn bool_ret4(x: bool) -> bool {
4762
if x { return false } else { return true };
4863
//~^ ERROR this if-then-else expression returns a bool literal
4964
//~| HELP you can reduce it to
50-
//~| SUGGESTION `return !x`
65+
//~| SUGGESTION return !x
66+
}
67+
68+
#[allow(needless_return)]
69+
fn bool_ret6(x: bool, y: bool) -> bool {
70+
if x && y { return false } else { return true };
71+
//~^ ERROR this if-then-else expression returns a bool literal
72+
//~| HELP you can reduce it to
73+
//~| SUGGESTION return !(x && y)
5174
}

0 commit comments

Comments
 (0)