Skip to content

static_mut_refs suggests invalid code in static context #144496

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn static_mut_refs

this code:

pub static mut ZERO: [u8; 1] = [0];
pub static ZERO_REF: &[u8; 1] = unsafe { &ZERO };
pub static mut OPT_ZERO: Option<u8> = Some(0);

caused the following diagnostics:

    Checking _static_cross_crate v0.1.0 (/tmp/icemaker_global_tempdir.0gE5D6KlClUx/icemaker_clippyfix_tempdir.zqEni6SMBF1V/_static_cross_crate)
warning: creating a shared reference to mutable static
 --> src/lib.rs:2:42
  |
2 | pub static ZERO_REF: &[u8; 1] = unsafe { &ZERO };
  |                                          ^^^^^ shared reference to mutable static
  |
  = note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
  = 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
  = note: requested on the command line with `--force-warn static-mut-refs`
help: use `&raw const` instead to create a raw pointer
  |
2 | pub static ZERO_REF: &[u8; 1] = unsafe { &raw const ZERO };
  |                                           +++++++++

warning: `_static_cross_crate` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s

However after applying these diagnostics, the resulting code:

pub static mut ZERO: [u8; 1] = [0];
pub static ZERO_REF: &[u8; 1] = unsafe { &raw const ZERO };
pub static mut OPT_ZERO: Option<u8> = Some(0);

no longer compiled:

    Checking _static_cross_crate v0.1.0 (/tmp/icemaker_global_tempdir.0gE5D6KlClUx/icemaker_clippyfix_tempdir.zqEni6SMBF1V/_static_cross_crate)
error[E0308]: mismatched types
 --> src/lib.rs:2:42
  |
2 | pub static ZERO_REF: &[u8; 1] = unsafe { &raw const ZERO };
  |                                          ^^^^^^^^^^^^^^^ expected `&[u8; 1]`, found `*const [u8; 1]`
  |
  = note: expected reference `&'static [u8; 1]`
           found raw pointer `*const [u8; 1]`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `_static_cross_crate` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_static_cross_crate` (lib) due to 1 previous error

Version:

rustc 1.90.0-nightly (f32b23204 2025-07-26)
binary: rustc
commit-hash: f32b23204a0efe2fe8383ed4be1a30b56c1bbf94
commit-date: 2025-07-26
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-static_mut_refsLint: static_mut_refsT-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