Skip to content

Commit 392b2ef

Browse files
committed
Fix blocks_in_if_conditions false positive
1 parent 07f4f7c commit 392b2ef

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

clippy_lints/src/blocks_in_if_conditions.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
7272

7373
let body = self.cx.tcx.hir().body(eid);
7474
let ex = &body.value;
75-
if matches!(ex.kind, ExprKind::Block(_, _)) && !body.value.span.from_expansion() {
76-
self.found_block = Some(ex);
77-
return;
75+
if let ExprKind::Block(block, _) = ex.kind {
76+
if !body.value.span.from_expansion() && !block.stmts.is_empty() {
77+
self.found_block = Some(ex);
78+
return;
79+
}
7880
}
7981
}
8082
walk_expr(self, expr);

tests/ui/blocks_in_if_conditions_closure.rs

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ fn macro_in_closure() {
4444
}
4545
}
4646

47+
fn closure(_: impl FnMut()) -> bool {
48+
true
49+
}
50+
51+
fn function_with_empty_closure() {
52+
if closure(|| {}) {}
53+
}
54+
4755
#[rustfmt::skip]
4856
fn main() {
4957
let mut range = 0..10;

0 commit comments

Comments
 (0)