-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.Category: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Forgetting to initialize a non-Copy variable in a loop and then referring to it gives a 'borrowed after move' error, instead of a 'possibly-uninitialized variable' error:
fn bla(_: &mut String) {}
fn main() {
for _ in 0..10 {
let mut x: String;
bla(&mut x);
}
}
error[E0382]: borrow of moved value: `x`
--> src/main.rs:6:13
|
5 | let mut x: String;
| ----- move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
6 | bla(&mut x);
| ^^^^^^ value borrowed here after move
7 | }
| - value moved here, in previous iteration of loop
Using a Copy
type like i32
does result in the right error:
error[E0381]: borrow of possibly-uninitialized variable: `x`
--> src/main.rs:6:13
|
6 | bla(&mut x);
| ^^^^^^ use of possibly-uninitialized `x`
Tested with 1.43.0 (stable) and 1.45.0-nightly.
avl, lukechu10, SvetlinZarev, nneonneo and GirkovArpaCGMossa, 197g, mpfaff, fasterthanlime, lukechu10 and 1 more
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.Category: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.