Skip to content

Deny static mut declarations entirely #12896

Open
@pitaj

Description

@pitaj

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.

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions