Skip to content

Usage of pattern on lhs of assignment results in unsafety check for raw pointer deferences being skipped #107288

Closed
@Aiden2207

Description

@Aiden2207

I tried this code:

fn main() {
    let _ = *(0 as *const ());
}

playground

I expected to see this happen: A compiler error, dereferencing pointers is unsafe, and no unsafe block is provided.
Instead, this happened:

   Compiling playground v0.0.1 (/playground)
warning: dereferencing a null pointer
 --> src/main.rs:2:13
  |
2 |     let _ = *(0 as *const ());
  |             ^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
  |
  = note: `#[warn(deref_nullptr)]` on by default

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

As far as I can tell, this code erroneously compiles for any Copy type, so long as the pattern doesn't bind anything to an identifier. Calls to unsafe functions cause a compiler error as normal.

Meta

Works on the playground when I tested with stable-1.66.1, beta-1.67.0-beta.10, and nightly-1.69.0 (2023-01-23)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions