Misleading error message when variable is not re-initialized in loop #83760
Labels
A-borrow-checker
Area: The borrow checker
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
D-terse
Diagnostics: An error or lint that doesn't give enough information about the problem at hand.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
The following code compiles:
If we don't ensure that
val
is re-initialized at the end of the loop:we get the following error:
This error message seems to imply that the
while let pat = variable { ... }
pattern is invalid. However, the fact thatval
gets re-initialized in one branch of the loop means that the user likely intended to initialize it in all branches.We should detect when this kind of case occurs, and explain that
val
might not be initialized at the end of the loop. We could even try to detect where the missing initializations need to be inserted, but that might be very difficult.The text was updated successfully, but these errors were encountered: