rust-lang / rust-lang/rust-clippy

`deref_addrof` conflicts with the suggestion of the rustc `static_mut_refs` lint

Open
#13,783 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

Since 1.83, the compiler warns when creating a shared reference to a mutable static:

static mut FOO: Option<String> = None;
let is_some = unsafe { &FOO }.is_some();
print!("{is_some:?}");

Gives this warning:

warning: creating a shared reference to mutable static is discouraged
 --> src/main.rs:2:28
  |
2 |     let is_some = unsafe { &FOO }.is_some();
  |                            ^^^^ shared reference to mutable static
  |
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
  = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives
  = note: `#[warn(static_mut_refs)]` on by default
help: use `&raw const` instead to create a raw pointer
  |
2 |     let is_some = unsafe { &raw const FOO }.is_some();

Following the suggestion, we can write this:

static mut FOO: Option<String> = None;
let value = unsafe { &*&raw const FOO }.is_some();
print!("{value:?}");

But then clippy says this:

warning: immediately dereferencing a reference
 --> src/main.rs:2:27
  |
2 |     let value = unsafe { &*&raw const FOO }.is_some();
  |                           ^^^^^^^^^^^^^^^ help: try: `FOO`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#deref_addrof
  = note: `#[warn(clippy::deref_addrof)]` on by default

I get why clippy says that, but I don't really see a clean solution to this with the current lints, apart from simply disabling one of them.

A different way to look at it: the suggested fix from clippy triggers the static_mut_refs lint.

Version
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
Additional Labels

@rustbot label D-confusing

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the interaction between the static_mut_refs compiler lint and Clippy's deref_addrof lint using the Rust 1.83 examples in the issue. Read the behavior and diagnostics for both lints, then determine a resolution that avoids one lint recommending code rejected by the other; done means their suggestions no longer conflict.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.