What changed, and why it matters
This commit is purely a code-style cleanup. It turns on a Clippy lint that encourages using 'Self' instead of repeating type names inside Rust code, then updates many files in the rust-bitcoin hashes crate to follow that style. There is no functional change, no bug fix, and no security impact.
No security action needed. This is a routine lint/style commit. Reviewers may verify CI passes with the new lint enabled.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds clippy::use_self = ‘warn’ in hashes/Cargo.toml and hashes/embedded/Cargo.toml, then mechanically replaces explicit type constructors (e.g., Hash(…), HmacEngine {…}) with Self(…) / Self {…} and explicit type names in function signatures and trait implementations with Self where applicable. The diff shows only syntactic substitutions; no logic, constants, visibility, or behavior changed.
Changed components
hashes/Cargo.tomlhashes/embedded/Cargo.tomlhashes/src/hash160/mod.rshashes/src/hmac/mod.rshashes/src/ripemd160/mod.rshashes/src/sha1/mod.rshashes/src/sha256/crypto.rshashes/src/sha256/mod.rshashes/src/sha256d/mod.rshashes/src/sha256t/mod.rshashes/src/sha384/mod.rshashes/src/sha512/mod.rshashes/src/sha512_256/mod.rshashes/src/siphash24/mod.rshashes/tests/api.rsInspect captured patch +45 / −39
diff --git a/hashes/Cargo.toml b/hashes/Cargo.toml
index b59733e8..32dd8f64 100644
--- a/hashes/Cargo.toml
+++ b/hashes/Cargo.toml
@@ -37,3 +37,6 @@ rustdoc-args = ["--cfg", "docsrs"]
[lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(hashes_fuzz)', 'cfg(rust_v_1_64)' ] }
+
+[lints.clippy]
+use_self = "warn"
diff --git a/hashes/embedded/Cargo.toml b/hashes/embedded/Cargo.toml
index 531c98b6..ef146685 100644
--- a/hashes/embedded/Cargo.toml
+++ b/hashes/embedded/Cargo.toml
@@ -21,6 +21,9 @@ alloc-cortex-m = { version = "0.4.1", optional = true }
bitcoin_hashes = { path="../", default-features = false, features = [] }
bitcoin-io = { path = "../../io", default_features = false, features = ["hashes"] }
+[lints.clippy]
+use_self = "warn"
+
[[bin]]
name = "embedded"
test = false
diff --git a/hashes/src/hash160/mod.rs b/hashes/src/hash160/mod.rs
index 21440c30..499c6666 100644
--- a/hashes/src/hash160/mod.rs
+++ b/hashes/src/hash160/mod.rs
@@ -23,7 +23,7 @@ impl Hash {
let mut ret = [0; 20];
ret.copy_from_slice(rmd.as_byte_array());
- Hash(ret)
+ Self(ret)
}
}
diff --git a/hashes/src/hmac/mod.rs b/hashes/src/hmac/mod.rs
index 1acaee30..b1939fd6 100644
--- a/hashes/src/hmac/mod.rs
+++ b/hashes/src/hmac/mod.rs
@@ -22,7 +22,7 @@ pub struct Hmac<T: Hash>(T);
impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> {
type Err = <T as str::FromStr>::Err;
- fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Hmac(str::FromStr::from_str(s)?)) }
+ fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Self(str::FromStr::from_str(s)?)) }
}
impl<T: Hash> PartialEq for Hmac<T> {
@@ -46,7 +46,7 @@ impl<T: HashEngine> HmacEngine<T> {
/// # Panics
///
/// Larger hashes will result in a panic.
- pub fn new(key: &[u8]) -> HmacEngine<T>
+ pub fn new(key: &[u8]) -> Self
where
T: Default,
{
@@ -54,7 +54,7 @@ impl<T: HashEngine> HmacEngine<T> {
let mut ipad = [0x36u8; 128];
let mut opad = [0x5cu8; 128];
- let mut ret = HmacEngine { iengine: T::default(), oengine: T::default() };
+ let mut ret = Self { iengine: T::default(), oengine: T::default() };
if key.len() > T::BLOCK_SIZE {
let mut engine = T::default();
@@ -82,8 +82,8 @@ impl<T: HashEngine> HmacEngine<T> {
}
/// A special constructor giving direct access to the underlying "inner" and "outer" engines.
- pub fn from_inner_engines(iengine: T, oengine: T) -> HmacEngine<T> {
- HmacEngine { iengine, oengine }
+ pub fn from_inner_engines(iengine: T, oengine: T) -> Self {
+ Self { iengine, oengine }
}
}
@@ -121,7 +121,7 @@ impl<T: Hash> convert::AsRef<[u8]> for Hmac<T> {
impl<T: Hash> Hash for Hmac<T> {
type Bytes = T::Bytes;
- fn from_byte_array(bytes: T::Bytes) -> Self { Hmac(T::from_byte_array(bytes)) }
+ fn from_byte_array(bytes: T::Bytes) -> Self { Self(T::from_byte_array(bytes)) }
fn to_byte_array(self) -> Self::Bytes { self.0.to_byte_array() }
@@ -137,9 +137,9 @@ impl<T: Hash + Serialize> Serialize for Hmac<T> {
#[cfg(feature = "serde")]
impl<'de, T: Hash + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
- fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Hmac<T>, D::Error> {
+ fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let bytes = Deserialize::deserialize(d)?;
- Ok(Hmac(bytes))
+ Ok(Self(bytes))
}
}
diff --git a/hashes/src/ripemd160/mod.rs b/hashes/src/ripemd160/mod.rs
index 4c63eac8..e130eedb 100644
--- a/hashes/src/ripemd160/mod.rs
+++ b/hashes/src/ripemd160/mod.rs
@@ -36,7 +36,7 @@ impl Hash {
e.input(&(8 * n_bytes_hashed).to_le_bytes());
debug_assert_eq!(incomplete_block_len(&e), 0);
- Hash(e.midstate())
+ Self(e.midstate())
}
/// Finalize a hash engine to produce a hash.
diff --git a/hashes/src/sha1/mod.rs b/hashes/src/sha1/mod.rs
index 700c4746..c2a8aecc 100644
--- a/hashes/src/sha1/mod.rs
+++ b/hashes/src/sha1/mod.rs
@@ -35,7 +35,7 @@ impl Hash {
e.input(&(8 * n_bytes_hashed).to_be_bytes());
debug_assert_eq!(incomplete_block_len(&e), 0);
- Hash(e.midstate())
+ Self(e.midstate())
}
}
diff --git a/hashes/src/sha256/crypto.rs b/hashes/src/sha256/crypto.rs
index 5f208f06..f85e0ec9 100644
--- a/hashes/src/sha256/crypto.rs
+++ b/hashes/src/sha256/crypto.rs
@@ -243,7 +243,7 @@ impl Midstate {
output[i * 4 + 3] = (state[i + 0] >> 0) as u8;
i += 1;
}
- Midstate { bytes: output, bytes_hashed: bytes.len() as u64 }
+ Self { bytes: output, bytes_hashed: bytes.len() as u64 }
}
}
diff --git a/hashes/src/sha256/mod.rs b/hashes/src/sha256/mod.rs
index fe6da741..6633c673 100644
--- a/hashes/src/sha256/mod.rs
+++ b/hashes/src/sha256/mod.rs
@@ -46,14 +46,14 @@ impl HashEngine {
/// Constructs a new [`HashEngine`] from a [`Midstate`].
///
/// Please see docs on [`Midstate`] before using this function.
- pub fn from_midstate(midstate: Midstate) -> HashEngine {
+ pub fn from_midstate(midstate: Midstate) -> Self {
let mut ret = [0; 8];
for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate.as_ref().bitcoin_as_chunks().0)
{
*ret_val = u32::from_be_bytes(*midstate_bytes);
}
- HashEngine { buffer: [0; BLOCK_SIZE], h: ret, bytes_hashed: midstate.bytes_hashed }
+ Self { buffer: [0; BLOCK_SIZE], h: ret, bytes_hashed: midstate.bytes_hashed }
}
/// Returns `true` if the midstate can be extracted from this engine.
@@ -126,7 +126,7 @@ impl Hash {
e.input(&(8 * n_bytes_hashed).to_be_bytes());
debug_assert_eq!(incomplete_block_len(&e), 0);
- Hash(e.midstate_unchecked().bytes)
+ Self(e.midstate_unchecked().bytes)
}
/// Finalize a hash engine to obtain a hash.
@@ -149,7 +149,7 @@ impl Hash {
///
/// Warning: this function is inefficient. It should be only used in `const` context.
pub const fn hash_unoptimized(bytes: &[u8]) -> Self {
- Hash(Midstate::compute_midstate_unoptimized(bytes, true).bytes)
+ Self(Midstate::compute_midstate_unoptimized(bytes, true).bytes)
}
}
@@ -187,7 +187,7 @@ impl Midstate {
panic!("bytes hashed is not a multiple of 64");
}
- Midstate { bytes: state, bytes_hashed }
+ Self { bytes: state, bytes_hashed }
}
/// Deconstructs the [`Midstate`], returning the underlying byte array and number of bytes hashed.
diff --git a/hashes/src/sha256d/mod.rs b/hashes/src/sha256d/mod.rs
index 0e158351..0abfbaf3 100644
--- a/hashes/src/sha256d/mod.rs
+++ b/hashes/src/sha256d/mod.rs
@@ -18,7 +18,7 @@ impl Hash {
let mut ret = [0; 32];
ret.copy_from_slice(sha2d.as_byte_array());
- Hash(ret)
+ Self(ret)
}
}
diff --git a/hashes/src/sha256t/mod.rs b/hashes/src/sha256t/mod.rs
index b8804791..b7c73aa9 100644
--- a/hashes/src/sha256t/mod.rs
+++ b/hashes/src/sha256t/mod.rs
@@ -67,7 +67,7 @@ where
/// Produces a hash from the current state of a given engine.
pub fn from_engine(e: HashEngine<T>) -> Self {
- Hash::from_byte_array(sha256::Hash::from_engine(e.0).to_byte_array())
+ Self::from_byte_array(sha256::Hash::from_engine(e.0).to_byte_array())
}
/// Constructs a new engine.
@@ -108,16 +108,16 @@ impl<T: Tag> Clone for Hash<T> {
fn clone(&self) -> Self { *self }
}
impl<T: Tag> PartialEq for Hash<T> {
- fn eq(&self, other: &Hash<T>) -> bool { self.as_byte_array() == other.as_byte_array() }
+ fn eq(&self, other: &Self) -> bool { self.as_byte_array() == other.as_byte_array() }
}
impl<T: Tag> Eq for Hash<T> {}
impl<T: Tag> PartialOrd for Hash<T> {
- fn partial_cmp(&self, other: &Hash<T>) -> Option<cmp::Ordering> {
+ fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(cmp::Ord::cmp(self, other))
}
}
impl<T: Tag> Ord for Hash<T> {
- fn cmp(&self, other: &Hash<T>) -> cmp::Ordering {
+ fn cmp(&self, other: &Self) -> cmp::Ordering {
cmp::Ord::cmp(&self.as_byte_array(), &other.as_byte_array())
}
}
@@ -134,7 +134,7 @@ pub struct HashEngine<T>(sha256::HashEngine, PhantomData<T>);
impl<T: Tag> Default for HashEngine<T> {
fn default() -> Self {
let tagged = sha256::HashEngine::from_midstate(T::MIDSTATE);
- HashEngine(tagged, PhantomData)
+ Self(tagged, PhantomData)
}
}
diff --git a/hashes/src/sha384/mod.rs b/hashes/src/sha384/mod.rs
index f1a53ff4..baa5651c 100644
--- a/hashes/src/sha384/mod.rs
+++ b/hashes/src/sha384/mod.rs
@@ -15,7 +15,7 @@ impl Hash {
pub fn from_engine(e: HashEngine) -> Self {
let mut ret = [0; 48];
ret.copy_from_slice(&sha512::Hash::from_engine(e.0).as_byte_array()[..48]);
- Hash(ret)
+ Self(ret)
}
}
diff --git a/hashes/src/sha512/mod.rs b/hashes/src/sha512/mod.rs
index b71c5cac..b5fbc610 100644
--- a/hashes/src/sha512/mod.rs
+++ b/hashes/src/sha512/mod.rs
@@ -38,7 +38,7 @@ impl Hash {
e.input(&(8 * n_bytes_hashed).to_be_bytes());
debug_assert_eq!(incomplete_block_len(&e), 0);
- Hash(e.midstate())
+ Self(e.midstate())
}
/// Finalize a hash engine to produce a hash.
@@ -99,7 +99,7 @@ impl HashEngine {
/// Constructs a new hash engine suitable for use constructing a `sha512_256::HashEngine`.
#[rustfmt::skip]
pub(crate) const fn sha512_256() -> Self {
- HashEngine {
+ Self {
h: [
0x22312194fc2bf72c, 0x9f555fa3c84c64c2, 0x2393b86b6f53b151, 0x963877195940eabd,
0x96283ee2a88effe3, 0xbe5e1e2553863992, 0x2b0199fc2c85b8aa, 0x0eb72ddc81c52ca2,
@@ -112,7 +112,7 @@ impl HashEngine {
/// Constructs a new hash engine suitable for constructing a `sha384::HashEngine`.
#[rustfmt::skip]
pub(crate) const fn sha384() -> Self {
- HashEngine {
+ Self {
h: [
0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4,
diff --git a/hashes/src/sha512_256/mod.rs b/hashes/src/sha512_256/mod.rs
index c2b64f60..1ce6f657 100644
--- a/hashes/src/sha512_256/mod.rs
+++ b/hashes/src/sha512_256/mod.rs
@@ -20,7 +20,7 @@ impl Hash {
pub fn from_engine(e: HashEngine) -> Self {
let mut ret = [0; 32];
ret.copy_from_slice(&sha512::Hash::from_engine(e.0).as_byte_array()[..32]);
- Hash(ret)
+ Self(ret)
}
}
diff --git a/hashes/src/siphash24/mod.rs b/hashes/src/siphash24/mod.rs
index 9dced9d2..d65585dd 100644
--- a/hashes/src/siphash24/mod.rs
+++ b/hashes/src/siphash24/mod.rs
@@ -77,8 +77,8 @@ pub struct HashEngine {
impl HashEngine {
/// Constructs a new SipHash24 engine with keys.
#[inline]
- pub const fn with_keys(k0: u64, k1: u64) -> HashEngine {
- HashEngine {
+ pub const fn with_keys(k0: u64, k1: u64) -> Self {
+ Self {
k0,
k1,
bytes_hashed: 0,
@@ -132,7 +132,7 @@ impl crate::HashEngine for HashEngine {
return;
} else {
self.state.v3 ^= self.tail;
- HashEngine::c_rounds(&mut self.state);
+ Self::c_rounds(&mut self.state);
self.state.v0 ^= self.tail;
self.ntail = 0;
}
@@ -147,7 +147,7 @@ impl crate::HashEngine for HashEngine {
let mi = unsafe { load_int_le!(msg, i, u64) };
self.state.v3 ^= mi;
- HashEngine::c_rounds(&mut self.state);
+ Self::c_rounds(&mut self.state);
self.state.v0 ^= mi;
i += 8;
@@ -168,7 +168,7 @@ impl Hash {
/// Produces a hash from the current state of a given engine.
#[cfg(not(hashes_fuzz))]
- pub fn from_engine(e: HashEngine) -> Self { Hash::from_u64(Hash::from_engine_to_u64(e)) }
+ pub fn from_engine(e: HashEngine) -> Self { Self::from_u64(Self::from_engine_to_u64(e)) }
#[cfg(hashes_fuzz)]
pub fn from_engine(e: HashEngine) -> Self {
@@ -177,17 +177,17 @@ impl Hash {
}
/// Hashes the given data with an engine with the provided keys.
- pub fn hash_with_keys(k0: u64, k1: u64, data: &[u8]) -> Hash {
+ pub fn hash_with_keys(k0: u64, k1: u64, data: &[u8]) -> Self {
let mut engine = HashEngine::with_keys(k0, k1);
engine.input(data);
- Hash::from_engine(engine)
+ Self::from_engine(engine)
}
/// Hashes the given data directly to u64 with an engine with the provided keys.
pub fn hash_to_u64_with_keys(k0: u64, k1: u64, data: &[u8]) -> u64 {
let mut engine = HashEngine::with_keys(k0, k1);
engine.input(data);
- Hash::from_engine_to_u64(engine)
+ Self::from_engine_to_u64(engine)
}
/// Produces a hash as `u64` from the current state of a given engine.
@@ -211,7 +211,7 @@ impl Hash {
pub fn to_u64(self) -> u64 { u64::from_le_bytes(self.0) }
/// Constructs a new hash from its (little endian) 64-bit integer representation.
- pub fn from_u64(hash: u64) -> Hash { Hash(hash.to_le_bytes()) }
+ pub fn from_u64(hash: u64) -> Self { Self(hash.to_le_bytes()) }
}
/// Loads a u64 using up to 7 bytes of a byte slice.
diff --git a/hashes/tests/api.rs b/hashes/tests/api.rs
index 2b8eed60..afa3f9c2 100644
--- a/hashes/tests/api.rs
+++ b/hashes/tests/api.rs
@@ -66,7 +66,7 @@ impl Hashes<Sha256> {
let tagged = TaggedHash::from_byte_array(Sha256t::<Tag>::hash(&[]).to_byte_array());
let siphash = Siphash24::from_engine(siphash24::HashEngine::with_keys(0, 0));
- Hashes {
+ Self {
a: Hash160::hash(&[]),
// b: hkdf,
c: hmac,
@@ -105,7 +105,7 @@ struct Engines {
impl Engines {
fn new_sha256() -> Self {
- Engines {
+ Self {
a: hash160::HashEngine::new(),
b: hmac::HmacEngine::<sha256::HashEngine>::new(&[]),
c: ripemd160::HashEngine::new(),
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.