Skip to content

Commit ec6f8c1

Browse files
samueltardieuflip1995
authored andcommitted
Fix collapsible_if FP on block stmt before expr (#14730)
Closes rust-lang/rust-clippy#14722 changelog: [`collapsible_if`] fix FP on block stmt before expr
1 parent 8443caf commit ec6f8c1

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/tools/clippy/clippy_lints/src/collapsible_if.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block, snippet_block_with_applicability};
44
use rustc_ast::BinOpKind;
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Block, Expr, ExprKind, StmtKind};
6+
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::impl_lint_pass;
@@ -202,13 +202,12 @@ fn block_starts_with_comment(cx: &LateContext<'_>, block: &Block<'_>) -> bool {
202202
fn expr_block<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
203203
match block.stmts {
204204
[] => block.expr,
205-
[stmt] => {
206-
if let StmtKind::Semi(expr) = stmt.kind {
207-
Some(expr)
208-
} else {
209-
None
210-
}
211-
},
205+
[
206+
Stmt {
207+
kind: StmtKind::Semi(expr),
208+
..
209+
},
210+
] if block.expr.is_none() => Some(expr),
212211
_ => None,
213212
}
214213
}

src/tools/clippy/tests/ui/collapsible_if.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,14 @@ fn layout_check() -> u32 {
162162
; 3
163163
//~^^^^^ collapsible_if
164164
}
165+
166+
fn issue14722() {
167+
let x = if true {
168+
Some(1)
169+
} else {
170+
if true {
171+
println!("Some debug information");
172+
};
173+
None
174+
};
175+
}

src/tools/clippy/tests/ui/collapsible_if.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,14 @@ fn layout_check() -> u32 {
172172
}; 3
173173
//~^^^^^ collapsible_if
174174
}
175+
176+
fn issue14722() {
177+
let x = if true {
178+
Some(1)
179+
} else {
180+
if true {
181+
println!("Some debug information");
182+
};
183+
None
184+
};
185+
}

0 commit comments

Comments
 (0)