Skip to content

#[derive(PartialEq)] on a packed struct suggests nonsensical clone #153126

@theemathas

Description

@theemathas

Code

#[repr(packed, C)]
#[derive(PartialEq)]
struct Thing(u8, String);

Current output

error[E0507]: cannot move out of a shared reference
 --> src/lib.rs:3:18
  |
2 | #[derive(PartialEq)]
  |          --------- in this derive macro expansion
3 | struct Thing(u8, String);
  |                  ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
  |
  = note: `#[derive(PartialEq)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
help: consider cloning the value if the performance cost is acceptable
  |
3 | struct Thing(u8, String.clone());
  |                        ++++++++

For more information about this error, try `rustc --explain E0507`.

Desired output

Don't suggest the clone

Rationale and extra context

No response

Other cases

Rust Version

Reproducible on the playground with version 1.95.0-nightly (2026-02-24 859951e3c7c9d0322c39)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-repr-packedArea: the naughtiest reprD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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