From 549be547accdb13037bf3ef60310ca30d045dce0 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 9 Dec 2024 16:50:16 +1100 Subject: [PATCH] primitives: Add must_use Enable lint `clippy::return_self_not_must_use` and add attribute `must_use` as required. Also run the linter with `clippy::must_use_candidate` enabled and manually check every warning site. While we are at it change the current `must_use` usages to have no message. We can always add a message later if needed. --- primitives/src/block.rs | 1 + primitives/src/lib.rs | 3 +++ primitives/src/opcodes.rs | 2 ++ primitives/src/script/borrowed.rs | 2 +- primitives/src/script/owned.rs | 2 +- primitives/src/witness.rs | 1 + 6 files changed, 9 insertions(+), 2 deletions(-) diff --git a/primitives/src/block.rs b/primitives/src/block.rs index 22d31c9ed..7736fcf8f 100644 --- a/primitives/src/block.rs +++ b/primitives/src/block.rs @@ -77,6 +77,7 @@ impl Block { /// Ignores block validation logic and just assumes you know what you are doing. /// /// You should only use this function if you trust the block i.e., it comes from a trusted node. + #[must_use] pub fn assume_checked(self, witness_root: Option) -> Block { Block { header: self.header, diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index b8a5204de..f3cd95a7a 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -16,6 +16,9 @@ #![warn(missing_docs)] #![warn(deprecated_in_future)] #![doc(test(attr(warn(unused))))] +// Pedantic lints that we enforce. +// #![warn(clippy::must_use_candidate)] +#![warn(clippy::return_self_not_must_use)] // Exclude lints we don't think are valuable. #![allow(clippy::needless_question_mark)] // https://github.com/rust-bitcoin/rust-bitcoin/pull/2134 #![allow(clippy::manual_range_contains)] // More readable than clippy's format. diff --git a/primitives/src/opcodes.rs b/primitives/src/opcodes.rs index 498713fe2..bd922e22c 100644 --- a/primitives/src/opcodes.rs +++ b/primitives/src/opcodes.rs @@ -351,6 +351,7 @@ pub enum ClassifyContext { impl Opcode { /// Classifies an Opcode into a broad class. #[inline] + #[must_use] pub fn classify(self, ctx: ClassifyContext) -> Class { match (self, ctx) { // 3 opcodes illegal in all contexts @@ -424,6 +425,7 @@ impl Opcode { /// /// Returns `None` if `self` is not a PUSHNUM. #[inline] + #[must_use] pub const fn decode_pushnum(self) -> Option { const START: u8 = OP_PUSHNUM_1.code; const END: u8 = OP_PUSHNUM_16.code; diff --git a/primitives/src/script/borrowed.rs b/primitives/src/script/borrowed.rs index a7ea5e2f0..e8ea04bcd 100644 --- a/primitives/src/script/borrowed.rs +++ b/primitives/src/script/borrowed.rs @@ -115,7 +115,7 @@ impl Script { pub fn is_empty(&self) -> bool { self.0.is_empty() } /// Converts a [`Box