Skip to content

fix: improve error messaging for unreachable loop return cases #119178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,16 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
&& let hir::ExprKind::Block(block, _) = hir.body(body_id).value.kind
&& !ty.is_never()
{
for stmt in block.stmts {
if let hir::StmtKind::Local(local) = stmt.kind {
if let hir::PatKind::Binding(_, _, _, None) = local.pat.kind {
err.span_note(
stmt.span,
"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.",
);
}
}
}
let indentation = if let None = block.expr
&& let [.., last] = &block.stmts
{
Expand Down