rust-lang / rust-lang/rust

static_mut_refs lint suggests to "use a type that relies on interior mutability instead" but the type is already interior mutable

Open
#158,735 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Code
use std::sync::Mutex;

macro_rules! declare_mutex {
    () => {
        static mut MACRO_MUTEX: Mutex<bool> = Mutex::new(false);
    };
}

declare_mutex!();

fn main() {
    let _lock = unsafe { MACRO_MUTEX.lock().unwrap() };
    //~^ ERROR creating a shared reference to mutable static [static_mut_refs]
}
Current output
error: creating a shared reference to mutable static
  --> src/main.rs:12:26
   |
12 |     let _lock = unsafe { MACRO_MUTEX.lock().unwrap() };
   |                          ^^^^^^^^^^^^^^^^^^ shared reference to mutable static
   |
   = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared 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>
   = note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
   = note: `#[deny(static_mut_refs)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
Desired output
I am not sure, but it should not suggest something that the code already does.
Rationale and extra context

Mutex<bool> is a type with interior mutability. It makes no sense to ask users to use a type with interior mutability instead of this one.

This got introduced in https://github.com/rust-lang/rust/pull/151362.

Other cases

Rust Version
1.98.0-nightly
(2026-07-02 c397dae808f70caebab1)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs and run it with the static_mut_refs lint enabled on the reported nightly version. Read the diagnostic changes introduced by pull request 151362 and trace the lint's help message for Mutex. Done means the diagnostic no longer recommends interior mutability when the referenced type already provides it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.