`static_mut_refs` suggests invalid code in static context
Open
Nobody has claimed this yet.
A-diagnostics
C-bug
D-invalid-suggestion
L-static_mut_refs
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the diagnostic with the src/lib.rs snippet and the reported nightly version. Inspect the static_mut_refs lint's suggestion generation and its expected reference type. Done means the compiler no longer suggests a replacement that fails with E0308, with regression coverage for this case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100