Skip to content

Fix static_mut_refs lint check logic - #159168

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
cclfmht:fix/issue-158735
Jul 15, 2026
Merged

Fix static_mut_refs lint check logic#159168
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
cclfmht:fix/issue-158735

Conversation

@cclfmht

@cclfmhtcclfmht commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

This PR fixes issue #158735.

Previously, the lint might suggested using interior mutable type even if it the compiler already decides that the referenced type is interior mutable, while it didn't give such suggestion when the referenced type is considered non-interior mutable. This commit refined the logic as follow: if the referenced type is not interior mutable, then suggests using types with interior mutability; otherwise, suggests removing mut if the reference is a shared reference. Note that in the latter case, compiler might be silent if the span of the static mut definition is not appropriate for suggestions (e.g., comes from macro expansion).

r? @RalfJung

@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 12, 2026
@rustbot

Copy link
Copy Markdown
Collaborator

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

| ^^^^^^^^^ mutable reference to mutable static
|
= note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>

@RalfJungRalfJungJul 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is the suggestion we want to give here -- certainly not the only one. In many cases, using raw pointers is an equally viable solution.

View changes since the review

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

In the subdiagnostic part it also suggests user to rewrite &mut to &raw mut for using raw pointers, so I think we provide 2 solutions here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That just means we're sending mixed signals, where we're saying both "do A" and "do B".

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Understood. Then it would be appropriate to remove that suggestion for explicit referencing (&a or &mut a) cases like this.

@RalfJung

Copy link
Copy Markdown
Member

r? @estebank for the code part of this
(This is a follow-up to #151362)

@rustbotrustbot assigned estebank and unassigned RalfJungJul 12, 2026
Previously, the lint might suggested using interior mutable type even if
it the compiler already decides that the referenced type is interior
mutable, while it didn't give such suggestion when the referenced type is
considered non-interior mutable. This commit refined the logic as follow:
if the referenced type is not interior mutable, then suggests using types
with interior mutability; otherwise, suggests removing `mut` if the
reference is a *shared* reference. Note that in the latter case, compiler
might be silent if the span of the `static mut` definition is not
appropriate for suggestions (e.g., comes from macro expansion).
For cases like `&a` or `&mut a`, we only leave a subdiagnostic to suggest
user to use raw borrow operators. Using raw pointers in such cases are
also equally viable solution but it's not appropriate to suggest using
interior mutable types at the same time.
@rustbot

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@estebank

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-borsBot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 1e65341 has been approved by estebank

It is now in the queue for this repository.

@rust-borsrust-borsBot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 14, 2026
@jhprattjhpratt mentioned this pull request Jul 15, 2026
rust-borsBot pushed a commit that referenced this pull request Jul 15, 2026
Rollup of 15 pull requests
Successful merges:
- #159311 (Add 1.97.1 release notes)
- #156220 (Implement `VecDeque::truncate_to_range`)
- #158608 (Implement `#[diagnostic::opaque]` attribute to hide backtraces of macros.)
- #159168 (Fix static_mut_refs lint check logic)
- #159242 (resolve: Inherit eager invocation parents)
- #159256 (Account for async closures when pointing at lifetime in return type)
- #159310 (cleanup: upstream dropped AMX-TF32)
- #158348 (Add documentation for the `inline` attribute)
- #159181 (add rustc_no_writable to mem::forget and structs it uses)
- #159191 (Mark `PrivateItems` with `std_internals` unstable feature.)
- #159194 (rustdoc: Fix auto trait normalization env)
- #159196 (OnceCell: Improve wording in module docs)
- #159289 (Fix Zulip backport command suggestion)
- #159294 (renovate: don't update PRs in the merge queue)
- #159305 (std: clarify available_parallelism docs for Windows 11 processor groups)
@rust-bors
rust-borsBot merged commit c0a4d3f into rust-lang:mainJul 15, 2026
13 checks passed
@rustbotrustbot added this to the 1.99.0 milestone Jul 15, 2026
rust-timer added a commit that referenced this pull request Jul 15, 2026
Rollup merge of #159168 - cclfmht:fix/issue-158735, r=estebank
Fix static_mut_refs lint check logic
This PR fixes issue #158735.
Previously, the lint might suggested using interior mutable type even if it the compiler already decides that the referenced type is interior mutable, while it didn't give such suggestion when the referenced type is considered non-interior mutable. This commit refined the logic as follow: if the referenced type is not interior mutable, then suggests using types with interior mutability; otherwise, suggests removing `mut` if the reference is a *shared* reference. Note that in the latter case, compiler might be silent if the span of the `static mut` definition is not appropriate for suggestions (e.g., comes from macro expansion).
r? @RalfJung
pullBot pushed a commit to LeeeeeeM/miri that referenced this pull request Jul 16, 2026
Rollup of 15 pull requests
Successful merges:
- rust-lang/rust#159311 (Add 1.97.1 release notes)
- rust-lang/rust#156220 (Implement `VecDeque::truncate_to_range`)
- rust-lang/rust#158608 (Implement `#[diagnostic::opaque]` attribute to hide backtraces of macros.)
- rust-lang/rust#159168 (Fix static_mut_refs lint check logic)
- rust-lang/rust#159242 (resolve: Inherit eager invocation parents)
- rust-lang/rust#159256 (Account for async closures when pointing at lifetime in return type)
- rust-lang/rust#159310 (cleanup: upstream dropped AMX-TF32)
- rust-lang/rust#158348 (Add documentation for the `inline` attribute)
- rust-lang/rust#159181 (add rustc_no_writable to mem::forget and structs it uses)
- rust-lang/rust#159191 (Mark `PrivateItems` with `std_internals` unstable feature.)
- rust-lang/rust#159194 (rustdoc: Fix auto trait normalization env)
- rust-lang/rust#159196 (OnceCell: Improve wording in module docs)
- rust-lang/rust#159289 (Fix Zulip backport command suggestion)
- rust-lang/rust#159294 (renovate: don't update PRs in the merge queue)
- rust-lang/rust#159305 (std: clarify available_parallelism docs for Windows 11 processor groups)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@cclfmht@rustbot@RalfJung@estebank