Skip to content

trait-impl-incorrect-safety false-positive with unsafe fields #19477

Open
@TimNN

Description

@TimNN

This requires the unstable #![feature(unsafe_fields)] (rust-lang/rust#132922). (So feel free to close if this should wait until the feature is closer to stabilization).

Implementing Copy for a struct with an unsafe field requires an unsafe impl as of rust-lang/rust#134008.

However, rust-analyzer reports an "unsafe impl for safe trait (trait-impl-incorrect-safety)" error for such an impl.

rust-analyzer version: rust-analyzer version: 0.4.2359-standalone (2e1ff255ae 2025-03-28)

rustc version: rustc 1.87.0-nightly (3f5502370 2025-03-27)

editor or extension: VSCode

code snippet to reproduce:

#![feature(unsafe_fields)]
#![allow(incomplete_features)]

#[repr(transparent)]
pub struct Alpha { unsafe a: u8 }

impl Clone for Alpha {
    fn clone(&self) -> Self { *self }
}

unsafe impl Copy for Alpha {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions