Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lengyijun committed Sep 21, 2023
1 parent 3cbf4d0 commit 6731d73
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
9 changes: 2 additions & 7 deletions clippy_lints/src/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,9 @@ where
}

fn check_and_warn(cx: &EarlyContext<'_>, expr: &ast::Expr) {
if let ast::ExprKind::Loop(loop_block, loop_label, ..)
| ast::ExprKind::While(_, loop_block, loop_label)
| ast::ExprKind::ForLoop(_, _, loop_block, loop_label) = &expr.kind
{
with_loop_block(expr, |loop_block, label| {
let p = |continue_label: Option<&Label>, span: Span| {
if compare_labels(loop_label.as_ref(), continue_label) {
if compare_labels(label, continue_label) {
span_lint_and_help(
cx,
NEEDLESS_CONTINUE,
Expand All @@ -425,9 +422,7 @@ fn check_and_warn(cx: &EarlyContext<'_>, expr: &ast::Expr) {
}
};
dfs_block(loop_block, &p);
}

with_loop_block(expr, |loop_block, label| {
for (i, stmt) in loop_block.stmts.iter().enumerate() {
with_if_expr(stmt, |if_expr, cond, then_block, else_expr| {
let data = &LintData {
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,25 @@ mod issue_2329 {
}
}
}

mod issus_4077 {
fn main() {
loop {
do_something();
if some_expr() {
continue;
}
}
}

// The contents of these functions are irrelevant, the purpose of this file is
// shown in main.

fn do_something() {
std::process::exit(0);
}

fn some_expr() -> bool {
true
}
}
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
--> $DIR/needless_continue.rs:160:17
|
LL | continue;
| ^^^^^^^^^
|
= help: consider dropping the `continue` expression

error: aborting due to 9 previous errors

0 comments on commit 6731d73

Please sign in to comment.