-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Typo suggestion for a variable with a name similar to struct fields #97240
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
Merged
bors
merged 2 commits into
rust-lang:master
from
TaKO8Ki:improve-errors-about-typos-on-variables
May 24, 2022
+163
−7
Merged
Changes from all commits
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 hidden or 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
46 changes: 46 additions & 0 deletions
46
src/test/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs
This file contains hidden or 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,46 @@ | ||
struct A { | ||
config: String, | ||
} | ||
|
||
impl A { | ||
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 | ||
} | ||
} | ||
|
||
trait B { | ||
const BAR: u32 = 3; | ||
type Baz; | ||
fn bar(&self); | ||
fn baz(&self) {} | ||
fn bah() {} | ||
} | ||
|
||
impl B for Box<isize> { | ||
type Baz = String; | ||
fn bar(&self) { | ||
// let baz = 3; | ||
baz(); | ||
//~^ ERROR cannot find function `baz` | ||
bah; | ||
//~^ ERROR cannot find value `bah` | ||
BAR; | ||
//~^ ERROR cannot find value `BAR` in this scope | ||
let foo: Baz = "".to_string(); | ||
//~^ ERROR cannot find type `Baz` in this scope | ||
} | ||
} | ||
|
||
fn ba() {} | ||
const BARR: u32 = 3; | ||
type Bar = String; | ||
|
||
fn main() {} |
109 changes: 109 additions & 0 deletions
109
src/test/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr
This file contains hidden or 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,109 @@ | ||
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[E0425]: cannot find function `baz` in this scope | ||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:31:9 | ||
| | ||
LL | baz(); | ||
| ^^^ | ||
... | ||
LL | fn ba() {} | ||
| ------- similarly named function `ba` defined here | ||
| | ||
help: you might have meant to call the method | ||
| | ||
LL | self.baz(); | ||
| ~~~~~~~~ | ||
help: a function with a similar name exists | ||
| | ||
LL | ba(); | ||
| ~~ | ||
|
||
error[E0425]: cannot find value `bah` in this scope | ||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:33:9 | ||
| | ||
LL | bah; | ||
| ^^^ | ||
... | ||
LL | fn ba() {} | ||
| ------- similarly named function `ba` defined here | ||
| | ||
help: you might have meant to call the associated function | ||
| | ||
LL | Self::bah; | ||
| ~~~~~~~~~ | ||
help: a function with a similar name exists | ||
| | ||
LL | ba; | ||
| ~~ | ||
|
||
error[E0425]: cannot find value `BAR` in this scope | ||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:35:9 | ||
| | ||
LL | BAR; | ||
| ^^^ | ||
... | ||
LL | const BARR: u32 = 3; | ||
| -------------------- similarly named constant `BARR` defined here | ||
| | ||
help: you might have meant to use the associated `const` | ||
| | ||
LL | Self::BAR; | ||
| ~~~~~~~~~ | ||
help: a constant with a similar name exists | ||
| | ||
LL | BARR; | ||
| ~~~~ | ||
|
||
error[E0412]: cannot find type `Baz` in this scope | ||
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:37:18 | ||
| | ||
LL | let foo: Baz = "".to_string(); | ||
| ^^^ | ||
... | ||
LL | type Bar = String; | ||
| ------------------ similarly named type alias `Bar` defined here | ||
| | ||
help: you might have meant to use the associated type | ||
| | ||
LL | let foo: Self::Baz = "".to_string(); | ||
| ~~~~~~~~~ | ||
help: a type alias with a similar name exists | ||
| | ||
LL | let foo: Bar = "".to_string(); | ||
| ~~~ | ||
|
||
error: aborting due to 7 previous errors | ||
|
||
Some errors have detailed explanations: E0412, E0425. | ||
For more information about an error, try `rustc --explain E0412`. |
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.