From fd89ddf4012c4dbfce07450b868dfdbcb0d6b2e6 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Tue, 17 Sep 2024 16:25:43 +0100 Subject: [PATCH] Remove or fix unused variables and methods in docs Examples in documentation are not linted in the same way as other code, but should still contain correctly written code. Throughout all of the crates except internals (another commit) unused variables have been prefixed with `_`, unused imports have been removed, and a warn attribute added to all of the `lib.rs` files. --- addresses/src/lib.rs | 1 + base58/src/lib.rs | 1 + hashes/src/lib.rs | 10 +++------- io/src/lib.rs | 1 + primitives/src/lib.rs | 1 + primitives/src/locktime/absolute.rs | 7 +++---- primitives/src/locktime/relative.rs | 7 +++---- units/src/lib.rs | 1 + 8 files changed, 14 insertions(+), 15 deletions(-) diff --git a/addresses/src/lib.rs b/addresses/src/lib.rs index 32b7bda62..18604ce0a 100644 --- a/addresses/src/lib.rs +++ b/addresses/src/lib.rs @@ -14,6 +14,7 @@ #![cfg_attr(all(not(test), not(feature = "std")), no_std)] // Experimental features we need. #![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![doc(test(attr(warn(unused))))] // Coding conventions. #![warn(missing_docs)] // Exclude lints we don't think are valuable. diff --git a/base58/src/lib.rs b/base58/src/lib.rs index 3677bd2d8..1c7e04a2d 100644 --- a/base58/src/lib.rs +++ b/base58/src/lib.rs @@ -12,6 +12,7 @@ #![cfg_attr(bench, feature(test))] // Coding conventions. #![warn(missing_docs)] +#![doc(test(attr(warn(unused))))] // Instead of littering the codebase for non-fuzzing and bench code just globally allow. #![cfg_attr(fuzzing, allow(dead_code, unused_imports))] #![cfg_attr(bench, allow(dead_code, unused_imports))] diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index b332b9a1f..31080d192 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -15,16 +15,14 @@ //! use bitcoin_hashes::Sha256; //! //! let bytes = [0u8; 5]; -//! let hash_of_bytes = Sha256::hash(&bytes); -//! let hash_of_string = Sha256::hash("some string".as_bytes()); +//! let _hash_of_bytes = Sha256::hash(&bytes); +//! let _hash_of_string = Sha256::hash("some string".as_bytes()); //! ``` //! //! //! Hashing content from a reader: //! //! ```rust -//! use bitcoin_hashes::Sha256; -//! //! #[cfg(std)] //! # fn main() -> std::io::Result<()> { //! let mut reader: &[u8] = b"hello"; // in real code, this could be a `File` or `TcpStream` @@ -42,9 +40,6 @@ //! Hashing content by [`std::io::Write`] on `HashEngine`: //! //! ```rust -//! use bitcoin_hashes::Sha256; -//! use std::io::Write; -//! //! #[cfg(std)] //! # fn main() -> std::io::Result<()> { //! let mut part1: &[u8] = b"hello"; @@ -68,6 +63,7 @@ #![cfg_attr(bench, feature(test))] // Coding conventions. #![warn(missing_docs)] +#![doc(test(attr(warn(unused))))] // Instead of littering the codebase for non-fuzzing and bench code just globally allow. #![cfg_attr(hashes_fuzz, allow(dead_code, unused_imports))] #![cfg_attr(bench, allow(dead_code, unused_imports))] diff --git a/io/src/lib.rs b/io/src/lib.rs index e3b7ac0cb..387fbdffb 100644 --- a/io/src/lib.rs +++ b/io/src/lib.rs @@ -13,6 +13,7 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg))] // Coding conventions. #![warn(missing_docs)] +#![doc(test(attr(warn(unused))))] // 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/lib.rs b/primitives/src/lib.rs index e6743bc1e..83149ba20 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -13,6 +13,7 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg))] // Coding conventions. #![warn(missing_docs)] +#![doc(test(attr(warn(unused))))] // 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/locktime/absolute.rs b/primitives/src/locktime/absolute.rs index 93f31a2c6..b5205524b 100644 --- a/primitives/src/locktime/absolute.rs +++ b/primitives/src/locktime/absolute.rs @@ -46,7 +46,7 @@ pub use units::locktime::absolute::*; /// # let n = LockTime::from_consensus(741521); // n OP_CHECKLOCKTIMEVERIFY /// # let lock_time = LockTime::from_consensus(741521); // nLockTime /// // To compare absolute lock times there are various `is_satisfied_*` methods, you may also use: -/// let is_satisfied = match (n, lock_time) { +/// let _is_satisfied = match (n, lock_time) { /// (Blocks(n), Blocks(lock_time)) => n <= lock_time, /// (Seconds(n), Seconds(lock_time)) => n <= lock_time, /// _ => panic!("handle invalid comparison error"), @@ -108,7 +108,6 @@ impl LockTime { /// /// ```rust /// # use bitcoin_primitives::absolute::LockTime; - /// # let n = LockTime::from_consensus(741521); // n OP_CHECKLOCKTIMEVERIFY /// /// // `from_consensus` roundtrips as expected with `to_consensus_u32`. /// let n_lock_time: u32 = 741521; @@ -237,7 +236,7 @@ impl LockTime { /// # Examples /// /// ```rust - /// # use bitcoin_primitives::absolute::{LockTime, LockTime::*}; + /// # use bitcoin_primitives::absolute::LockTime; /// let lock_time = LockTime::from_consensus(741521); /// let check = LockTime::from_consensus(741521 + 1); /// assert!(lock_time.is_implied_by(check)); @@ -270,7 +269,7 @@ impl LockTime { /// # let n = LockTime::from_consensus(741521); // n OP_CHECKLOCKTIMEVERIFY /// # let lock_time = LockTime::from_consensus(741521 + 1); // nLockTime /// - /// let is_satisfied = match (n, lock_time) { + /// let _is_satisfied = match (n, lock_time) { /// (Blocks(n), Blocks(lock_time)) => n <= lock_time, /// (Seconds(n), Seconds(lock_time)) => n <= lock_time, /// _ => panic!("invalid comparison"), diff --git a/primitives/src/locktime/relative.rs b/primitives/src/locktime/relative.rs index a890b940b..0d9658c5e 100644 --- a/primitives/src/locktime/relative.rs +++ b/primitives/src/locktime/relative.rs @@ -165,7 +165,7 @@ impl LockTime { /// /// ```rust /// # use bitcoin_primitives::Sequence; - /// # use bitcoin_primitives::locktime::relative::{LockTime, Height, Time}; + /// # use bitcoin_primitives::locktime::relative::{Height, Time}; /// /// # let required_height = 100; // 100 blocks. /// # let intervals = 70; // Approx 10 hours. @@ -204,7 +204,6 @@ impl LockTime { /// /// ```rust /// # use bitcoin_primitives::Sequence; - /// # use bitcoin_primitives::locktime::relative::{LockTime, Height, Time}; /// /// # let required_height = 100; // 100 blocks. /// # let lock = Sequence::from_height(required_height).to_relative_lock_time().expect("valid height"); @@ -252,7 +251,7 @@ impl LockTime { /// /// ```rust /// # use bitcoin_primitives::Sequence; - /// # use bitcoin_primitives::locktime::relative::{LockTime, Height, Time}; + /// # use bitcoin_primitives::locktime::relative::Height; /// /// let required_height: u16 = 100; /// let lock = Sequence::from_height(required_height).to_relative_lock_time().expect("valid height"); @@ -279,7 +278,7 @@ impl LockTime { /// /// ```rust /// # use bitcoin_primitives::Sequence; - /// # use bitcoin_primitives::locktime::relative::{LockTime, Height, Time}; + /// # use bitcoin_primitives::locktime::relative::Time; /// /// let intervals: u16 = 70; // approx 10 hours; /// let lock = Sequence::from_512_second_intervals(intervals).to_relative_lock_time().expect("valid time"); diff --git a/units/src/lib.rs b/units/src/lib.rs index addedf8d5..af2890b01 100644 --- a/units/src/lib.rs +++ b/units/src/lib.rs @@ -8,6 +8,7 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg))] // Coding conventions. #![warn(missing_docs)] +#![doc(test(attr(warn(unused))))] // 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.