Skip to content

Commit

Permalink
Fix wrong usage of Copy in Clone when bounds are present
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 4, 2023
1 parent d0dbebd commit 0172c99
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- Use the `Copy` implementation for `Clone` only if no bounds are present.

## [1.2.5] - 2023-09-03

### Changed
Expand Down
8 changes: 6 additions & 2 deletions src/test/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ fn check_trait_bounds() -> Result<()> {
{
#[inline]
fn clone(&self) -> Self {
*self
match self {
Test(ref __field_0, ref __field_1) => Test(::core::clone::Clone::clone(__field_0), ::core::clone::Clone::clone(__field_1)),
}
}
}

Expand Down Expand Up @@ -277,7 +279,9 @@ fn check_multiple_trait_bounds() -> Result<()> {
{
#[inline]
fn clone(&self) -> Self {
*self
match self {
Test(ref __field_0, ref __field_1) => Test(::core::clone::Clone::clone(__field_0), ::core::clone::Clone::clone(__field_1)),
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/trait_/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ impl TraitImpl for Clone {

fn build_signature(
&self,
_any_bound: bool,
any_bound: bool,
item: &Item,
_generics: &SplitGenerics<'_>,
traits: &[DeriveTrait],
_trait_: &DeriveTrait,
body: &TokenStream,
) -> TokenStream {
// Special implementation for items also implementing `Copy`.
if traits.iter().any(|trait_| trait_ == Trait::Copy) {
if !any_bound && traits.iter().any(|trait_| trait_ == Trait::Copy) {
return quote! {
#[inline]
fn clone(&self) -> Self { *self }
Expand Down Expand Up @@ -85,12 +85,12 @@ impl TraitImpl for Clone {

fn build_body(
&self,
_any_bound: bool,
any_bound: bool,
traits: &[DeriveTrait],
trait_: &DeriveTrait,
data: &Data,
) -> TokenStream {
if traits.iter().any(|trait_| trait_ == Trait::Copy) {
if !any_bound && traits.iter().any(|trait_| trait_ == Trait::Copy) {
return TokenStream::new();
}

Expand Down

0 comments on commit 0172c99

Please sign in to comment.