Skip to content

Commit 2917de5

Browse files
authored
Merge pull request #261 from tcharding/more-clippy
More clippy
2 parents 1a818ea + a584643 commit 2917de5

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

src/ecdh.rs

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ impl SharedSecret {
6161
self.len
6262
}
6363

64+
/// True if the underlying data buffer is empty.
65+
pub fn is_empty(&self) -> bool {
66+
self.data.is_empty()
67+
}
68+
6469
/// Set the length of the object.
6570
pub(crate) fn set_len(&mut self, len: usize) {
6671
debug_assert!(len <= self.data.len());

src/key.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ mod test {
512512
self.0 -= 1;
513513
}
514514
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
515-
Ok(self.fill_bytes(dest))
515+
self.fill_bytes(dest);
516+
Ok(())
516517
}
517518
}
518519

@@ -761,13 +762,13 @@ mod test {
761762
let s = Secp256k1::new();
762763
let mut set = HashSet::new();
763764
const COUNT : usize = 1024;
764-
let count = (0..COUNT).map(|_| {
765+
for _ in 0..COUNT {
765766
let (_, pk) = s.generate_keypair(&mut thread_rng());
766767
let hash = hash(&pk);
767768
assert!(!set.contains(&hash));
768769
set.insert(hash);
769-
}).count();
770-
assert_eq!(count, COUNT);
770+
};
771+
assert_eq!(set.len(), COUNT);
771772
}
772773

773774
#[test]
@@ -795,7 +796,7 @@ mod test {
795796
let pk1 = PublicKey::from_slice(
796797
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
797798
).unwrap();
798-
let pk2 = pk1.clone();
799+
let pk2 = pk1;
799800
let pk3 = PublicKey::from_slice(
800801
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
801802
).unwrap();

src/lib.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,15 @@ mod tests {
809809

810810
#[test]
811811
fn test_raw_ctx() {
812+
use std::mem::ManuallyDrop;
813+
812814
let ctx_full = Secp256k1::new();
813815
let ctx_sign = Secp256k1::signing_only();
814816
let ctx_vrfy = Secp256k1::verification_only();
815817

816-
let full = unsafe {Secp256k1::from_raw_all(ctx_full.ctx)};
817-
let sign = unsafe {Secp256k1::from_raw_signining_only(ctx_sign.ctx)};
818-
let vrfy = unsafe {Secp256k1::from_raw_verification_only(ctx_vrfy.ctx)};
818+
let mut full = unsafe {Secp256k1::from_raw_all(ctx_full.ctx)};
819+
let mut sign = unsafe {Secp256k1::from_raw_signining_only(ctx_sign.ctx)};
820+
let mut vrfy = unsafe {Secp256k1::from_raw_verification_only(ctx_vrfy.ctx)};
819821

820822
let (sk, pk) = full.generate_keypair(&mut thread_rng());
821823
let msg = Message::from_slice(&[2u8; 32]).unwrap();
@@ -827,8 +829,15 @@ mod tests {
827829
assert!(vrfy.verify(&msg, &sig, &pk).is_ok());
828830
assert!(full.verify(&msg, &sig, &pk).is_ok());
829831

830-
drop(full);drop(sign);drop(vrfy);
831-
drop(ctx_full);drop(ctx_sign);drop(ctx_vrfy);
832+
unsafe {
833+
ManuallyDrop::drop(&mut full);
834+
ManuallyDrop::drop(&mut sign);
835+
ManuallyDrop::drop(&mut vrfy);
836+
837+
}
838+
drop(ctx_full);
839+
drop(ctx_sign);
840+
drop(ctx_vrfy);
832841
}
833842

834843
#[test]

src/schnorrsig.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ impl KeyPair {
185185
tweak.as_c_ptr(),
186186
);
187187

188-
return if err == 1 {
188+
if err == 1 {
189189
Ok(())
190190
} else {
191191
Err(Error::InvalidTweak)
192-
};
192+
}
193193
}
194194
}
195195
}
@@ -384,7 +384,7 @@ impl<C: Signing> Secp256k1<C> {
384384
msg: &Message,
385385
keypair: &KeyPair,
386386
nonce_data: *const ffi::types::c_void,
387-
) -> Result<Signature, Error> {
387+
) -> Signature {
388388
unsafe {
389389
let mut sig = [0u8; constants::SCHNORRSIG_SIGNATURE_SIZE];
390390
assert_eq!(
@@ -399,15 +399,15 @@ impl<C: Signing> Secp256k1<C> {
399399
)
400400
);
401401

402-
Ok(Signature(sig))
402+
Signature(sig)
403403
}
404404
}
405405

406406
/// Create a schnorr signature internally using the ThreadRng random number
407407
/// generator to generate the auxiliary random data.
408408
/// Requires compilation with "rand-std" feature.
409409
#[cfg(any(test, feature = "rand-std"))]
410-
pub fn schnorrsig_sign(&self, msg: &Message, keypair: &KeyPair) -> Result<Signature, Error> {
410+
pub fn schnorrsig_sign(&self, msg: &Message, keypair: &KeyPair) -> Signature {
411411
let mut rng = thread_rng();
412412
self.schnorrsig_sign_with_rng(msg, keypair, &mut rng)
413413
}
@@ -417,7 +417,7 @@ impl<C: Signing> Secp256k1<C> {
417417
&self,
418418
msg: &Message,
419419
keypair: &KeyPair,
420-
) -> Result<Signature, Error> {
420+
) -> Signature {
421421
self.schnorrsig_sign_helper(msg, keypair, ptr::null())
422422
}
423423

@@ -427,7 +427,7 @@ impl<C: Signing> Secp256k1<C> {
427427
msg: &Message,
428428
keypair: &KeyPair,
429429
aux_rand: &[u8; 32],
430-
) -> Result<Signature, Error> {
430+
) -> Signature {
431431
self.schnorrsig_sign_helper(
432432
msg,
433433
keypair,
@@ -444,7 +444,7 @@ impl<C: Signing> Secp256k1<C> {
444444
msg: &Message,
445445
keypair: &KeyPair,
446446
rng: &mut R,
447-
) -> Result<Signature, Error> {
447+
) -> Signature {
448448
let mut aux = [0u8; 32];
449449
rng.fill_bytes(&mut aux);
450450
self.schnorrsig_sign_helper(msg, keypair, aux.as_c_ptr() as *const ffi::types::c_void)
@@ -465,11 +465,11 @@ impl<C: Signing> Secp256k1<C> {
465465
pubkey.as_c_ptr(),
466466
);
467467

468-
return if ret == 1 {
468+
if ret == 1 {
469469
Ok(())
470470
} else {
471471
Err(Error::InvalidSignature)
472-
};
472+
}
473473
}
474474
}
475475

@@ -533,29 +533,27 @@ mod tests {
533533
let mut aux_rand = [0; 32];
534534
rng.fill_bytes(&mut aux_rand);
535535
secp.schnorrsig_sign_with_aux_rand(msg, seckey, &aux_rand)
536-
.unwrap()
537536
})
538537
}
539538

540539
#[test]
541540
fn test_schnorrsig_sign_with_rng_verify() {
542541
test_schnorrsig_sign_helper(|secp, msg, seckey, mut rng| {
543542
secp.schnorrsig_sign_with_rng(msg, seckey, &mut rng)
544-
.unwrap()
545543
})
546544
}
547545

548546
#[test]
549547
fn test_schnorrsig_sign_verify() {
550548
test_schnorrsig_sign_helper(|secp, msg, seckey, _| {
551-
secp.schnorrsig_sign(msg, seckey).unwrap()
549+
secp.schnorrsig_sign(msg, seckey)
552550
})
553551
}
554552

555553
#[test]
556554
fn test_schnorrsig_sign_no_aux_rand_verify() {
557555
test_schnorrsig_sign_helper(|secp, msg, seckey, _| {
558-
secp.schnorrsig_sign_no_aux_rand(msg, seckey).unwrap()
556+
secp.schnorrsig_sign_no_aux_rand(msg, seckey)
559557
})
560558
}
561559

@@ -575,8 +573,7 @@ mod tests {
575573
let expected_sig = Signature::from_str("6470FD1303DDA4FDA717B9837153C24A6EAB377183FC438F939E0ED2B620E9EE5077C4A8B8DCA28963D772A94F5F0DDF598E1C47C137F91933274C7C3EDADCE8").unwrap();
576574

577575
let sig = secp
578-
.schnorrsig_sign_with_aux_rand(&msg, &sk, &aux_rand)
579-
.unwrap();
576+
.schnorrsig_sign_with_aux_rand(&msg, &sk, &aux_rand);
580577

581578
assert_eq!(expected_sig, sig);
582579
}
@@ -730,8 +727,7 @@ mod tests {
730727
let keypair = KeyPair::from_seckey_slice(&s, &[2; 32]).unwrap();
731728
let aux = [3; 32];
732729
let sig = s
733-
.schnorrsig_sign_with_aux_rand(&msg, &keypair, &aux)
734-
.unwrap();
730+
.schnorrsig_sign_with_aux_rand(&msg, &keypair, &aux);
735731
static SIG_BYTES: [u8; constants::SCHNORRSIG_SIGNATURE_SIZE] = [
736732
0x14, 0xd0, 0xbf, 0x1a, 0x89, 0x53, 0x50, 0x6f, 0xb4, 0x60, 0xf5, 0x8b, 0xe1, 0x41,
737733
0xaf, 0x76, 0x7f, 0xd1, 0x12, 0x53, 0x5f, 0xb3, 0x92, 0x2e, 0xf2, 0x17, 0x30, 0x8e,

0 commit comments

Comments
 (0)