Fix collapsible_if FP on block stmt before expr (rust-lang/rust-clippy#14730)

Closes rust-lang/rust-clippy#14722

changelog: [`collapsible_if`] fix FP on block stmt before expr
This commit is contained in:
Samuel Tardieu 2025-05-06 06:33:10 +00:00 committed by Josh Stone
parent 9fec7f0c94
commit 569e9ec628
3 changed files with 29 additions and 8 deletions

View file

@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block, snippet_block_with_applicability};
use rustc_ast::BinOpKind;
use rustc_errors::Applicability;
use rustc_hir::{Block, Expr, ExprKind, StmtKind};
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::TyCtxt;
use rustc_session::impl_lint_pass;
@ -202,13 +202,12 @@ fn block_starts_with_comment(cx: &LateContext<'_>, block: &Block<'_>) -> bool {
fn expr_block<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
match block.stmts {
[] => block.expr,
[stmt] => {
if let StmtKind::Semi(expr) = stmt.kind {
Some(expr)
} else {
None
}
},
[
Stmt {
kind: StmtKind::Semi(expr),
..
},
] if block.expr.is_none() => Some(expr),
_ => None,
}
}

View file

@ -162,3 +162,14 @@ fn layout_check() -> u32 {
; 3
//~^^^^^ collapsible_if
}
fn issue14722() {
let x = if true {
Some(1)
} else {
if true {
println!("Some debug information");
};
None
};
}

View file

@ -172,3 +172,14 @@ fn layout_check() -> u32 {
}; 3
//~^^^^^ collapsible_if
}
fn issue14722() {
let x = if true {
Some(1)
} else {
if true {
println!("Some debug information");
};
None
};
}