-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Typo suggestion for a variable with a name similar to struct fields #97240
Merged
bors
merged 2 commits into
rust-lang:master
from
TaKO8Ki:improve-errors-about-typos-on-variables
May 24, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/test/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
struct Foo { | ||
config: String, | ||
} | ||
|
||
impl Foo { | ||
fn new(cofig: String) -> Self { | ||
Self { config } //~ Error cannot find value `config` in this scope | ||
} | ||
|
||
fn do_something(cofig: String) { | ||
println!("{config}"); //~ Error cannot find value `config` in this scope | ||
} | ||
|
||
fn self_is_available(self, cofig: String) { | ||
println!("{config}"); //~ Error cannot find value `config` in this scope | ||
} | ||
} | ||
|
||
fn main() {} |
36 changes: 36 additions & 0 deletions
36
src/test/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
error[E0425]: cannot find value `config` in this scope | ||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:7:16 | ||
| | ||
LL | Self { config } | ||
| ^^^^^^ | ||
| | | ||
| a field by this name exists in `Self` | ||
| help: a local variable with a similar name exists: `cofig` | ||
|
||
error[E0425]: cannot find value `config` in this scope | ||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:11:20 | ||
| | ||
LL | println!("{config}"); | ||
| ^^^^^^ | ||
| | | ||
| a field by this name exists in `Self` | ||
| help: a local variable with a similar name exists: `cofig` | ||
|
||
error[E0425]: cannot find value `config` in this scope | ||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20 | ||
| | ||
LL | println!("{config}"); | ||
| ^^^^^^ | ||
| | ||
help: you might have meant to use the available field | ||
| | ||
LL | println!("{self.config}"); | ||
| ~~~~~~~~~~~ | ||
help: a local variable with a similar name exists | ||
| | ||
LL | println!("{cofig}"); | ||
| ~~~~~ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0425`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive-by comment: this probably shouldn't be emitted, as it won't compile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that's a bit orthogonal to this diff, though. It might be possible to detect this case from HIR, but not related to the suggestion added here.
@TaKO8Ki, I would appreciate if you find some time to investigate suppressing this. Don't block this PR on that if it's more difficult than it seems -- if not immediately easy to fix, then could you file an issue and downgrade the suggestion from
MachineApplicable
toMaybeIncorrect
? 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I will investigate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #97311. Can I work on this problem in a new PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sounds good.