Skip to content

Commit 56f1843

Browse files
committed
Add must_use for mut self key manipulation methods
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`.
1 parent 5b86e38 commit 56f1843

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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)