-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Detect likely .
-> ..
typo in method calls
#105765
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
Merged
Changes from all commits
Commits
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
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
30 changes: 30 additions & 0 deletions
30
src/test/ui/suggestions/method-access-to-range-literal-typo.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,30 @@ | ||
fn as_ref() -> Option<Vec<u8>> { | ||
None | ||
} | ||
struct Type { | ||
option: Option<Vec<u8>> | ||
} | ||
trait Trait { | ||
fn foo(&self) -> Vec<u8>; | ||
} | ||
impl Trait for Option<Vec<u8>> { | ||
fn foo(&self) -> Vec<u8> { | ||
vec![1, 2, 3] | ||
} | ||
} | ||
|
||
impl Type { | ||
fn method(&self) -> Option<Vec<u8>> { | ||
self.option..as_ref().map(|x| x) | ||
//~^ ERROR E0308 | ||
} | ||
fn method2(&self) -> &u8 { | ||
self.option..foo().get(0) | ||
//~^ ERROR E0425 | ||
//~| ERROR E0308 | ||
} | ||
} | ||
|
||
fn main() { | ||
let _ = Type { option: None }.method(); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/ui/suggestions/method-access-to-range-literal-typo.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,48 @@ | ||
error[E0425]: cannot find function `foo` in this scope | ||
--> $DIR/method-access-to-range-literal-typo.rs:22:22 | ||
| | ||
LL | self.option..foo().get(0) | ||
| ^^^ not found in this scope | ||
| | ||
help: you might have meant to write `.` instead of `..` | ||
| | ||
LL - self.option..foo().get(0) | ||
LL + self.option.foo().get(0) | ||
| | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/method-access-to-range-literal-typo.rs:18:9 | ||
| | ||
LL | fn method(&self) -> Option<Vec<u8>> { | ||
| --------------- expected `Option<Vec<u8>>` because of return type | ||
LL | self.option..as_ref().map(|x| x) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Option`, found struct `Range` | ||
| | ||
= note: expected enum `Option<_>` | ||
found struct `std::ops::Range<Option<_>>` | ||
help: you likely meant to write a method call instead of a range | ||
| | ||
LL - self.option..as_ref().map(|x| x) | ||
LL + self.option.as_ref().map(|x| x) | ||
| | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/method-access-to-range-literal-typo.rs:22:9 | ||
| | ||
LL | fn method2(&self) -> &u8 { | ||
| --- expected `&u8` because of return type | ||
LL | self.option..foo().get(0) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&u8`, found struct `Range` | ||
| | ||
= note: expected reference `&u8` | ||
found struct `std::ops::Range<Option<Vec<u8>>>` | ||
help: you likely meant to write a method call instead of a range | ||
| | ||
LL - self.option..foo().get(0) | ||
LL + self.option.foo().get(0) | ||
| | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0308, E0425. | ||
For more information about an error, try `rustc --explain E0308`. |
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.
I didn't look too much into this, but does this check that this is a method?
We should also probably double check this is a path with one segment.
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.
It doesn't, but during resolve we don't have enough data to do that (although now that I'm tracking
start
I might be able to cobble something together).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'm comfortable with the weasel wording on this one suggestion: it is a guess, and we'd have to perform more extensive surgery on resolve to track more state, and it is already quite a beast.
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.
My only concern is that this triggers when we have any resolution error on the RHS of a range, e.g.
Results in:
I'd much prefer if the wording was made to not mention methods in specific, but focus on the
.
itself -- something like "you might've meant to write the.
operator instead of a range"?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.
What do you think of "you might have meant to write
.
instead of..
"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, that works fine.
Just would prefer it to not mention "method" unless we can verify it's actually a method, but since this code is in resolve, I totally understand how that's hard, haha.