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

Apply clippy::clone_on_copy throughout stacks core #5764

Closed
wants to merge 2 commits into from

Conversation

jferrant
Copy link
Collaborator

No description provided.

@jferrant jferrant added the lint Related to linting/clippy/cargo warns label Jan 29, 2025
@jferrant jferrant requested a review from a team as a code owner January 29, 2025 02:02
@kantai
Copy link
Member

kantai commented Jan 29, 2025

I think I would prefer deleting the Copy implementations for many of these types.

@jbencin
Copy link
Contributor

jbencin commented Jan 29, 2025

@kantai I assume you're referring mostly to 32 byte hash/key types, right? Do we want to establish an upper size limit for what types are allowed to implement Copy? Maybe anything bigger than u128 should not implement Copy to encourage passing by reference instead?

@kantai
Copy link
Member

kantai commented Jan 29, 2025

@kantai I assume you're referring mostly to 32 byte hash/key types, right? Do we want to establish an upper size limit for what types are allowed to implement Copy? Maybe anything bigger than u128 should not implement Copy to encourage passing by reference instead?

Yeah -- I think something along this kind of patch at least:

diff --git a/stacks-common/src/util/macros.rs b/stacks-common/src/util/macros.rs
index 4e332179e6..5a3a873410 100644
--- a/stacks-common/src/util/macros.rs
+++ b/stacks-common/src/util/macros.rs
@@ -414,8 +414,6 @@ macro_rules! impl_array_newtype {
             }
         }
 
-        impl Copy for $thing {}
-
         impl ::std::hash::Hash for $thing {
             #[inline]
             fn hash<H>(&self, state: &mut H)
diff --git a/stacks-common/src/util/secp256k1.rs b/stacks-common/src/util/secp256k1.rs
index e569a8ba0d..ca60a622fe 100644
--- a/stacks-common/src/util/secp256k1.rs
+++ b/stacks-common/src/util/secp256k1.rs
@@ -35,7 +35,7 @@ use crate::util::hash::{hex_bytes, to_hex};
 // per-thread Secp256k1 context
 thread_local!(static _secp256k1: Secp256k1<secp256k1::All> = Secp256k1::new());
 
-#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
 pub struct Secp256k1PublicKey {
     // serde is broken for secp256k1, so do it ourselves
     #[serde(
@@ -46,7 +46,7 @@ pub struct Secp256k1PublicKey {
     compressed: bool,
 }
 
-#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
 pub struct Secp256k1PrivateKey {
     // serde is broken for secp256k1, so do it ourselves
     #[serde(

I am little hesitant to establish an upper size limit: for my part, I kind of think Copy should be avoided almost whenever possible.

@jbencin
Copy link
Contributor

jbencin commented Jan 29, 2025

I kind of think Copy should be avoided almost whenever possible

Copy is great for small types, it lets you make assignments without the extra noise of clone() or &s in your code. The standard library implements Copy for all primitive types, but also for small-to-medium sized structs/enums like Duration and SocketAddrV6. Anything that's the size of usize or less doesn't benefit from passing by reference (because usize is the size of the pointer it needs to copy), so we should at least implement Copy for those types. I think it would be reasonable to limit Copy in our code to <= 128-bit types, curious to know what others think...

@obycode
Copy link
Contributor

obycode commented Jan 31, 2025

I would agree with @kantai here. I would default to only have Copy for simple types, things that would go into a register.

@jcnelson
Copy link
Member

jcnelson commented Feb 7, 2025

From the call: Consensus is to get rid of Copy implementations, since .clone() and Copy have the same behavior for these types.

@jferrant
Copy link
Collaborator Author

jferrant commented Feb 8, 2025

Gonna close and replace.

@jferrant jferrant closed this Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint Related to linting/clippy/cargo warns
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants