Skip to content

declare_interior_mutable_const fires for static pointer types #13233

@Qix-

Description

@Qix-

Summary

Unless I'm not understanding something here, declare_interior_mutable_const seems to be firing a false-positive for pointers to inner mutable types. I can understand why constant inner-mutables make no sense, but pointers to static inner mutables are a different thing - the pointer is the constant part, not the object itself, and in certain (all?) contexts, the pointer will not change.

Lint Name

declare_interior_mutable_const

Reproducer

I tried this code:

#![feature(const_refs_to_static)]
#![allow(dead_code)]

use std::mem::MaybeUninit;
use std::sync::Mutex;

static SOME_THING: MaybeUninit<Mutex<usize>> = MaybeUninit::uninit();
const SOME_THING_PTR: *const Mutex<usize> = SOME_THING.as_ptr();

I saw this happen:

warning: a `const` item should not be interior mutable
 --> src/lib.rs:8:1
  |
8 | const SOME_THING_PTR: *const Mutex<usize> = SOME_THING.as_ptr();
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
  = note: `#[warn(clippy::declare_interior_mutable_const)]` on by default

I expected to see this happen:

No warning.

Playground link:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c920c5c7250d8e8f02d481675415664b

Version

rustc 1.81.0-nightly (fcc325f1b 2024-07-17)
binary: rustc
commit-hash: fcc325f1bc477975e2ce5ba534fe4c77ff8a8536
commit-date: 2024-07-17
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions