Skip to content

Commit 7117bd9

Browse files
committed
fix: needless_continue FP when match type is not unit or never
1 parent 0415d96 commit 7117bd9

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

clippy_lints/src/needless_continue.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ fn suggestion_snippet_for_continue_inside_else(cx: &LateContext<'_>, data: &Lint
413413
)
414414
}
415415

416-
fn check_last_stmt_in_expr<F>(inner_expr: &Expr<'_>, func: &F)
416+
fn check_last_stmt_in_expr<F>(cx: &LateContext<'_>, inner_expr: &Expr<'_>, func: &F)
417417
where
418418
F: Fn(Option<&Label>, Span),
419419
{
@@ -422,36 +422,40 @@ where
422422
func(continue_label.label.as_ref(), inner_expr.span);
423423
},
424424
ExprKind::If(_, then_block, else_block) if let ExprKind::Block(then_block, _) = then_block.kind => {
425-
check_last_stmt_in_block(then_block, func);
425+
check_last_stmt_in_block(cx, then_block, func);
426426
if let Some(else_block) = else_block {
427-
check_last_stmt_in_expr(else_block, func);
427+
check_last_stmt_in_expr(cx, else_block, func);
428428
}
429429
},
430430
ExprKind::Match(_, arms, _) => {
431+
let match_ty = cx.typeck_results().expr_ty(inner_expr);
432+
if !match_ty.is_unit() && !match_ty.is_never() {
433+
return;
434+
}
431435
for arm in arms.iter() {
432-
check_last_stmt_in_expr(arm.body, func);
436+
check_last_stmt_in_expr(cx, arm.body, func);
433437
}
434438
},
435439
ExprKind::Block(b, _) => {
436-
check_last_stmt_in_block(b, func);
440+
check_last_stmt_in_block(cx, b, func);
437441
},
438442
_ => {},
439443
}
440444
}
441445

442-
fn check_last_stmt_in_block<F>(b: &Block<'_>, func: &F)
446+
fn check_last_stmt_in_block<F>(cx: &LateContext<'_>, b: &Block<'_>, func: &F)
443447
where
444448
F: Fn(Option<&Label>, Span),
445449
{
446450
if let Some(expr) = b.expr {
447-
check_last_stmt_in_expr(expr, func);
451+
check_last_stmt_in_expr(cx, expr, func);
448452
return;
449453
}
450454

451455
if let Some(last_stmt) = b.stmts.last()
452456
&& let StmtKind::Expr(inner_expr) | StmtKind::Semi(inner_expr) = last_stmt.kind
453457
{
454-
check_last_stmt_in_expr(inner_expr, func);
458+
check_last_stmt_in_expr(cx, inner_expr, func);
455459
}
456460
}
457461

@@ -501,7 +505,7 @@ fn check_and_warn(cx: &LateContext<'_>, expr: &Expr<'_>) {
501505
}
502506

503507
if i == stmts.len() - 1 && loop_block.expr.is_none() && !maybe_emitted_in_if {
504-
check_last_stmt_in_block(loop_block, &p);
508+
check_last_stmt_in_block(cx, loop_block, &p);
505509
}
506510
}
507511

@@ -534,7 +538,7 @@ fn check_and_warn(cx: &LateContext<'_>, expr: &Expr<'_>) {
534538
});
535539

536540
if !maybe_emitted_in_if {
537-
check_last_stmt_in_block(loop_block, &p);
541+
check_last_stmt_in_block(cx, loop_block, &p);
538542
}
539543
}
540544
});

tests/ui/needless_continue.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,18 @@ mod issue_4077 {
244244
true
245245
}
246246
}
247+
248+
#[allow(clippy::let_unit_value)]
249+
fn issue14550(mut producer: impl Iterator<Item = Result<i32, u32>>) -> Result<u32, u32> {
250+
let mut counter = 2;
251+
loop {
252+
match producer.next().unwrap() {
253+
Ok(ok) => break Ok((ok + 1) as u32),
254+
Err(12) => {
255+
counter -= 1;
256+
continue;
257+
},
258+
err => err?,
259+
};
260+
}
261+
}

0 commit comments

Comments
 (0)