-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Suggest method syntax x.max(y)
for unresolved max(x, y)
calls
#147102
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
Closed
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
fn main() { | ||
//~^ HELP consider importing this function | ||
//~| HELP consider importing this function | ||
//~| HELP consider importing this function | ||
//~| HELP consider importing this function | ||
let x = 2; | ||
let y = 4; | ||
|
||
max(x, y); | ||
//~^ ERROR cannot find function `max` in this scope | ||
//~| HELP you may have meant to use the method syntax | ||
let _ = min(x, y); | ||
//~^ ERROR cannot find function `min` in this scope | ||
//~| HELP you may have meant to use the method syntax | ||
println!("{}", min(43, 43)); | ||
//~^ ERROR cannot find function `min` in this scope | ||
//~| HELP you may have meant to use the method syntax | ||
let _ = vec![max(f(), g())]; | ||
//~^ ERROR cannot find function `max` in this scope | ||
//~| HELP you may have meant to use the method syntax | ||
} | ||
|
||
const fn f() -> u32 { | ||
4 | ||
} | ||
|
||
const fn g() -> u32 { | ||
2 | ||
} |
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,67 @@ | ||
error[E0425]: cannot find function `max` in this scope | ||
--> $DIR/method-syntax-for-min-max.rs:9:5 | ||
| | ||
LL | max(x, y); | ||
| ^^^ not found in this scope | ||
| | ||
help: you may have meant to use the method syntax | ||
| | ||
LL - max(x, y); | ||
LL + x.max(y); | ||
| | ||
help: consider importing this function | ||
| | ||
LL + use std::cmp::max; | ||
| | ||
|
||
error[E0425]: cannot find function `min` in this scope | ||
--> $DIR/method-syntax-for-min-max.rs:12:13 | ||
| | ||
LL | let _ = min(x, y); | ||
| ^^^ not found in this scope | ||
| | ||
help: you may have meant to use the method syntax | ||
| | ||
LL - let _ = min(x, y); | ||
LL + let _ = x.min(y); | ||
| | ||
help: consider importing this function | ||
| | ||
LL + use std::cmp::min; | ||
| | ||
|
||
error[E0425]: cannot find function `min` in this scope | ||
--> $DIR/method-syntax-for-min-max.rs:15:20 | ||
| | ||
LL | println!("{}", min(43, 43)); | ||
| ^^^ not found in this scope | ||
| | ||
help: you may have meant to use the method syntax | ||
| | ||
LL - println!("{}", min(43, 43)); | ||
LL + println!("{}", 43.min(43)); | ||
| | ||
help: consider importing this function | ||
| | ||
LL + use std::cmp::min; | ||
| | ||
|
||
error[E0425]: cannot find function `max` in this scope | ||
--> $DIR/method-syntax-for-min-max.rs:18:18 | ||
| | ||
LL | let _ = vec![max(f(), g())]; | ||
| ^^^ not found in this scope | ||
| | ||
help: you may have meant to use the method syntax | ||
| | ||
LL - let _ = vec![max(f(), g())]; | ||
LL + let _ = vec![f().max(g())]; | ||
| | ||
help: consider importing this function | ||
| | ||
LL + use std::cmp::max; | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0425`. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
As a rule of thumb, please never unwrap
span_to_snippet
, it can indeed fail.Under your PR we likely crash under a scenario like (not tested):
Then compile
a.rs
and delete or movea.rs
and compileb.rs
with--extern a
.Uh oh!
There was an error while loading. Please reload this page.
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.
Instead of using
span_to_snippet
, construct a multi-part suggestion that maps the span ofmin(
ormax(
to empty and the span of,
to.min(
or.max(
respectively. These subspans can be obtained viaSpan::{to,between,until}
etc. However, you'd probably still want to guard against differing expansion levels viaeq_ctxt
(heavy hammer) orfind_*_ancestor_*
(brittle).Uh oh!
There was an error while loading. Please reload this page.
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.
Independently, you gonna want to account for more complex expressions that need to be wrapped in parentheses. E.g:
max(1 + 1, 0)
→(1 + 1).max(0)
not1 + 1.max(0)
.Uh oh!
There was an error while loading. Please reload this page.
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.
Hm, do you have idea how can I protect myself from more complex expressions like this, always wrap left part in parentheses? or there is something better I can do about it
Also about multi-part suggestion I'm not sure if I can use it here because as far I as remember it's something with vec of strings and the final type of variable that using here is like this
Option<(&Span, &str, String)>
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.
Yes, you can :) since we're trying to construct a method call, if
first_arg.precedence() < ExprPrecedence::Unambiguous
it needs parentheses, otherwise it doesn't. That's a good approximation that's also used byrustc_ast_pretty
essentially.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.
Arf, that's annoying; in that case only suggest something if both
span_to_snippet
s areOk(_)
.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.
Yep, I guess I can even add this inside let chains, right after a some zulip poll if we decide on necessity of this diagnostics