Skip to content

Commit 27a3106

Browse files
committed
Fix suggestions for needless_bool
1 parent 7778f31 commit 27a3106

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc::hir::*;
77
use syntax::ast::LitKind;
88
use syntax::codemap::Spanned;
99
use utils::{span_lint, span_lint_and_then, snippet, snippet_opt};
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
5659
};
60+
61+
let hint = if ret {
62+
format!("return {};", snip)
63+
} else {
64+
snip.to_string()
65+
};
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
}

tests/compile-fail/needless_bool.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
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);
@@ -39,13 +44,13 @@ fn bool_ret3(x: bool) -> bool {
3944
if x { return true } else { return false };
4045
//~^ ERROR this if-then-else expression returns a bool literal
4146
//~| HELP you can reduce it to
42-
//~| SUGGESTION `return x`
47+
//~| SUGGESTION return x
4348
}
4449

4550
#[allow(needless_return)]
4651
fn bool_ret4(x: bool) -> bool {
4752
if x { return false } else { return true };
4853
//~^ ERROR this if-then-else expression returns a bool literal
4954
//~| HELP you can reduce it to
50-
//~| SUGGESTION `return !x`
55+
//~| SUGGESTION return !x
5156
}

0 commit comments

Comments
 (0)