Missing help message when forgetting to use the let
keyword #132483
Description
Code
fn main() {
let n = 1;
n1 = 2;
}
Current output
error[E0425]: cannot find value `n1` in this scope
--> src/main.rs:3:5
|
3 | n1 = 2;
| ^^ help: a local variable with a similar name exists: `n`
For more information about this error, try `rustc --explain E0425`.
Desired output
error[E0425]: cannot find value `n1` in this scope
--> src/main.rs:3:5
|
3 | n1 = 2;
| ^^ help: a local variable with a similar name exists: `n`
|
help: you might have meant to introduce a new binding
|
3 | let n1 = 2;
| +++
For more information about this error, try `rustc --explain E0425`.
Rationale and extra context
Coming from a Python background, I sometimes forget to use the let
keyword in Rust when binding a value to a new variable. In such cases, it would be useful to get a help message from the compiler that asks the programmer to do so (help: you might have meant to introduce a new binding
). I noticed that sometimes the compiler does give such a message, but sometimes not. Suppose I want to define two variables and I forget to use the let
statement for the second variable. After some experimentation, it seems like the desired help message appears after a pattern common to both variable names is matched and the non-matched part has a length that crosses a certain threshold. I will give some examples below. Assume that the first variable's name is never changed, and we tweak the second variable's name slightly to see when the message help: you might have meant to introduce a new binding
is triggered. Note that the other help message help: a local variable with a similar name exists
is always triggered, which is not always helpful/relevant, and is therefore one of the reasons why I raised this issue.
- The matched pattern is 'n' and the differing characters between the two variable names are '11', and I do get the desired help message from the compiler. The difference here is in 2 characters, but this is not always the minimum number required to trigger the help message as shown in example 3. below.
let n = 1;
n11 = 2;
- The desired help message in the following case is triggered when the number of differing characters is at least 2 (e.g.
abc = 2
does not trigger the message).
let ab = 1;
abcd = 2;
- The desired help message in the following case is triggered when the number of differing characters is at least 4 (e.g.
abcdefghi = 2
does not trigger the message).
let abcdef = 1;
abcdefghij = 2;
- Now let's flip the first 2 characters of the 2 variables. So suppose I have the following:
let abcdef = 1;
ba = 2;
where the first 2 characters of the 2 variables are flipped (ab
versus ba
). Then, I will start adding one new (and different) character (in alphabetical order) at a time to the second variable's name and compile to see when the help message appears. We see the message in the following cases: ba
, bac
, bacd
, bacde
. Then the message disappears in the following cases: bacdef
, bacdefg
. And then reappears in the case bacdefgh
, etc.
- As a last example, suppose a user is interested in doing some data analysis and names a variable
data_attribute
. Then they name another variable with a similar name but with an underscore '_' followed by a numerical suffix. They will not get the desired help message unless they append at least 7 digits. So this means that
let data_attribute = 1;
data_attribute_1234567 = 2;
is the first time the help message appears. The message does not appear when the second variable's name is one of the following: data_attribute_1
, data_attribute_12
, data_attribute_123
, data_attribute_1234
, data_attribute_12345
, data_attribute_123456
.
These are just some random cases I quickly thought of, but perhaps there are more rules to when a message is triggered. The variable names in the examples above (except the last one) are not the best, but I used them to show which events trigger the message. But from especially the last example, one would think that data_attribute
is completely different from e.g. data_attribute_71
for many use cases, and so the desired help message would be quite useful.
Can we not have the message triggered every time a let
statement is forgotten?
Other cases
No response
Rust Version
rustc 1.82.0 (f6e511e 2024-10-15)
binary: rustc
commit-hash: f6e511e
commit-date: 2024-10-15
host: aarch64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1
Anything else?
No response