Skip to content

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

Description

@RalfJung

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

Other cases

Rust Version

1.98.0-nightly
(2026-07-02 c397dae808f70caebab1)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions