Skip to content

Add interior-mutability suggestion to static_mut_refs#151362

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
JohnTitor:interior-mutability-sugg
May 18, 2026
Merged

Add interior-mutability suggestion to static_mut_refs#151362
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
JohnTitor:interior-mutability-sugg

Conversation

@JohnTitor
Copy link
Copy Markdown
Member

@JohnTitor JohnTitor commented Jan 19, 2026

View all comments

Closes #151131
r? @estebank

I've skipped to expand catching below code as a mutable reference shouldn't be involved (maybe a new lint would be needed?):

static mut COUNTER: u64 = 0;
fn main() {
    unsafe { COUNTER = 1 };
}

@rustbot rustbot 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 Jan 19, 2026
@rust-log-analyzer

This comment has been minimized.

@JohnTitor JohnTitor force-pushed the interior-mutability-sugg branch from 41e2b83 to b9914a8 Compare January 19, 2026 09:56
@rust-bors

This comment has been minimized.

@JohnTitor JohnTitor force-pushed the interior-mutability-sugg branch from b9914a8 to 7c76d7e Compare February 8, 2026 07:54
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

Comment thread tests/ui/issues/issue-39367.stderr Outdated
Comment on lines +14 to +20
= 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>
= note: `#[warn(static_mut_refs)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable
|
LL - static mut ONCE: Once = Once::new();
LL + static ONCE: Once = Once::new();
|
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.

Should the note be presented given that we have the suggestion? We're telling people "use internal mutability type" and "you are already using internal mutability type". I think we can just make the bool false if the suggestion is Some.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Absolutely! Tweaked in 647dead (this PR)

Comment on lines +241 to +246
let source_map = cx.sess().source_map();
let snippet = source_map.span_to_snippet(header_span).ok()?;

let (_static_start, static_end) = find_word(&snippet, "static", 0)?;
let (mut_start, mut_end) = find_word(&snippet, "mut", static_end)?;
let mut_end = extend_trailing_space(&snippet, mut_end);
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.

Is there no way of attaining this from the HIR instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AFAIK we cannot get precise mut spans on HIR and we would still require a string trick like this. Mutability itself doesn't contain spans currently, e.g. https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/struct.StaticItem.html.

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.

I'm still unhappy with having to build spans from text, but let's land the improvement. Could you add a // FIXME to these functions mentioning something along the way of "we want to replace this with operating on the HIR in some way"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added FIXME in ee8949e

Comment on lines +255 to +284
fn find_word(snippet: &str, word: &str, start: usize) -> Option<(usize, usize)> {
let bytes = snippet.as_bytes();
let word_bytes = word.as_bytes();
let mut search = start;
while search <= snippet.len() {
let found = snippet[search..].find(word)?;
let idx = search + found;
let end = idx + word_bytes.len();
let before_ok = idx == 0 || !is_ident_char(bytes[idx - 1]);
let after_ok = end >= bytes.len() || !is_ident_char(bytes[end]);
if before_ok && after_ok {
return Some((idx, end));
}
search = end;
}
None
}

fn is_ident_char(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || byte == b'_'
}

fn extend_trailing_space(snippet: &str, mut end: usize) -> usize {
if let Some(ch) = snippet[end..].chars().next()
&& (ch == ' ' || ch == '\t')
{
end += ch.len_utf8();
}
end
}
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.

I would prefer for us not to have to do all this :-/

But that'll only be possible if we can take the info from the HIR.

@JohnTitor JohnTitor force-pushed the interior-mutability-sugg branch from 7c76d7e to 647dead Compare March 6, 2026 12:44
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@apiraino
Copy link
Copy Markdown
Contributor

@JohnTitor Seem there are a few comments pending, am I reading correctly? I'll switch the review flag to you to incorporate changes. Feel free to request a review with @rustbot ready, thanks!

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 30, 2026
@JohnTitor
Copy link
Copy Markdown
Member Author

@apiraino No, I areadly addressed them, waiting for another review.
@rustbot review

@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 (such as code changes or more information) from the author. labels Apr 30, 2026
Copy link
Copy Markdown
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

r=me after addressing the nitpciks.

View changes since this review

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.

Can we add another test for the case where the structured suggestion wouldn't trigger but the new note would?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in 33d92f7.

Comment on lines +241 to +246
let source_map = cx.sess().source_map();
let snippet = source_map.span_to_snippet(header_span).ok()?;

let (_static_start, static_end) = find_word(&snippet, "static", 0)?;
let (mut_start, mut_end) = find_word(&snippet, "mut", static_end)?;
let mut_end = extend_trailing_space(&snippet, mut_end);
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.

I'm still unhappy with having to build spans from text, but let's land the improvement. Could you add a // FIXME to these functions mentioning something along the way of "we want to replace this with operating on the HIR in some way"?

@estebank estebank added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 8, 2026
@JohnTitor JohnTitor force-pushed the interior-mutability-sugg branch from 647dead to ee8949e Compare May 17, 2026 06:03
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 17, 2026

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.

@JohnTitor
Copy link
Copy Markdown
Member Author

Thanks for the review!
@bors r=estebank

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 17, 2026

📌 Commit ee8949e has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 17, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 17, 2026
…, r=estebank

Add interior-mutability suggestion to `static_mut_refs`

Closes rust-lang#151131
r? @estebank

I've skipped to expand catching below code as a mutable _reference_ shouldn't be involved (maybe a new lint would be needed?):
```rs
static mut COUNTER: u64 = 0;
fn main() {
    unsafe { COUNTER = 1 };
}
```
rust-bors Bot pushed a commit that referenced this pull request May 17, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #151742 (Remove redundant information in `rustc_abi::Variants`)
 - #151362 (Add interior-mutability suggestion to `static_mut_refs`)
 - #156121 (compiler: suggest `.collect()` when `String` is expected and `Iterator` is found)
 - #156208 (Emit retags in codegen to support BorrowSanitizer (part 1))
 - #156596 (Split `LintExpectationId`s)
 - #156607 (ci: Update FreeBSD version to FreeBSD 14)
 - #156376 (suggest hex escapes for C-style escapes)
 - #156577 (Test EII UI tests with prefer-dynamic)
 - #156585 (explicit tail calls: ignore some tests on unsupported LLVM targets)
 - #156598 (Avoid rustfix suggestions for macro-expanded unused imports)
 - #156616 (rustdoc: add test case for `-Drustdoc::` and `--cap-lints`)
 - #156633 (Add regression test for issue #41261)
 - #156635 (rename unexpected_try_recover function)
 - #156636 (minor `rustc_mir_transform` cleanup)
rust-bors Bot pushed a commit that referenced this pull request May 17, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #151742 (Remove redundant information in `rustc_abi::Variants`)
 - #151362 (Add interior-mutability suggestion to `static_mut_refs`)
 - #156121 (compiler: suggest `.collect()` when `String` is expected and `Iterator` is found)
 - #156208 (Emit retags in codegen to support BorrowSanitizer (part 1))
 - #156596 (Split `LintExpectationId`s)
 - #156607 (ci: Update FreeBSD version to FreeBSD 14)
 - #156376 (suggest hex escapes for C-style escapes)
 - #156577 (Test EII UI tests with prefer-dynamic)
 - #156585 (explicit tail calls: ignore some tests on unsupported LLVM targets)
 - #156598 (Avoid rustfix suggestions for macro-expanded unused imports)
 - #156616 (rustdoc: add test case for `-Drustdoc::` and `--cap-lints`)
 - #156633 (Add regression test for issue #41261)
 - #156635 (rename unexpected_try_recover function)
 - #156636 (minor `rustc_mir_transform` cleanup)
@rust-bors rust-bors Bot merged commit 9de8fa9 into rust-lang:main May 18, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone May 18, 2026
rust-timer added a commit that referenced this pull request May 18, 2026
Rollup merge of #151362 - JohnTitor:interior-mutability-sugg, r=estebank

Add interior-mutability suggestion to `static_mut_refs`

Closes #151131
r? @estebank

I've skipped to expand catching below code as a mutable _reference_ shouldn't be involved (maybe a new lint would be needed?):
```rs
static mut COUNTER: u64 = 0;
fn main() {
    unsafe { COUNTER = 1 };
}
```
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request May 18, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#151742 (Remove redundant information in `rustc_abi::Variants`)
 - rust-lang/rust#151362 (Add interior-mutability suggestion to `static_mut_refs`)
 - rust-lang/rust#156121 (compiler: suggest `.collect()` when `String` is expected and `Iterator` is found)
 - rust-lang/rust#156208 (Emit retags in codegen to support BorrowSanitizer (part 1))
 - rust-lang/rust#156596 (Split `LintExpectationId`s)
 - rust-lang/rust#156607 (ci: Update FreeBSD version to FreeBSD 14)
 - rust-lang/rust#156376 (suggest hex escapes for C-style escapes)
 - rust-lang/rust#156577 (Test EII UI tests with prefer-dynamic)
 - rust-lang/rust#156585 (explicit tail calls: ignore some tests on unsupported LLVM targets)
 - rust-lang/rust#156598 (Avoid rustfix suggestions for macro-expanded unused imports)
 - rust-lang/rust#156616 (rustdoc: add test case for `-Drustdoc::` and `--cap-lints`)
 - rust-lang/rust#156633 (Add regression test for issue rust-lang/rust#41261)
 - rust-lang/rust#156635 (rename unexpected_try_recover function)
 - rust-lang/rust#156636 (minor `rustc_mir_transform` cleanup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant 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.

Provide suggestion when creating static mut of type with internal mutability

5 participants