Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deny static mut declarations entirely #12896

Open
pitaj opened this issue Jun 6, 2024 · 13 comments
Open

Deny static mut declarations entirely #12896

pitaj opened this issue Jun 6, 2024 · 13 comments
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@pitaj
Copy link
Contributor

pitaj commented Jun 6, 2024

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.

@pitaj pitaj added good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints labels Jun 6, 2024
@lolbinarycat
Copy link

@rustbot claim

lolbinarycat added a commit to lolbinarycat/rust-clippy that referenced this issue Jun 9, 2024
lolbinarycat added a commit to lolbinarycat/rust-clippy that referenced this issue Jun 9, 2024
lolbinarycat added a commit to lolbinarycat/rust-clippy that referenced this issue Jun 12, 2024
lolbinarycat added a commit to lolbinarycat/rust-clippy that referenced this issue Jun 12, 2024
@archief2910
Copy link

@rustbot claim

@rustbot rustbot assigned archief2910 and unassigned lolbinarycat Jul 1, 2024
@archief2910 archief2910 removed their assignment Jul 1, 2024
@pitaj

This comment was marked as outdated.

@pitaj

This comment was marked as resolved.

@archief2910
Copy link

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

@aaron-ang
Copy link
Contributor

@lolbinarycat are you still working on this issue?

@lolbinarycat
Copy link

@aaron-ang not really, no.

@aaron-ang
Copy link
Contributor

@pitaj can I confirm that #13770 is not related to this issue?

@pitaj
Copy link
Contributor Author

pitaj commented Jan 21, 2025

Correct, #13770 is unrelated. I was confused earlier

@aaron-ang
Copy link
Contributor

@rustbot claim

@rustbot rustbot assigned aaron-ang and unassigned lolbinarycat Jan 21, 2025
@pitaj
Copy link
Contributor Author

pitaj commented Jan 21, 2025

@aaron-ang you might want to read through the discussion on #12914 before putting in any work.

@aaron-ang
Copy link
Contributor

@aaron-ang you might want to read through the discussion on #12914 before putting in any work.

Thanks @pitaj. I'm aware that static mut semantics might change in the future.

@aaron-ang
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants