rust-lang / rust-lang/rust-clippy

incorrect_clone_impl_on_copy_type false positive when macro-generated code doesn't know whether the type is Copy

Open
#11,080 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Consider this code using https://github.com/dtolnay/ghost:

#[phantom]
#[derive(Clone)]
pub struct MyPhantom<T: ?Sized>;

This expands to a pub type MyPhantom and impl Clone for MyPhantom.

That impl Clone needs to emit the same code regardless of whether the caller of this macro has handwritten impl<T> Copy for MyPhantom<T> {} somewhere else in this crate. The attribute macro can't possibly know whether they have.

So the macro can't produce fn clone(&self) -> Self { *self } because the type may or may not be Copy.

But if it produces fn clone(&self) -> Self { /* other correct implementation */ } and the type happens to be Copy, then Clippy's incorrect_clone_impl_on_copy_type lint will be sad about this.

Would this situation be worth documenting as a "known issue" for the lint?

Lint Name

incorrect_clone_impl_on_copy_type

Reproducer

No response

Version
rustc 1.72.0-nightly (839e9a6e1 2023-07-02)
binary: rustc
commit-hash: 839e9a6e1210934fd24b15548b811a97c77138fc
commit-date: 2023-07-02
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5
Additional Labels

No response

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 with the incorrect_clone_impl_on_copy_type lint documentation and its existing tests or entry point. Confirm how macro-generated Clone implementations trigger the lint when Copy status is unknown, then document this limitation as a known issue if it is not already covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.