From cbff33a1b5fa2fe5ba475f9f95f4ea0f1e04fae3 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:00:15 +1000 Subject: [PATCH 01/11] add clippy configuration file Add a `clippy.toml` configuration file using the current MSRV of 1.41.1 --- clippy.toml | 1 + 1 file changed, 1 insertion(+) create mode 100644 clippy.toml diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..799264e --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +msrv = "1.41.1" From 07c3e501b1b75c906df2c916c83b4cda5929fd53 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:00:43 +1000 Subject: [PATCH 02/11] Remove unneeded 'static lifetime Clippy emits: warning: constants have by default a `'static` lifetime We have edition 2018 now so we do not need the explicit static lifetime. --- src/pbkdf2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index e7d3375..89fc18c 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -1,6 +1,6 @@ use bitcoin_hashes::{hmac, sha512, Hash, HashEngine}; -const SALT_PREFIX: &'static str = "mnemonic"; +const SALT_PREFIX: &str = "mnemonic"; /// Calculate the binary size of the mnemonic. fn mnemonic_byte_len(mnemonic: M) -> usize From 4d7e783c2d914da20e080f947cea96f2163ed6d8 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:09:54 +1000 Subject: [PATCH 03/11] Make operator precedence explicit clippy emits: warning: operator precedence can trip the unwary As suggested, add parenthesis to make the operator precedence explicit. --- src/pbkdf2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index 89fc18c..4249f68 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -83,7 +83,7 @@ fn create_hmac_engine(mnemonic: M) -> hmac::HmacEngine fn u32_to_array_be(val: u32) -> [u8; 4] { let mut res = [0; 4]; for i in 0..4 { - res[i] = ((val >> (4 - i - 1) * 8) & 0xff) as u8; + res[i] = ((val >> ((4 - i - 1) * 8)) & 0xff) as u8; } res } From 1f0538897b7b86f7038460af31c186a7d3cc82ab Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:12:02 +1000 Subject: [PATCH 04/11] Use byte literal instead of cast Clippy emits: warning: casting a character literal to `u8` truncates As suggested, use byte literal instead of a cast. --- src/pbkdf2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index 4249f68..2caaf7b 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -58,8 +58,8 @@ fn create_hmac_engine(mnemonic: M) -> hmac::HmacEngine let mut cursor = 0; for (i, word) in mnemonic.enumerate() { if i > 0 { - ipad[cursor] ^= ' ' as u8; - opad[cursor] ^= ' ' as u8; + ipad[cursor] ^= b' '; + opad[cursor] ^= b' '; cursor += 1; } for (b_i, b_h) in ipad.iter_mut().skip(cursor).zip(word.as_bytes()) { From a8bfa8bde8ad4122716163c55b22cdfc7f52acb6 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:16:52 +1000 Subject: [PATCH 05/11] Remove unneeded references Clippy emits a few warnings of form: warning: this expression creates a reference which is immediately dereferenced by the compiler Remove unneeded references. --- benches/bench.rs | 6 +++--- src/language/mod.rs | 2 +- src/lib.rs | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index 9d1c316..1043f6f 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -38,7 +38,7 @@ const LANG: Language = Language::Spanish; #[bench] fn validate(b: &mut Bencher) { let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes(); - let mnemonic = Mnemonic::from_entropy_in(LANG, &entropy).unwrap(); + let mnemonic = Mnemonic::from_entropy_in(LANG, entropy).unwrap(); assert_eq!(mnemonic.word_count(), 24); let phrase = mnemonic.to_string(); @@ -52,7 +52,7 @@ fn from_entropy(b: &mut Bencher) { let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes(); b.iter(|| { - let _ = Mnemonic::from_entropy_in(LANG, &entropy).unwrap(); + let _ = Mnemonic::from_entropy_in(LANG, entropy).unwrap(); }); } @@ -66,7 +66,7 @@ fn new_mnemonic(b: &mut Bencher) { #[bench] fn to_seed(b: &mut Bencher) { let entropy = "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f".as_bytes(); - let m = Mnemonic::from_entropy_in(LANG, &entropy).unwrap(); + let m = Mnemonic::from_entropy_in(LANG, entropy).unwrap(); b.iter(|| { let _ = m.to_seed(""); diff --git a/src/language/mod.rs b/src/language/mod.rs index 1f5f922..85c2d9b 100644 --- a/src/language/mod.rs +++ b/src/language/mod.rs @@ -220,7 +220,7 @@ mod tests { let mut digest = sha256::Hash::engine(); for (_idx, word) in lang.word_list().iter().enumerate() { #[cfg(feature = "std")] - assert!(::unicode_normalization::is_nfkd(&word)); + assert!(::unicode_normalization::is_nfkd(word)); digest.input(word.as_bytes()); digest.input("\n".as_bytes()); } diff --git a/src/lib.rs b/src/lib.rs index 856ffe6..8b8bb8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -207,7 +207,7 @@ impl Mnemonic { return Err(Error::BadEntropyBitCount(nb_bits)); } - let check = sha256::Hash::hash(&entropy); + let check = sha256::Hash::hash(entropy); let mut bits = [false; MAX_ENTROPY_BITS + MAX_CHECKSUM_BITS]; for i in 0..nb_bytes { for j in 0..8 { @@ -789,9 +789,9 @@ mod tests { ]; for vector in &test_vectors { - let entropy = Vec::::from_hex(&vector.0).unwrap(); + let entropy = Vec::::from_hex(vector.0).unwrap(); let mnemonic_str = vector.1; - let seed = Vec::::from_hex(&vector.2).unwrap(); + let seed = Vec::::from_hex(vector.2).unwrap(); let mnemonic = Mnemonic::from_entropy(&entropy).unwrap(); @@ -1050,10 +1050,10 @@ mod tests { ]; for vector in &vectors { - let entropy = Vec::::from_hex(&vector.0).unwrap(); + let entropy = Vec::::from_hex(vector.0).unwrap(); let mnemonic_str = vector.1; let passphrase = vector.2; - let seed = Vec::::from_hex(&vector.3).unwrap(); + let seed = Vec::::from_hex(vector.3).unwrap(); let mnemonic = Mnemonic::from_entropy_in(Language::Japanese, &entropy).unwrap(); From 60afec7f09c6145f5b564d4fe88032a87e643673 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:17:45 +1000 Subject: [PATCH 06/11] Remove explicit lifetime Clippy emits: warning: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration) As suggested, remove explicit lifetime. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8b8bb8d..f7fb2f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,7 +183,7 @@ impl Mnemonic { /// can be avoided for languages without special UTF8 characters. #[inline] #[cfg(feature = "std")] - pub fn normalize_utf8_cow<'a>(cow: &mut Cow<'a, str>) { + pub fn normalize_utf8_cow(cow: &mut Cow<'_, str>) { let is_nfkd = unicode_normalization::is_nfkd_quick(cow.as_ref().chars()); if is_nfkd != unicode_normalization::IsNormalized::Yes { *cow = Cow::Owned(cow.as_ref().nfkd().to_string()); From c246aaca715ccca0884e34442ad1bb4cd5bc9cae Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:21:01 +1000 Subject: [PATCH 07/11] Remove unneeded return statement Clippy emits: warning: unneeded `return` statement As suggested, remove the unneeded return statement. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f7fb2f6..8475268 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -372,7 +372,7 @@ impl Mnemonic { } } - return Err(Error::AmbiguousLanguages(AmbiguousLanguages(possible))); + Err(Error::AmbiguousLanguages(AmbiguousLanguages(possible))) } /// Determine the language of the mnemonic. From 0b5858095931a78ff84d1139460fc763e83856a3 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:21:57 +1000 Subject: [PATCH 08/11] Use is_empty Clippy emits warning: length comparison to zero As suggested, use `is_empty`. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8475268..ec2b45a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -328,7 +328,7 @@ impl Mnemonic { { // Start scope to drop first_word so that words can be reborrowed later. let first_word = words.peek().ok_or(Error::BadWordCount(0))?; - if first_word.len() == 0 { + if first_word.is_empty() { return Err(Error::BadWordCount(0)); } From ca357850cf72f73e875ef223751f22b749989cfb Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:26:14 +1000 Subject: [PATCH 09/11] Remove useless use of vec macro Clippy emits: warning: useless use of `vec!` As suggested, remove the useless call to `vec!` and use the slice directly. --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ec2b45a..916ffde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -883,13 +883,13 @@ mod tests { #[test] fn test_invalid_entropy() { //between 128 and 256 bits, but not divisible by 32 - assert_eq!(Mnemonic::from_entropy(&vec![b'x'; 17]), Err(Error::BadEntropyBitCount(136))); + assert_eq!(Mnemonic::from_entropy(&[b'x'; 17]), Err(Error::BadEntropyBitCount(136))); //less than 128 bits - assert_eq!(Mnemonic::from_entropy(&vec![b'x'; 4]), Err(Error::BadEntropyBitCount(32))); + assert_eq!(Mnemonic::from_entropy(&[b'x'; 4]), Err(Error::BadEntropyBitCount(32))); //greater than 256 bits - assert_eq!(Mnemonic::from_entropy(&vec![b'x'; 36]), Err(Error::BadEntropyBitCount(288))); + assert_eq!(Mnemonic::from_entropy(&[b'x'; 36]), Err(Error::BadEntropyBitCount(288))); } #[cfg(all(feature = "japanese", feature = "std"))] From 41eddb98df946daaf77289ae6b8781cbbe282110 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:33:51 +1000 Subject: [PATCH 10/11] Remove unused lifetime Clippy emits: warning: this lifetime isn't used in the impl Remove unused lifetime. --- src/internal_macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal_macros.rs b/src/internal_macros.rs index 4d4ece1..2cc5f0e 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -47,7 +47,7 @@ macro_rules! serde_string_impl { } #[cfg(feature = "serde")] - impl<'de> $crate::serde::Serialize for $name { + impl $crate::serde::Serialize for $name { fn serialize(&self, serializer: S) -> Result where S: $crate::serde::Serializer, From a083fafb69fdafddb84b81ab85cc07c5a2f0ec80 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 30 Jun 2022 14:50:57 +1000 Subject: [PATCH 11/11] Use hyperlink Building the docs throws a warning: warning: this URL is not a hyperlink As suggested, use a hyperlink. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 916ffde..2eb85e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ //! # BIP39 Mnemonic Codes //! -//! https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki +//! //! #![deny(non_upper_case_globals)]