Skip to content

Commit db11cf9

Browse files
committed
Merge #364: Add lints to catch missing traits
69f44d9 Manually implement Debug for SerializedSignature (Tobin Harding) 26921a3 Add lints to catch missing traits (Tobin Harding) 35556e2 Remove useless call to format (Tobin Harding) 0ad414a Remove unneeded return statements (Tobin Harding) Pull request description: We can use the linters to help us catch type definitions that are missing 'standard' derives. 'standard' is project defined to be - Copy - Clone - Debug - PartialEq and Eq - PartialOrd and Ord - Hash (I've assumed this to be true based on the code and an open [PR](rust-bitcoin/rust-bitcoin#587) in rust-bitcoin.) While neither Rustc nor Clippy can find all of these, Rustc can warn for missing `Copy` and `Debug` implementations and these warnings can assist us find types that may need additional derives. First two patches are trivial Clippy fixes in preparation for using the linter to improve type definitions crate wide. Patch 3 adds ``` #![warn(missing_copy_implementations)] #![warn(missing_debug_implementations)] ``` and fixes newly emitted warnings. ACKs for top commit: thomaseizinger: ACK 69f44d9 apoelstra: ACK 69f44d9 Tree-SHA512: 18f2c52d207f962ef7d6749a57a35e48eb18a18fac82d4df4ff3dce549b69661cb27f66c4cae516ae5477f5b919d9197f70a5c924955605c73f8545f430c3b42
2 parents 6911734 + 69f44d9 commit db11cf9

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

src/context.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,19 @@ pub trait Signing: Context {}
6969
pub trait Verification: Context {}
7070

7171
/// Represents the set of capabilities needed for signing with a user preallocated memory.
72+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7273
pub struct SignOnlyPreallocated<'buf> {
7374
phantom: PhantomData<&'buf ()>,
7475
}
7576

7677
/// Represents the set of capabilities needed for verification with a user preallocated memory.
78+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7779
pub struct VerifyOnlyPreallocated<'buf> {
7880
phantom: PhantomData<&'buf ()>,
7981
}
8082

8183
/// Represents the set of all capabilities with a user preallocated memory.
84+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8285
pub struct AllPreallocated<'buf> {
8386
phantom: PhantomData<&'buf ()>,
8487
}
@@ -112,14 +115,17 @@ mod alloc_only {
112115

113116
/// Represents the set of capabilities needed for signing.
114117
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
118+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
115119
pub enum SignOnly {}
116120

117121
/// Represents the set of capabilities needed for verification.
118122
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
123+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
119124
pub enum VerifyOnly {}
120125

121126
/// Represents the set of all capabilities.
122127
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
128+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
123129
pub enum All {}
124130

125131
impl Signing for SignOnly {}

src/ecdsa/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,19 @@ impl fmt::Debug for Signature {
3232
impl fmt::Display for Signature {
3333
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3434
let sig = self.serialize_der();
35-
for v in sig.iter() {
35+
sig.fmt(f)
36+
}
37+
}
38+
39+
impl fmt::Debug for SerializedSignature {
40+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41+
fmt::Display::fmt(self, f)
42+
}
43+
}
44+
45+
impl fmt::Display for SerializedSignature {
46+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47+
for v in self.data.iter().take(self.len) {
3648
write!(f, "{:02x}", v)?;
3749
}
3850
Ok(())
@@ -386,7 +398,7 @@ impl<C: Signing> Secp256k1<C> {
386398
/// Requires a signing capable context.
387399
pub fn sign_ecdsa_grind_r(&self, msg: &Message, sk: &SecretKey, bytes_to_grind: usize) -> Signature {
388400
let len_check = |s : &ffi::Signature| der_length_check(s, 71 - bytes_to_grind);
389-
return self.sign_grind_with_check(msg, sk, len_check);
401+
self.sign_grind_with_check(msg, sk, len_check)
390402
}
391403

392404
/// Constructs a signature for `msg` using the secret key `sk`, RFC6979 nonce
@@ -397,7 +409,7 @@ impl<C: Signing> Secp256k1<C> {
397409
/// Requires a signing capable context.
398410
#[deprecated(since = "0.21.0", note = "Use sign_ecdsa_grind_r instead.")]
399411
pub fn sign_low_r(&self, msg: &Message, sk: &SecretKey) -> Signature {
400-
return self.sign_grind_with_check(msg, sk, compact_sig_has_zero_first_bit)
412+
self.sign_grind_with_check(msg, sk, compact_sig_has_zero_first_bit)
401413
}
402414

403415
/// Constructs a signature for `msg` using the secret key `sk`, RFC6979 nonce
@@ -407,7 +419,7 @@ impl<C: Signing> Secp256k1<C> {
407419
/// will perform two signing operations.
408420
/// Requires a signing capable context.
409421
pub fn sign_ecdsa_low_r(&self, msg: &Message, sk: &SecretKey) -> Signature {
410-
return self.sign_grind_with_check(msg, sk, compact_sig_has_zero_first_bit)
422+
self.sign_grind_with_check(msg, sk, compact_sig_has_zero_first_bit)
411423
}
412424
}
413425

src/key.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ impl Ord for PublicKey {
511511
}
512512

513513
/// Opaque data structure that holds a keypair consisting of a secret and a public key.
514-
#[derive(Clone)]
514+
// Should secrets implement Copy: https://github.com/rust-bitcoin/rust-secp256k1/issues/363
515+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
515516
pub struct KeyPair(ffi::KeyPair);
516517
impl_display_secret!(KeyPair);
517518

@@ -1084,7 +1085,7 @@ mod test {
10841085
// Zero
10851086
assert_eq!(SecretKey::from_slice(&[0; 32]), Err(InvalidSecretKey));
10861087
assert_eq!(
1087-
SecretKey::from_str(&format!("0000000000000000000000000000000000000000000000000000000000000000")),
1088+
SecretKey::from_str("0000000000000000000000000000000000000000000000000000000000000000"),
10881089
Err(InvalidSecretKey)
10891090
);
10901091
// -1

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@
132132
#![deny(non_snake_case)]
133133
#![deny(unused_mut)]
134134
#![warn(missing_docs)]
135+
#![warn(missing_copy_implementations)]
136+
#![warn(missing_debug_implementations)]
135137

136138

137139
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]

src/secret.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ macro_rules! impl_display_secret {
5757
///
5858
/// [`Display`]: fmt::Display
5959
/// [`Debug`]: fmt::Debug
60+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6061
pub struct DisplaySecret {
6162
secret: [u8; SECRET_KEY_SIZE]
6263
}

0 commit comments

Comments
 (0)