rust-lang/rust-clippy

Deny `static mut` declarations entirely

Open

#12,896 opened on Jun 6, 2024

View on GitHub
 (13 comments) (3 reactions) (1 assignee)Rust (10,406 stars) (1,391 forks)batch import
A-lintgood first issue

Description

What it does

The static_mut_refs rustc lint checks for shared or mutable references of mutable statics. This catches most incorrect uses, but doesn't necessarily accomplish the goal of moving the ecosystem away from static mut entirely.

The static_mut clippy lint will check for any declarations of mutable statics, and recommend using an immutable static with a type with interior mutability instead.

Advantage

Move the ecosystem away from static mut. May even catch some unsoundness issues.

Drawbacks

Churn

Example

static mut NUM: usize = 0;
static mut FOO: Thing = Thing::new();

Could be written as:

static NUM: AtomicUsize = AtomicUsize::new(0);
static FOO: Mutex<Thing> = Mutex::new(Thing::new());
// Or RwLock, LazyLock, SyncUnsafeCell, etc

It would be good to point people to the edition guide explanation and the higher level sync objects docs.

Contributor guide