-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(match_as_ref): suggest as_ref when the reference needs to be cast
#15934
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
Conversation
3e13380 to
6a7509f
Compare
6a7509f to
44e7186
Compare
|
Opened another PR for the "two-step cast" approach: #15935, lmk which one you like more |
as_ref when the reference needs to be castas_ref when the reference needs to be cast
44e7186 to
e284f60
Compare
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 prefer this version over the other one, as it proposes to use cleaner code even if a bit further from the original code written by the user.
| applicability, | ||
| ); | ||
| if need_as_ref { | ||
| diag.note("but the type is coerced to `&_`, and so `as_ref` will need to be used instead"); |
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 would prefer this message.
| diag.note("but the type is coerced to `&_`, and so `as_ref` will need to be used instead"); | |
| diag.note("but the type is coerced to a non-mutable reference, and so `as_ref` can be used instead"); |
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 would prefer this message.
Yeah this was somewhat of a stand-in.
Though I'm not so sure about "can" here -- after all, it's not like as_mut would work by itself. This could work if we first presented the .as_mut().map() option and only then proposed changing that to .as_ref() as a way to simplify the code, but we currently don't...
That said, there seems to be a general trend in lints to try to make the suggestion type-check by putting extra things on top of it (type ascriptions, casts, etc.), and those rarely -- if ever -- get acknowledged by the help message, not to mention getting the motivation behind them explained.
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.
The idea behind "can be used" is that it "should be used", but without telling the user that they did something wrong. We are showing them the possibility for doing so instead of .as_mut().map().
e284f60 to
22bdd9f
Compare
|
@rustbot ready |
(EDIT: only remembered to add this after the PR was merged already... Fixes #15932)
I first implemented the naive version, which just talks about, and suggests using,
Option::as_ref. But the resulting error message sounded a bit misleading -- after all, the manual implementation was factually that ofOption::as_mut, and we only suggestas_refas a means of downcasting. So then I added another help message to mention the need for reference downcasting, which.. still looks awkward.Honestly it might be the easiest to hide the reference downcasting into the
.mapafter all, so that it encapsulates all the casting needed following the switch toas_ref/as_mut.changelog: [
match_as_ref]: suggestas_refwhen the reference needs to be castr? @samueltardieu