Skip to content

Improve the error message of typos on variables that has the same name as struct fields #97133

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
matheus-consoli opened this issue May 18, 2022 · 0 comments · Fixed by #97240
Assignees
Labels
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.

Comments

@matheus-consoli
Copy link

If a method's arguments have a name close to a struct field and we use the field name by mistake when trying to refer to the variable, the message is not very helpful. This should be more common inside new methods.

Given the following code: playground

struct Foo {
    config: String,
}

impl Foo {
    fn new(cofig: String) -> Self {
        // ^^^^^ typo
        Self { config }
    }

    fn do_something(cofig: String) {
        println!("{config}");
        //         ^^^^^^ typo (?)
    }
}

The current output is:

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `config` in this scope
 --> src/lib.rs:8:16
  |
8 |         Self { config }
  |                ^^^^^^ a field by this name exists in `Self`

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `config` in this scope
  --> src/lib.rs:12:20
   |
12 |         println!("{config}");
   |                    ^^^^^^ a field by this name exists in `Self`

Compare the new error message to a free-function-style new:

Ideally, the output of the new should look like this:

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `config` in this scope
  --> src/lib.rs:19:11
   |
19 |     Foo { config }
   |           ^^^^^^ help: a local variable with a similar name exists: `cofig`

Is quite not there yet, but is much more helpful as it points to a typo I didn't know I have. In this case, we could either the message pointing to me that I should write Foo { config: cofig }, or, ideally, pointing that cofig may be a typo to config.

The second case (do_something), is more complicated as It's not clear whether cofig is related to config or not.

Sorry if this is duplication, I couldn't find a similar issue 😅 .

@matheus-consoli matheus-consoli 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 May 18, 2022
@TaKO8Ki TaKO8Ki self-assigned this May 18, 2022
@bors bors closed this as completed in 8d9f258 May 24, 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 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.

2 participants