rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.51k stars 1.55k forks source link

Deny `static mut` declarations entirely #12896

Open pitaj opened 6 months ago

pitaj commented 6 months ago

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.

lolbinarycat commented 5 months ago

@rustbot claim

archief2910 commented 5 months ago

@rustbot claim

pitaj commented 5 months ago

@archief2910 see the discussion in the previous implementation pr: https://github.com/rust-lang/rust-clippy/pull/12914

Essentially, you may want to wait until we know more about the direction in rustc and std.

pitaj commented 5 months ago

You also shouldn't claim an issue that's already assigned, without first attempting to contact the assignee.

archief2910 commented 5 months ago

sorry @pitaj it was by mistake that's why i unassigned the issue