Skip to content
New issue

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

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

Already on GitHub? # to your account

declare_interior_mutable_const fires for static pointer types #13233

Closed
Qix- opened this issue Aug 8, 2024 · 0 comments · Fixed by #13290 · May be fixed by #13207
Closed

declare_interior_mutable_const fires for static pointer types #13233

Qix- opened this issue Aug 8, 2024 · 0 comments · Fixed by #13290 · May be fixed by #13207
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Qix-
Copy link

Qix- commented Aug 8, 2024

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

@Qix- Qix- added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 8, 2024
@Alexendoo Alexendoo linked a pull request Aug 8, 2024 that will close this issue
@bors bors closed this as completed in e5a1ef0 Aug 20, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
1 participant