Static rustc_lint_defs::builtin::STATIC_MUT_REFS

source ·
pub static STATIC_MUT_REFS: &Lint
Expand description

The static_mut_refs lint checks for shared or mutable references of mutable static inside unsafe blocks and unsafe functions.

§Example

fn main() {
    static mut X: i32 = 23;
    static mut Y: i32 = 24;

    unsafe {
        let y = &X;
        let ref x = X;
        let (x, y) = (&X, &Y);
        foo(&X);
    }
}

unsafe fn _foo() {
    static mut X: i32 = 23;
    static mut Y: i32 = 24;

    let y = &X;
    let ref x = X;
    let (x, y) = (&X, &Y);
    foo(&X);
}

fn foo<'a>(_x: &'a i32) {}

{{produces}}

§Explanation

Shared or mutable references of mutable static are almost always a mistake and can lead to undefined behavior and various other problems in your code.

This lint is “warn” by default on editions up to 2021, in 2024 there is a hard error instead.