Auto merge of #12206 - y21:issue12205, r=Alexendoo

[`never_loop`]: recognize desugared `try` blocks

Fixes #12205

The old code assumed that only blocks with an explicit label can be jumped to (using `break`). This is mostly correct except for `try` desugaring, where the `?` operator is rewritten to a `break` to that block, even without a label on the block. `Block::targeted_by_break` is a little more accurate than just checking if a block has a label in that regard, so we should just use that instead

changelog: [`never_loop`]: avoid linting when `?` is used inside of a try block
This commit is contained in:
bors 2024-01-27 17:27:38 +00:00
commit 18e1f25a9f
2 changed files with 13 additions and 4 deletions

View File

@ -201,12 +201,12 @@ fn never_loop_expr<'tcx>(
}) })
}) })
}, },
ExprKind::Block(b, l) => { ExprKind::Block(b, _) => {
if l.is_some() { if b.targeted_by_break {
local_labels.push((b.hir_id, false)); local_labels.push((b.hir_id, false));
} }
let ret = never_loop_block(cx, b, local_labels, main_loop_id); let ret = never_loop_block(cx, b, local_labels, main_loop_id);
let jumped_to = l.is_some() && local_labels.pop().unwrap().1; let jumped_to = b.targeted_by_break && local_labels.pop().unwrap().1;
match ret { match ret {
NeverLoopResult::Diverging if jumped_to => NeverLoopResult::Normal, NeverLoopResult::Diverging if jumped_to => NeverLoopResult::Normal,
_ => ret, _ => ret,

View File

@ -1,4 +1,4 @@
#![feature(inline_const)] #![feature(inline_const, try_blocks)]
#![allow( #![allow(
clippy::eq_op, clippy::eq_op,
clippy::single_match, clippy::single_match,
@ -400,6 +400,15 @@ pub fn test32() {
} }
} }
pub fn issue12205() -> Option<()> {
loop {
let _: Option<_> = try {
None?;
return Some(());
};
}
}
fn main() { fn main() {
test1(); test1();
test2(); test2();