rust-lang/rust-clippy

Deny `static mut` declarations entirely

Open

#12.896 aperta il 6 giu 2024

Vedi su GitHub
 (13 commenti) (3 reazioni) (1 assegnatario)Rust (1391 fork)batch import
A-lintgood first issue

Metriche repository

Star
 (10.406 star)
Metriche merge PR
 (Merge medio 18g 15h) (115 PR mergiate in 30 g)

Descrizione

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.

Guida contributor