Skip to content
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

Better error messages: Tell programmer about forgotten let #101880

Closed
Kaligule opened this issue Sep 16, 2022 · 2 comments · Fixed by #101908
Closed

Better error messages: Tell programmer about forgotten let #101880

Kaligule opened this issue Sep 16, 2022 · 2 comments · Fixed by #101908
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.

Comments

@Kaligule
Copy link

Kaligule commented Sep 16, 2022

As a python programmer, forgetting a let in an assignment to a variable that didn't exist before is an easy mistake to make.

example code

fn main() {
    // forgot the "let" here
    horses = ["Secretariat", "Seabiscuit", "Binky"];
    println!("I have {} horses.", horses.len());
}

(link to playground)

Rust sees two uses of an undefined variable here (E0425 twice):

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `horses` in this scope
 --> src/main.rs:3:5
  |
3 |     horses = ["Secretariat", "Seabiscuit", "Binky"];
  |     ^^^^^^ not found in this scope
error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `horses` in this scope
 --> src/main.rs:4:35
  |
4 |     println!("I have {} horses.", horses.len());
  |                                   ^^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.

What I would hope for

While the second one is fine, in the first case I would have really appreciated a sentence like this:

Did you mean to use `let` to introduce a new variable? Try `let horses = ...`

This would helped me as a newcomer to the language, because it does an educated guess what the problem might be and tells me how to fix it.

I think the hint should be added if the value is assigned to (first error), but not if it is used as an argument (second error).

I hope this is the right place and format to file this suggestion, I would appreciate hints if this is not the case.

@Rageking8
Copy link
Contributor

I hope this is the right place and format to file this suggestion, I would appreciate hints if this is not the case.

This is the right place, however you should use the "diagnostic" template when creating issues like this. Thanks for the issue though.

@rustbot label +T-compiler +A-diagnostics

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 16, 2022
@chenyukang
Copy link
Member

@rustbot claim

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Sep 16, 2022
notriddle added a commit to notriddle/rust that referenced this issue Oct 21, 2022
Suggest let for assignment, and some code refactor

Fixes rust-lang#101880
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 23, 2022
Suggest let for assignment, and some code refactor

Fixes rust-lang#101880
@bors bors closed this as completed in eb68e27 Oct 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants