Skip to content

Commit 18e1f25

Browse files
committedJan 27, 2024
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
2 parents 85e08cd + ff5afac commit 18e1f25

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed
 

Diff for: ‎clippy_lints/src/loops/never_loop.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ fn never_loop_expr<'tcx>(
201201
})
202202
})
203203
},
204-
ExprKind::Block(b, l) => {
205-
if l.is_some() {
204+
ExprKind::Block(b, _) => {
205+
if b.targeted_by_break {
206206
local_labels.push((b.hir_id, false));
207207
}
208208
let ret = never_loop_block(cx, b, local_labels, main_loop_id);
209-
let jumped_to = l.is_some() && local_labels.pop().unwrap().1;
209+
let jumped_to = b.targeted_by_break && local_labels.pop().unwrap().1;
210210
match ret {
211211
NeverLoopResult::Diverging if jumped_to => NeverLoopResult::Normal,
212212
_ => ret,

Diff for: ‎tests/ui/never_loop.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(inline_const)]
1+
#![feature(inline_const, try_blocks)]
22
#![allow(
33
clippy::eq_op,
44
clippy::single_match,
@@ -400,6 +400,15 @@ pub fn test32() {
400400
}
401401
}
402402

403+
pub fn issue12205() -> Option<()> {
404+
loop {
405+
let _: Option<_> = try {
406+
None?;
407+
return Some(());
408+
};
409+
}
410+
}
411+
403412
fn main() {
404413
test1();
405414
test2();

0 commit comments

Comments
 (0)