Skip to content

Missing parenthesis in static_mut_refs diagnostic suggestion #131977

@CharlyCst

Description

@CharlyCst

Code

static mut TEST: usize = 0;

fn main() {
    let _ = unsafe { (&TEST) as *const usize };
}

Current output

   Compiling playground v0.0.1 (/playground)
warning: creating a shared reference to mutable static is discouraged
 --> src/main.rs:4:22
  |
4 |     let _ = unsafe { (&TEST) as *const usize };
  |                      ^^^^^^^ 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
  |
4 |     let _ = unsafe { &raw const TEST) as *const usize };
  |                      ~~~~~~~~~~

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s
     Running `target/debug/playground`

Desired output

   Compiling playground v0.0.1 (/playground)
warning: creating a shared reference to mutable static is discouraged
 --> src/main.rs:4:22
  |
4 |     let _ = unsafe { (&TEST) as *const usize };
  |                      ^^^^^^^ 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
  |
4 |     let _ = unsafe { (&raw const TEST) as *const usize };
  |                       ~~~~~~~~~~

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s
     Running `target/debug/playground`

Rationale and extra context

The suggestion removes the left parenthesis, but keeps the right parenthesis. Of course the proposed change does not compile.
Original code: (&TEST) suggestion: &raw const TEST)

Other cases

No response

Rust Version

rustc 1.84.0-nightly (da93539 2024-10-19)
binary: rustc
commit-hash: da93539
commit-date: 2024-10-19
host: aarch64-apple-darwin
release: 1.84.0-nightly
LLVM version: 19.1.1

Anything else?

Link to rust playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions