Skip to content

Add interior-mutability suggestion to static_mut_refs#151362

Open
JohnTitor wants to merge 1 commit intorust-lang:mainfrom
JohnTitor:interior-mutability-sugg
Open

Add interior-mutability suggestion to static_mut_refs#151362
JohnTitor wants to merge 1 commit intorust-lang:mainfrom
JohnTitor:interior-mutability-sugg

Conversation

@JohnTitor
Copy link
Member

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
Copy link
Collaborator

rustbot commented Feb 8, 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.

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 26, 2026

☔ The latest upstream changes (presumably #153124) made this pull request unmergeable. Please resolve the merge conflicts.

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

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
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?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide suggestion when creating static mut of type with internal mutability

4 participants