Skip to content

Commit e7acf5c

Browse files
committed
fix: collapsible_if FP on block stmt before expr
1 parent 50e0bf1 commit e7acf5c

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

clippy_lints/src/collapsible_if.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block, snippet_block_with_applicability};
55
use rustc_ast::BinOpKind;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Block, Expr, ExprKind, StmtKind};
7+
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::impl_lint_pass;
1010
use rustc_span::Span;
@@ -203,13 +203,12 @@ fn block_starts_with_comment(cx: &LateContext<'_>, block: &Block<'_>) -> bool {
203203
fn expr_block<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
204204
match block.stmts {
205205
[] => block.expr,
206-
[stmt] => {
207-
if let StmtKind::Semi(expr) = stmt.kind {
208-
Some(expr)
209-
} else {
210-
None
211-
}
212-
},
206+
[
207+
Stmt {
208+
kind: StmtKind::Semi(expr),
209+
..
210+
},
211+
] if block.expr.is_none() => Some(expr),
213212
_ => None,
214213
}
215214
}

tests/ui/collapsible_if.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,14 @@ fn layout_check() -> u32 {
143143
; 3
144144
//~^^^^^ collapsible_if
145145
}
146+
147+
fn issue14722() {
148+
let x = if true {
149+
Some(1)
150+
} else {
151+
if true {
152+
println!("Some debug information");
153+
};
154+
None
155+
};
156+
}

tests/ui/collapsible_if.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,14 @@ fn layout_check() -> u32 {
153153
}; 3
154154
//~^^^^^ collapsible_if
155155
}
156+
157+
fn issue14722() {
158+
let x = if true {
159+
Some(1)
160+
} else {
161+
if true {
162+
println!("Some debug information");
163+
};
164+
None
165+
};
166+
}

0 commit comments

Comments
 (0)