Skip to content

Commit f29aa69

Browse files
committed
fix: needless_bool_assign missing curlies when on else if
1 parent f46cf87 commit f29aa69

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,16 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
199199
let mut applicability = Applicability::MachineApplicable;
200200
let cond = Sugg::hir_with_applicability(cx, cond, "..", &mut applicability);
201201
let lhs = snippet_with_applicability(cx, lhs_a.span, "..", &mut applicability);
202-
let sugg = if a == b {
202+
let mut sugg = if a == b {
203203
format!("{cond}; {lhs} = {a:?};")
204204
} else {
205205
format!("{lhs} = {};", if a { cond } else { !cond })
206206
};
207+
208+
if is_else_clause(cx.tcx, e) {
209+
sugg = format!("{{ {sugg} }}");
210+
}
211+
207212
span_lint_and_sugg(
208213
cx,
209214
NEEDLESS_BOOL_ASSIGN,

tests/ui/needless_bool_assign.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ fn main() {
3333
b = true;
3434
}
3535
}
36+
37+
fn issue15063(x: bool, y: bool) {
38+
let mut z = false;
39+
40+
if x && y {
41+
todo!()
42+
} else { z = x || y; }
43+
//~^^^^^ needless_bool_assign
44+
}

tests/ui/needless_bool_assign.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@ fn main() {
4545
b = true;
4646
}
4747
}
48+
49+
fn issue15063(x: bool, y: bool) {
50+
let mut z = false;
51+
52+
if x && y {
53+
todo!()
54+
} else if x || y {
55+
z = true;
56+
} else {
57+
z = false;
58+
}
59+
//~^^^^^ needless_bool_assign
60+
}

tests/ui/needless_bool_assign.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,16 @@ LL | | }
5151
= note: `-D clippy::if-same-then-else` implied by `-D warnings`
5252
= help: to override `-D warnings` add `#[allow(clippy::if_same_then_else)]`
5353

54-
error: aborting due to 4 previous errors
54+
error: this if-then-else expression assigns a bool literal
55+
--> tests/ui/needless_bool_assign.rs:54:12
56+
|
57+
LL | } else if x || y {
58+
| ____________^
59+
LL | | z = true;
60+
LL | | } else {
61+
LL | | z = false;
62+
LL | | }
63+
| |_____^ help: you can reduce it to: `{ z = x || y; }`
64+
65+
error: aborting due to 5 previous errors
5566

0 commit comments

Comments
 (0)