Skip to content

Deny static mut declarations entirely #12896

Open

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.

Activity

  1. added
    good first issueThese issues are a good way to get started with Clippy
    A-lintArea: New lints
    on Jun 6, 2024
  2. lolbinarycat commented on Jun 9, 2024

    @lolbinarycat
    Contributor

    @rustbot claim

  3. added 4 commits that reference this issue on Jun 9, 2024
    f6d1a8f
    7efdadb
    0a759ec
    3dbaa0c
  4. archief2910 commented on Jul 1, 2024

    @archief2910

    @rustbot claim

  5. removed their assignment
    on Jul 1, 2024
  6. pitaj commented on Jul 1, 2024

    @pitaj
    Author
  7. pitaj commented on Jul 1, 2024

    @pitaj
    Author
  8. archief2910 commented on Jul 1, 2024

    @archief2910

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

  9. aaron-ang commented on Jan 21, 2025

    @aaron-ang
    Contributor

    @lolbinarycat are you still working on this issue?

  10. lolbinarycat commented on Jan 21, 2025

    @lolbinarycat
    Contributor

    @aaron-ang not really, no.

  11. aaron-ang commented on Jan 21, 2025

    @aaron-ang
    Contributor

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

  12. pitaj commented on Jan 21, 2025

    @pitaj
    ContributorAuthor

    Correct, #13770 is unrelated. I was confused earlier

  13. aaron-ang commented on Jan 21, 2025

    @aaron-ang
    Contributor

    @rustbot claim

  14. pitaj commented on Jan 21, 2025

    @pitaj
    ContributorAuthor

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

  15. aaron-ang commented on Jan 21, 2025

    @aaron-ang
    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.

  16. aaron-ang commented on Jan 23, 2025

    @aaron-ang
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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