Skip to content

Conversation

@ada4a
Copy link
Contributor

@ada4a ada4a commented Oct 22, 2025

(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 of Option::as_mut, and we only suggest as_ref as 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 .map after all, so that it encapsulates all the casting needed following the switch to as_ref/as_mut.

changelog: [match_as_ref]: suggest as_ref when the reference needs to be cast

r? @samueltardieu

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 22, 2025
@ada4a ada4a force-pushed the 15932-match_as_ref branch from 3e13380 to 6a7509f Compare October 22, 2025 09:29
@ada4a ada4a marked this pull request as draft October 22, 2025 09:46
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 22, 2025
@ada4a ada4a marked this pull request as ready for review October 22, 2025 09:53
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 22, 2025
@ada4a ada4a force-pushed the 15932-match_as_ref branch from 6a7509f to 44e7186 Compare October 22, 2025 09:53
@ada4a
Copy link
Contributor Author

ada4a commented Oct 22, 2025

Opened another PR for the "two-step cast" approach: #15935, lmk which one you like more

@ada4a ada4a changed the title WIP: fix(match_as_ref): suggest as_ref when the reference needs to be cast fix(match_as_ref): suggest as_ref when the reference needs to be cast Oct 22, 2025
@ada4a ada4a force-pushed the 15932-match_as_ref branch from 44e7186 to e284f60 Compare October 22, 2025 09:59
Copy link
Member

@samueltardieu samueltardieu left a 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.

View changes since this review

applicability,
);
if need_as_ref {
diag.note("but the type is coerced to `&_`, and so `as_ref` will need to be used instead");
Copy link
Member

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.

Suggested change
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");

Copy link
Contributor Author

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.

Copy link
Member

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().

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Oct 22, 2025
@ada4a ada4a force-pushed the 15932-match_as_ref branch from e284f60 to 22bdd9f Compare October 22, 2025 11:16
@ada4a
Copy link
Contributor Author

ada4a commented Oct 22, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Oct 22, 2025
@samueltardieu samueltardieu added this pull request to the merge queue Oct 22, 2025
Merged via the queue into rust-lang:master with commit 0c592df Oct 22, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 22, 2025
@ada4a ada4a deleted the 15932-match_as_ref branch October 22, 2025 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

match_as_ref might suggest invalid code

3 participants