rust-lang / rust-lang/rust-analyzer

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

Open
#19,477 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

B-unstable C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

This requires the unstable #![feature(unsafe_fields)] (https://github.com/rust-lang/rust/issues/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 https://github.com/rust-lang/rust/pull/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 {}

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

Start by reproducing the reported trait-impl-incorrect-safety diagnostic with the unsafe_fields code snippet and the listed Rust nightly version. Trace how rust-analyzer classifies the unsafe impl Copy case, then verify that the valid impl no longer produces a false positive while the diagnostic still catches genuinely invalid unsafe impls.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, 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.