Skip to content

Commit

Permalink
Auto merge of #12206 - y21:issue12205, r=Alexendoo
Browse files Browse the repository at this point in the history
[`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
  • Loading branch information
bors committed Jan 27, 2024
2 parents 85e08cd + ff5afac commit 18e1f25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions clippy_lints/src/loops/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ fn never_loop_expr<'tcx>(
})
})
},
ExprKind::Block(b, l) => {
if l.is_some() {
ExprKind::Block(b, _) => {
if b.targeted_by_break {
local_labels.push((b.hir_id, false));
}
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 {
NeverLoopResult::Diverging if jumped_to => NeverLoopResult::Normal,
_ => ret,
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/never_loop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(inline_const)]
#![feature(inline_const, try_blocks)]
#![allow(
clippy::eq_op,
clippy::single_match,
Expand Down Expand Up @@ -400,6 +400,15 @@ pub fn test32() {
}
}

pub fn issue12205() -> Option<()> {
loop {
let _: Option<_> = try {
None?;
return Some(());
};
}
}

fn main() {
test1();
test2();
Expand Down

0 comments on commit 18e1f25

Please sign in to comment.