Skip to content

Wrong suggestions for typos on variables that have the same name as struct fields #97311

Closed
@TaKO8Ki

Description

@TaKO8Ki

Given the following code:

1.63.0-nightly (2022-05-22)

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=bb6ee98b3c92917f9eab8607bf3f5deb

struct A {
    config: String,
}

impl A {
    fn do_something(self, cofig: String) {
        println!("{config}"); //~ Error cannot find value `config` in this scope
    }
}

fn main() {}

The current output is:

This suggestion can cause compiler errors.

Compiling playground v0.0.1 (/playground)
error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find value `config` in this scope
 --> src/main.rs:7:20
  |
7 |         println!("{config}"); //~ Error cannot find value `config` in this scope
  |                    ^^^^^^ help: you might have meant to use the available field: `self.config`

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

help shouldn't be emitted.

or should look like println!("{}", self.config).

Compiling playground v0.0.1 (/playground)
error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find value `config` in this scope
 --> src/main.rs:7:20
  |
7 |         println!("{config}"); //~ Error cannot find value `config` in this scope
  |                    ^^^^^^

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions