Skip to content

Commit 3b99407

Browse files
author
yossydev
committed
fix: improve error messaging for unreachable loop return cases
1 parent 558ac1c commit 3b99407

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,16 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
17001700
&& let hir::ExprKind::Block(block, _) = hir.body(body_id).value.kind
17011701
&& !ty.is_never()
17021702
{
1703+
for stmt in block.stmts {
1704+
if let hir::StmtKind::Local(local) = stmt.kind {
1705+
if let hir::PatKind::Binding(_, _, _, None) = local.pat.kind {
1706+
err.span_note(
1707+
stmt.span,
1708+
"This pattern has a single binding, which might cause issues if the loop doesn't execute. Consider handling the case where the loop might not run.",
1709+
);
1710+
}
1711+
}
1712+
}
17031713
let indentation = if let None = block.expr
17041714
&& let [.., last] = &block.stmts
17051715
{

0 commit comments

Comments
 (0)