Skip to content

Commit

Permalink
needless_continue: check labels consistency before warning
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Nov 3, 2024
1 parent 52b8324 commit 2f1b7b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,11 @@ fn suggestion_snippet_for_continue_inside_else(cx: &EarlyContext<'_>, data: &Lin
}

fn check_and_warn(cx: &EarlyContext<'_>, expr: &ast::Expr) {
if let ast::ExprKind::Loop(loop_block, ..) = &expr.kind
if let ast::ExprKind::Loop(loop_block, loop_label, ..) = &expr.kind
&& let Some(last_stmt) = loop_block.stmts.last()
&& let ast::StmtKind::Expr(inner_expr) | ast::StmtKind::Semi(inner_expr) = &last_stmt.kind
&& let ast::ExprKind::Continue(_) = inner_expr.kind
&& let ast::ExprKind::Continue(continue_label) = inner_expr.kind
&& compare_labels(loop_label.as_ref(), continue_label.as_ref())
{
span_lint_and_help(
cx,
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,20 @@ mod issue_2329 {
}
}
}

fn issue_13641() {
'a: while std::hint::black_box(true) {
#[allow(clippy::never_loop)]
loop {
continue 'a;
}
}

#[allow(clippy::never_loop)]
while std::hint::black_box(true) {
'b: loop {
continue 'b;
//~^ ERROR: this `continue` expression is redundant
}
}
}
10 changes: 9 additions & 1 deletion tests/ui/needless_continue.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,13 @@ LL | | }
println!("bar-5");
}

error: aborting due to 8 previous errors
error: this `continue` expression is redundant
--> tests/ui/needless_continue.rs:166:13
|
LL | continue 'b;
| ^^^^^^^^^^^^
|
= help: consider dropping the `continue` expression

error: aborting due to 9 previous errors

0 comments on commit 2f1b7b6

Please sign in to comment.