Description
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 😅 .