Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dtolnay opened this issue Jul 3, 2023 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@dtolnay
Copy link
Member

dtolnay commented Jul 3, 2023

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

@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 3, 2023
@Centri3
Copy link
Member

Centri3 commented Jul 3, 2023

Does #[phantom] not add #[automatically_derived]? That should prevent it from firing.

I think checking if it's from a proc macro could be worthwhile, though, when that's not done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants