Skip to content

Commit 121f867

Browse files
Do not trigger let_and_return on let else (#16829)
changelog: [`let_and_return_on`]: do not trigger on `let else` construct Fixes rust-lang/rust-clippy#16820
2 parents a333abf + 33a3b22 commit 121f867

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

clippy_lints/src/returns/let_and_return.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(super) fn check_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'_>)
1919
&& let Some(stmt) = block.stmts.last()
2020
&& let StmtKind::Let(local) = &stmt.kind
2121
&& local.ty.is_none()
22+
&& local.els.is_none()
2223
&& cx.tcx.hir_attrs(local.hir_id).is_empty()
2324
&& let Some(initexpr) = &local.init
2425
&& let PatKind::Binding(_, local_id, _, _) = local.pat.kind

tests/ui/let_and_return.edition2021.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,14 @@ fn wrongly_unmangled_macros() -> i32 {
292292
//~^ let_and_return
293293
}
294294

295+
fn issue16820() -> Option<i32> {
296+
let value = Some(42);
297+
298+
let v @ None = value else {
299+
panic!("uh oh!");
300+
};
301+
302+
v
303+
}
304+
295305
fn main() {}

tests/ui/let_and_return.edition2024.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,14 @@ fn wrongly_unmangled_macros() -> i32 {
292292
//~^ let_and_return
293293
}
294294

295+
fn issue16820() -> Option<i32> {
296+
let value = Some(42);
297+
298+
let v @ None = value else {
299+
panic!("uh oh!");
300+
};
301+
302+
v
303+
}
304+
295305
fn main() {}

tests/ui/let_and_return.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,14 @@ fn wrongly_unmangled_macros() -> i32 {
292292
//~^ let_and_return
293293
}
294294

295+
fn issue16820() -> Option<i32> {
296+
let value = Some(42);
297+
298+
let v @ None = value else {
299+
panic!("uh oh!");
300+
};
301+
302+
v
303+
}
304+
295305
fn main() {}

0 commit comments

Comments
 (0)