Skip to content

Commit adce3ef

Browse files
author
Grzegorz Bartoszek
committed
needless bool lint suggestion is wrapped in brackets if it is an "else" clause of an "if-else" statement
1 parent e648adf commit adce3ef

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
6767
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
6868
use self::Expression::*;
6969
if let ExprKind::If(ref pred, ref then_block, Some(ref else_expr)) = e.node {
70+
7071
let reduce = |ret, not| {
7172
let mut applicability = Applicability::MachineApplicable;
7273
let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
7374
let snip = if not { !snip } else { snip };
7475

75-
let hint = if ret {
76+
let mut hint = if ret {
7677
format!("return {}", snip)
7778
} else {
7879
snip.to_string()
7980
};
8081

82+
if parent_node_is_if_expr(&e, &cx) {
83+
hint = format!("{{ {} }}", hint);
84+
}
85+
8186
span_lint_and_sugg(
8287
cx,
8388
NEEDLESS_BOOL,
@@ -119,6 +124,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
119124
}
120125
}
121126

127+
fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool {
128+
let parent_id = cx.tcx.hir().get_parent_node(expr.id);
129+
let parent_node = cx.tcx.hir().get(parent_id);
130+
131+
if let rustc::hir::Node::Expr(e) = parent_node {
132+
if let ExprKind::If(_,_,_) = e.node {
133+
return true;
134+
}
135+
}
136+
137+
false
138+
}
139+
122140
#[derive(Copy, Clone)]
123141
pub struct BoolComparison;
124142

tests/ui/needless_bool.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,16 @@ fn needless_bool3(x: bool) {
141141
if x == true {};
142142
if x == false {};
143143
}
144+
145+
fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() {
146+
let b = false;
147+
let returns_bool = || false;
148+
149+
let x = if b {
150+
true
151+
} else if returns_bool() {
152+
false
153+
} else {
154+
true
155+
};
156+
}

tests/ui/needless_bool.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,16 @@ error: equality checks against false can be replaced by a negation
136136
LL | if x == false {};
137137
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
138138

139-
error: aborting due to 15 previous errors
139+
error: this if-then-else expression returns a bool literal
140+
--> $DIR/needless_bool.rs:151:12
141+
|
142+
LL | } else if returns_bool() {
143+
| ____________^
144+
LL | | false
145+
LL | | } else {
146+
LL | | true
147+
LL | | };
148+
| |_____^ help: you can reduce it to: `{ !returns_bool() }`
149+
150+
error: aborting due to 16 previous errors
140151

0 commit comments

Comments
 (0)