-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 on MaybeUninit buffers #11072
Comments
rust-lang/rust-clippy#11072 error: incorrect implementation of `clone` on a `Copy` type --> src/buffer/mod.rs:86:29 | 86 | fn clone(&self) -> Self { | _____________________________^ 87 | | Buffer::new() 88 | | } | |_____^ help: change this to: `{ *self }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type = note: `-D clippy::incorrect-clone-impl-on-copy-type` implied by `-D clippy::all`
rust-lang/rust-clippy#11072 error: incorrect implementation of `clone` on a `Copy` type --> src/buffer.rs:64:29 | 64 | fn clone(&self) -> Self { | _____________________________^ 65 | | Buffer::new() 66 | | } | |_____^ help: change this to: `{ *self }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type = note: `-D clippy::incorrect-clone-impl-on-copy-type` implied by `-D clippy::all`
rust-lang/rust-clippy#11072 error: incorrect implementation of `clone` on a `Copy` type --> src/lib.rs:74:29 | 74 | fn clone(&self) -> Self { | _____________________________^ 75 | | Buffer::new() 76 | | } | |_____^ help: change this to: `{ *self }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type = note: `-D clippy::incorrect-clone-impl-on-copy-type` implied by `-D clippy::all`
rust-lang/rust-clippy#11072 error: incorrect implementation of `clone` on a `Copy` type --> src/lib.rs:93:29 | 93 | fn clone(&self) -> Self { | _____________________________^ 94 | | Buffer::new() 95 | | } | |_____^ help: change this to: `{ *self }` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type = note: `-D clippy::incorrect-clone-impl-on-copy-type` implied by `-D clippy::all`
I noticed this at #10925 (comment). I believe there's no reason it can't, but an equally good option is really just allowing the lint in cases like these (like you've done so already). The lint can have "FPs" in cases where the behavior is unnecessarily complex and a potential footgun but performance is better (like, Nevertheless, a "Known issues" section for the lint's documentation would be worthwhile. |
I think for a deny-by-default lint we should avoid FPs as much as possible. Allowing a lint is a good suggestion for pedantic lints, but default lints should avoid this such cases. One of the common complaint I heard from newcomers is, that they're not sure how to allow a lint. TBH it also took me some time to learn how. 😅 |
Well I don't really think this is a FP, per se; it's just slower. I'm not sure whether Also, I believe majority of the cases where this should be allowed is definitely due to performance; which, in this case at least, We could move this to |
+1 for recategorizing to Other than that, I'll stick to an |
@dtolnay Can you say more about why this type is If you don't actually want it to be bit-copied, then it seems counter-productive. There's going to be lots of lints saying "hey, did you know you didn't need So I'm not convinced that this is really a false positive at all. |
The previous comment is convincing, so I will retract this issue. Thanks for the insight Scott! |
warning: lint `clippy::incorrect_clone_impl_on_copy_type` has been renamed to `clippy::non_canonical_clone_impl` --> src/buffer/mod.rs:86:13 | 86 | #[allow(clippy::incorrect_clone_impl_on_copy_type)] // false positive rust-lang/rust-clippy#11072 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::non_canonical_clone_impl` | = note: `#[warn(renamed_and_removed_lints)]` on by default
warning: lint `clippy::incorrect_clone_impl_on_copy_type` has been renamed to `clippy::non_canonical_clone_impl` --> src/buffer.rs:64:13 | 64 | #[allow(clippy::incorrect_clone_impl_on_copy_type)] // false positive rust-lang/rust-clippy#11072 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::non_canonical_clone_impl` | = note: `#[warn(renamed_and_removed_lints)]` on by default
warning: lint `clippy::incorrect_clone_impl_on_copy_type` has been renamed to `clippy::non_canonical_clone_impl` --> src/lib.rs:95:13 | 95 | #[allow(clippy::incorrect_clone_impl_on_copy_type)] // false positive rust-lang/rust-clippy#11072 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::non_canonical_clone_impl` | = note: `#[warn(renamed_and_removed_lints)]` on by default
warning: lint `clippy::incorrect_clone_impl_on_copy_type` has been renamed to `clippy::non_canonical_clone_impl` --> src/lib.rs:76:13 | 76 | #[allow(clippy::incorrect_clone_impl_on_copy_type)] // false positive rust-lang/rust-clippy#11072 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::non_canonical_clone_impl` | = note: `#[warn(renamed_and_removed_lints)]` on by default
Summary
In the code below, Clippy's suggested "equivalent" Clone implementation regresses performance.
Given the API of this type, there is no reason for the Clone impl to memcpy any bytes. It can return a new uninit buffer, for free. The original clone() call compiles to 0 machine instructions. Clippy's suggested clone() implementation compiles to multiple instructions of redundant copying that cannot be optimized out.
Could Clippy identify the case of a type whose contents consist solely of MaybeUninit, and avoid triggering incorrect_clone_impl_on_copy_type in that case?
Lint Name
incorrect_clone_impl_on_copy_type
Reproducer
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: