Skip to content

clippy: avoid invalid ref_as_ptr suggestions in const/static initializers#17415

Merged
llogiq merged 1 commit into
rust-lang:masterfrom
rabindra789:fix/ref-as-ptr-const-static
Jul 15, 2026
Merged

clippy: avoid invalid ref_as_ptr suggestions in const/static initializers#17415
llogiq merged 1 commit into
rust-lang:masterfrom
rabindra789:fix/ref-as-ptr-const-static

Conversation

@rabindra789

@rabindra789 rabindra789 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #13910.

ref_as_ptr can suggest std::ptr::from_ref/std::ptr::from_mut for nested reference-to-raw-pointer casts inside const and static initializers. The suggested replacement then fails during const evaluation.

The existing guard only checks the immediate expression use site, which misses nested cast expressions. Extend it to also check whether the expression is inside an always-const context using is_inside_always_const_context.

Testing

  • Added a regression test reproducing the reported nested static mut case.
  • Ran the ref_as_ptr UI tests.

changelog: [ref_as_ptr]: avoid invalid suggestions in const/static initializers

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Jul 13, 2026
@rustbot

rustbot commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome!

You should hear from one of our reviewers after this PR gets at least 2 reviews from the community.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 13, 2026
@rabindra789 rabindra789 marked this pull request as draft July 13, 2026 06:54
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 13, 2026
Extend the existing const/static guard to also handle nested cast
expressions. This prevents \
ef_as_ptr\ from suggesting
\ptr::from_ref\/\ptr::from_mut\ replacements that fail during
const evaluation.
@rabindra789 rabindra789 force-pushed the fix/ref-as-ptr-const-static branch from 906d12f to d728c37 Compare July 13, 2026 06:57
@rabindra789 rabindra789 changed the title clippy: avoid invalid \ ef_as_ptr\ suggestions in const/static initializers clippy: avoid invalid ref_as_ptr suggestions in const/static initializers Jul 13, 2026
@rabindra789 rabindra789 marked this pull request as ready for review July 13, 2026 07:02
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 13, 2026

@DanielEScherzer DanielEScherzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

community review: makes sense to me

View changes since this review

@rustbot rustbot removed the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Jul 13, 2026
@rustbot

rustbot commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

r? @llogiq

rustbot has assigned @llogiq for the project review.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, llogiq

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thank you.

View changes since this review

@llogiq llogiq added this pull request to the merge queue Jul 15, 2026
Merged via the queue into rust-lang:master with commit eed04f6 Jul 15, 2026
15 of 17 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 15, 2026
@rabindra789 rabindra789 deleted the fix/ref-as-ptr-const-static branch July 15, 2026 13:16
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.

ref-as-ptr causes compiler error

5 participants