-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-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
Given the following code:
fn main() {
let foo: Vec<&str>;
foo.push("hi!");
}
... the current output is:
error[E0381]: used binding `foo` isn't initialized
--> src/main.rs:4:5
|
2 | let foo: Vec<&str>;
| --- binding declared here but left uninitialized
3 |
4 | foo.push("hi!");
| ^^^^^^^^^^^^^^^ `foo` used here but it isn't initialized
While this message is correct, people (especially beginners) stumbling upon it sometimes still don't know what to do (e.g. they expect let var: ty;
to automatically initialize var
to Default::default()
, as is the case in some languages).
Maybe we could extend the message to say:
error[E0381]: used binding `foo` isn't initialized
--> src/main.rs:4:5
|
2 | let foo: Vec<&str>;
| --- binding declared here but left uninitialized
3 |
4 | foo.push("hi!");
| ^^^^^^^^^^^^^^^ `foo` used here but it isn't initialized
|
help: use `=` to assign some value to `foo`
|
2 | let foo: Vec<&str> = something;
| ~~~~~~~~~
... with an extra case that if ty
is Default
:
help: use `=` to assign default value to `foo`
|
2 | let foo: Vec<&str> = Default::default();
| ~~~~~~~~~~~~~~~~~~
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-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.