Skip to content

Commit 5f59820

Browse files
committed
Merge #465: Add must_use for mut self key manipulation methods
56f1843 Add must_use for mut self key manipulation methods (Tobin C. Harding) 5b86e38 Put compiler attributes below rustdocs (Tobin C. Harding) Pull request description: We recently added a bunch of key tweaking methods that take `mut self` and return the tweaked/negated keys. These functions are pure and as such the returned result is expected to be used. To help downstream users use the API correctly add `must_use` attributes with a descriptive error string for each of the methods that takes `mut self`. Patch 1 is preparatory cleanup. ACKs for top commit: apoelstra: ACK 56f1843 Tree-SHA512: 95ee63d5d0a34a9915551471d2f71de1963875eda04bf4217544076be0ed2836dcdee1875432dba5e02678556af86d7487e39daac6e928083807661430ddbcd6
2 parents 16b7402 + 56f1843 commit 5f59820

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

secp256k1-sys/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ macro_rules! impl_array_newtype {
2020
impl Copy for $thing {}
2121

2222
impl $thing {
23-
#[inline]
2423
/// Converts the object to a raw pointer for FFI interfacing
24+
#[inline]
2525
pub fn as_ptr(&self) -> *const $ty {
2626
let &$thing(ref dat) = self;
2727
dat.as_ptr()
2828
}
2929

30-
#[inline]
3130
/// Converts the object to a mutable raw pointer for FFI interfacing
31+
#[inline]
3232
pub fn as_mut_ptr(&mut self) -> *mut $ty {
3333
let &mut $thing(ref mut dat) = self;
3434
dat.as_mut_ptr()
3535
}
3636

37-
#[inline]
3837
/// Returns the length of the object as an array
38+
#[inline]
3939
pub fn len(&self) -> usize { $len }
4040

41-
#[inline]
4241
/// Returns whether the object as an array is empty
42+
#[inline]
4343
pub fn is_empty(&self) -> bool { false }
4444
}
4545

src/key.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ impl SecretKey {
243243

244244
/// Negates the secret key.
245245
#[inline]
246+
#[must_use = "you forgot to use the negated secret key"]
246247
pub fn negate(mut self) -> SecretKey {
247248
unsafe {
248249
let res = ffi::secp256k1_ec_seckey_negate(
@@ -272,6 +273,7 @@ impl SecretKey {
272273
///
273274
/// Returns an error if the resulting key would be invalid.
274275
#[inline]
276+
#[must_use = "you forgot to use the tweaked secret key"]
275277
pub fn add_tweak(mut self, tweak: &Scalar) -> Result<SecretKey, Error> {
276278
unsafe {
277279
if ffi::secp256k1_ec_seckey_tweak_add(
@@ -302,6 +304,7 @@ impl SecretKey {
302304
///
303305
/// Returns an error if the resulting key would be invalid.
304306
#[inline]
307+
#[must_use = "you forgot to use the tweaked secret key"]
305308
pub fn mul_tweak(mut self, tweak: &Scalar) -> Result<SecretKey, Error> {
306309
unsafe {
307310
if ffi::secp256k1_ec_seckey_tweak_mul(
@@ -536,6 +539,7 @@ impl PublicKey {
536539

537540
/// Negates the public key.
538541
#[inline]
542+
#[must_use = "you forgot to use the negated public key"]
539543
pub fn negate<C: Verification>(mut self, secp: &Secp256k1<C>) -> PublicKey {
540544
unsafe {
541545
let res = ffi::secp256k1_ec_pubkey_negate(secp.ctx, &mut self.0);
@@ -566,6 +570,7 @@ impl PublicKey {
566570
///
567571
/// Returns an error if the resulting key would be invalid.
568572
#[inline]
573+
#[must_use = "you forgot to use the tweaked public key"]
569574
pub fn add_exp_tweak<C: Verification>(
570575
mut self,
571576
secp: &Secp256k1<C>,
@@ -602,6 +607,7 @@ impl PublicKey {
602607
///
603608
/// Returns an error if the resulting key would be invalid.
604609
#[inline]
610+
#[must_use = "you forgot to use the tweaked public key"]
605611
pub fn mul_tweak<C: Verification>(
606612
mut self,
607613
secp: &Secp256k1<C>,
@@ -971,6 +977,7 @@ impl KeyPair {
971977
/// ```
972978
// TODO: Add checked implementation
973979
#[inline]
980+
#[must_use = "you forgot to use the tweaked key pair"]
974981
pub fn add_xonly_tweak<C: Verification>(
975982
mut self,
976983
secp: &Secp256k1<C>,
@@ -1270,6 +1277,7 @@ impl XOnlyPublicKey {
12701277
/// let tweaked = xonly.add_tweak(&secp, &tweak).expect("Improbable to fail with a randomly generated tweak");
12711278
/// # }
12721279
/// ```
1280+
#[must_use = "you forgot to use the tweaked xonly pubkey"]
12731281
pub fn add_tweak<V: Verification>(
12741282
mut self,
12751283
secp: &Secp256k1<V>,

0 commit comments

Comments
 (0)