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

deref_addrof conflicts with the suggestion of the rustc static_mut_refs lint #13783

Open
de-vri-es opened this issue Dec 4, 2024 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@de-vri-es
Copy link
Contributor

de-vri-es commented Dec 4, 2024

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

@rustbot
Copy link
Collaborator

rustbot commented Dec 4, 2024

Error: Label D-confusing can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@StackOverflowExcept1on
Copy link
Contributor

StackOverflowExcept1on commented Dec 14, 2024

@rustbot label +C-bug +I-false-negative

@rustbot rustbot added I-false-negative Issue: The lint should have been triggered on code, but wasn't C-bug Category: Clippy is not doing the correct thing labels Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

3 participants