From b3f8e064f297e7ea71dc4e490515d4850c089c10 Mon Sep 17 00:00:00 2001 From: honeywest <50997103+honeywest@users.noreply.github.com> Date: Mon, 8 Mar 2021 18:35:26 +0800 Subject: [PATCH] Clippy arithmetic new (#8282) * optimize code * fix clippy replace = with += or %= * fix redundant closure found warning * redundant field names in struct initialization * fix clippy warning and optimize code * fix clippy warning * fix clippy warning * fix test error --- .../primitives/arithmetic/src/biguint.rs | 2 +- .../primitives/arithmetic/src/fixed_point.rs | 26 +++++++-------- substrate/primitives/arithmetic/src/lib.rs | 4 +-- .../primitives/arithmetic/src/per_things.rs | 4 +-- substrate/primitives/core/src/changes_trie.rs | 4 +-- substrate/primitives/core/src/ecdsa.rs | 4 +-- substrate/primitives/core/src/ed25519.rs | 2 -- substrate/primitives/core/src/hashing.rs | 2 -- substrate/primitives/core/src/offchain/mod.rs | 2 +- substrate/primitives/tracing/src/types.rs | 33 +++++++++---------- 10 files changed, 37 insertions(+), 46 deletions(-) diff --git a/substrate/primitives/arithmetic/src/biguint.rs b/substrate/primitives/arithmetic/src/biguint.rs index 210cba8e2b..9813277506 100644 --- a/substrate/primitives/arithmetic/src/biguint.rs +++ b/substrate/primitives/arithmetic/src/biguint.rs @@ -326,7 +326,7 @@ impl BigUint { // PROOF: 0 <= normalizer_bits < SHIFT 0 <= normalizer < B. all conversions are // safe. let normalizer_bits = other.msb().leading_zeros() as Single; - let normalizer = (2 as Single).pow(normalizer_bits as u32) as Single; + let normalizer = 2_u32.pow(normalizer_bits as u32) as Single; // step D1. let mut self_norm = self.mul(&Self::from(normalizer)); diff --git a/substrate/primitives/arithmetic/src/fixed_point.rs b/substrate/primitives/arithmetic/src/fixed_point.rs index 44a8695610..896d5f3845 100644 --- a/substrate/primitives/arithmetic/src/fixed_point.rs +++ b/substrate/primitives/arithmetic/src/fixed_point.rs @@ -92,7 +92,7 @@ pub trait FixedPointNumber: /// /// Returns `None` if `int` exceeds accuracy. fn checked_from_integer(int: Self::Inner) -> Option { - int.checked_mul(&Self::DIV).map(|inner| Self::from_inner(inner)) + int.checked_mul(&Self::DIV).map(Self::from_inner) } /// Creates `self` from a rational number. Equal to `n / d`. @@ -119,7 +119,7 @@ pub trait FixedPointNumber: multiply_by_rational(n.value, Self::DIV.unique_saturated_into(), d.value).ok() .and_then(|value| from_i129(I129 { value, negative })) - .map(|inner| Self::from_inner(inner)) + .map(Self::from_inner) } /// Checked multiplication for integer type `N`. Equal to `self * n`. @@ -184,7 +184,7 @@ pub trait FixedPointNumber: if inner >= Self::Inner::zero() { self } else { - Self::from_inner(inner.checked_neg().unwrap_or_else(|| Self::Inner::max_value())) + Self::from_inner(inner.checked_neg().unwrap_or_else(Self::Inner::max_value)) } } @@ -230,7 +230,7 @@ pub trait FixedPointNumber: self.into_inner().checked_div(&Self::DIV) .expect("panics only if DIV is zero, DIV is not zero; qed") .checked_mul(&Self::DIV) - .map(|inner| Self::from_inner(inner)) + .map(Self::from_inner) .expect("can not overflow since fixed number is >= integer part") } @@ -254,12 +254,10 @@ pub trait FixedPointNumber: fn ceil(self) -> Self { if self.is_negative() { self.trunc() + } else if self.frac() == Self::zero() { + self } else { - if self.frac() == Self::zero() { - self - } else { - self.saturating_add(Self::one()).trunc() - } + self.saturating_add(Self::one()).trunc() } } @@ -281,12 +279,10 @@ pub trait FixedPointNumber: let n = self.frac().saturating_mul(Self::saturating_from_integer(10)); if n < Self::saturating_from_integer(5) { self.trunc() + } else if self.is_positive() { + self.saturating_add(Self::one()).trunc() } else { - if self.is_positive() { - self.saturating_add(Self::one()).trunc() - } else { - self.saturating_sub(Self::one()).trunc() - } + self.saturating_sub(Self::one()).trunc() } } } @@ -585,7 +581,7 @@ macro_rules! implement_fixed { { use sp_std::str::FromStr; let s = String::deserialize(deserializer)?; - $name::from_str(&s).map_err(|err_str| de::Error::custom(err_str)) + $name::from_str(&s).map_err(de::Error::custom) } } diff --git a/substrate/primitives/arithmetic/src/lib.rs b/substrate/primitives/arithmetic/src/lib.rs index 9b1e8711da..561c14a37e 100644 --- a/substrate/primitives/arithmetic/src/lib.rs +++ b/substrate/primitives/arithmetic/src/lib.rs @@ -203,7 +203,7 @@ pub fn normalize(input: &[T], targeted_sum: T) -> Result, &'static str .expect("Proof provided in the module doc; qed."); if output_with_idx[min_index].1 >= threshold { min_index += 1; - min_index = min_index % count; + min_index %= count; } } } @@ -215,7 +215,7 @@ pub fn normalize(input: &[T], targeted_sum: T) -> Result, &'static str .expect("Proof provided in the module doc; qed."); if output_with_idx[min_index].1 >= threshold { min_index += 1; - min_index = min_index % count; + min_index %= count; } leftover -= One::one() } diff --git a/substrate/primitives/arithmetic/src/per_things.rs b/substrate/primitives/arithmetic/src/per_things.rs index 319666747b..f2b8c4f93b 100644 --- a/substrate/primitives/arithmetic/src/per_things.rs +++ b/substrate/primitives/arithmetic/src/per_things.rs @@ -307,13 +307,13 @@ where // Round up if the fractional part of the result is non-zero. Rounding::Up => if rem_mul_upper % denom_upper > 0.into() { // `rem * numer / denom` is less than `numer`, so this will not overflow. - rem_mul_div_inner = rem_mul_div_inner + 1.into(); + rem_mul_div_inner += 1.into(); }, // Round up if the fractional part of the result is greater than a half. An exact half is // rounded down. Rounding::Nearest => if rem_mul_upper % denom_upper > denom_upper / 2.into() { // `rem * numer / denom` is less than `numer`, so this will not overflow. - rem_mul_div_inner = rem_mul_div_inner + 1.into(); + rem_mul_div_inner += 1.into(); }, } rem_mul_div_inner.into() diff --git a/substrate/primitives/core/src/changes_trie.rs b/substrate/primitives/core/src/changes_trie.rs index 32991ce44a..3291026f32 100644 --- a/substrate/primitives/core/src/changes_trie.rs +++ b/substrate/primitives/core/src/changes_trie.rs @@ -90,7 +90,7 @@ impl ChangesTrieConfiguration { return max_digest_interval; } - current_level = current_level - 1; + current_level -= 1; } } @@ -192,7 +192,7 @@ impl ChangesTrieConfiguration { digest_step = digest_interval; digest_interval = new_digest_interval; - current_level = current_level + 1; + current_level += 1; } Some(( diff --git a/substrate/primitives/core/src/ecdsa.rs b/substrate/primitives/core/src/ecdsa.rs index fc9b16beed..ee4f8f811b 100644 --- a/substrate/primitives/core/src/ecdsa.rs +++ b/substrate/primitives/core/src/ecdsa.rs @@ -487,7 +487,7 @@ impl TraitPair for Pair { let message = secp256k1::Message::parse(&blake2_256(message.as_ref())); let sig: (_, _) = match sig.try_into() { Ok(x) => x, _ => return false }; match secp256k1::recover(&message, &sig.0, &sig.1) { - Ok(actual) => &pubkey.0[..] == &actual.serialize_compressed()[..], + Ok(actual) => pubkey.0[..] == actual.serialize_compressed()[..], _ => false, } } @@ -525,7 +525,7 @@ impl Pair { #[cfg(feature = "std")] pub fn from_legacy_string(s: &str, password_override: Option<&str>) -> Pair { Self::from_string(s, password_override).unwrap_or_else(|_| { - let mut padded_seed: Seed = [' ' as u8; 32]; + let mut padded_seed: Seed = [b' '; 32]; let len = s.len().min(32); padded_seed[..len].copy_from_slice(&s.as_bytes()[..len]); Self::from_seed(&padded_seed) diff --git a/substrate/primitives/core/src/ed25519.rs b/substrate/primitives/core/src/ed25519.rs index 6589310931..3269f70be1 100644 --- a/substrate/primitives/core/src/ed25519.rs +++ b/substrate/primitives/core/src/ed25519.rs @@ -25,8 +25,6 @@ use sp_std::vec::Vec; use crate::{hash::H256, hash::H512}; use codec::{Encode, Decode}; -#[cfg(feature = "full_crypto")] -use blake2_rfc; #[cfg(feature = "full_crypto")] use core::convert::TryFrom; #[cfg(feature = "full_crypto")] diff --git a/substrate/primitives/core/src/hashing.rs b/substrate/primitives/core/src/hashing.rs index ac0eedef69..4c719f7c69 100644 --- a/substrate/primitives/core/src/hashing.rs +++ b/substrate/primitives/core/src/hashing.rs @@ -22,10 +22,8 @@ //! unless you know what you're doing. Using `sp_io` will be more performant, since instead of //! computing the hash in WASM it delegates that computation to the host client. -use blake2_rfc; use sha2::{Digest, Sha256}; use tiny_keccak::{Hasher, Keccak}; -use twox_hash; /// Do a Blake2 512-bit hash and place result in `dest`. pub fn blake2_512_into(data: &[u8], dest: &mut [u8; 64]) { diff --git a/substrate/primitives/core/src/offchain/mod.rs b/substrate/primitives/core/src/offchain/mod.rs index ef6c38a7d6..6a08df1d7f 100644 --- a/substrate/primitives/core/src/offchain/mod.rs +++ b/substrate/primitives/core/src/offchain/mod.rs @@ -30,7 +30,7 @@ pub mod storage; pub mod testing; /// Local storage prefix used by the Offchain Worker API to -pub const STORAGE_PREFIX : &'static [u8] = b"storage"; +pub const STORAGE_PREFIX : &[u8] = b"storage"; /// Offchain workers local storage. pub trait OffchainStorage: Clone + Send + Sync { diff --git a/substrate/primitives/tracing/src/types.rs b/substrate/primitives/tracing/src/types.rs index 725565c371..44f6b2f7ff 100644 --- a/substrate/primitives/tracing/src/types.rs +++ b/substrate/primitives/tracing/src/types.rs @@ -43,12 +43,12 @@ pub enum WasmLevel { impl From<&tracing_core::Level> for WasmLevel { fn from(l: &tracing_core::Level) -> WasmLevel { - match l { - &tracing_core::Level::ERROR => WasmLevel::ERROR, - &tracing_core::Level::WARN => WasmLevel::WARN, - &tracing_core::Level::INFO => WasmLevel::INFO, - &tracing_core::Level::DEBUG => WasmLevel::DEBUG, - &tracing_core::Level::TRACE => WasmLevel::TRACE, + match *l { + tracing_core::Level::ERROR => WasmLevel::ERROR, + tracing_core::Level::WARN => WasmLevel::WARN, + tracing_core::Level::INFO => WasmLevel::INFO, + tracing_core::Level::DEBUG => WasmLevel::DEBUG, + tracing_core::Level::TRACE => WasmLevel::TRACE, } } } @@ -129,7 +129,7 @@ impl From for WasmValue { impl From<&i8> for WasmValue { fn from(inp: &i8) -> WasmValue { - WasmValue::I8(inp.clone()) + WasmValue::I8(*inp) } } @@ -246,7 +246,7 @@ impl WasmFields { impl From> for WasmFields { fn from(v: Vec) -> WasmFields { - WasmFields(v.into()) + WasmFields(v) } } @@ -447,7 +447,7 @@ impl From<&tracing_core::Event<'_>> for WasmEntryAttributes { WasmEntryAttributes { parent_id: evt.parent().map(|id| id.into_u64()), metadata: evt.metadata().into(), - fields: fields + fields } } } @@ -459,7 +459,7 @@ impl From<&tracing_core::span::Attributes<'_>> for WasmEntryAttributes { WasmEntryAttributes { parent_id: attrs.parent().map(|id| id.into_u64()), metadata: attrs.metadata().into(), - fields: fields + fields } } } @@ -478,7 +478,6 @@ impl core::default::Default for WasmEntryAttributes { mod std_features { use tracing_core::callsite; - use tracing; /// Static entry use for wasm-originated metadata. pub struct WasmCallsite; @@ -488,13 +487,13 @@ mod std_features { } static CALLSITE: WasmCallsite = WasmCallsite; /// The identifier we are using to inject the wasm events in the generic `tracing` system - pub static WASM_TRACE_IDENTIFIER: &'static str = "wasm_tracing"; + pub static WASM_TRACE_IDENTIFIER: &str = "wasm_tracing"; /// The fieldname for the wasm-originated name - pub static WASM_NAME_KEY: &'static str = "name"; + pub static WASM_NAME_KEY: &str = "name"; /// The fieldname for the wasm-originated target - pub static WASM_TARGET_KEY: &'static str = "target"; + pub static WASM_TARGET_KEY: &str = "target"; /// The the list of all static field names we construct from the given metadata - pub static GENERIC_FIELDS: &'static [&'static str] = &[WASM_TARGET_KEY, WASM_NAME_KEY, + pub static GENERIC_FIELDS: &[&str] = &[WASM_TARGET_KEY, WASM_NAME_KEY, "file", "line", "module_path", "params"]; // Implementation Note: @@ -592,7 +591,7 @@ mod std_features { let metadata : &tracing_core::metadata::Metadata<'static> = (&a.metadata).into(); tracing::span::Span::child_of( - a.parent_id.map(|i|tracing_core::span::Id::from_u64(i)), + a.parent_id.map(tracing_core::span::Id::from_u64), &metadata, &tracing::valueset!{ metadata.fields(), target, name, file, line, module_path, ?params } ) @@ -611,7 +610,7 @@ mod std_features { let metadata : &tracing_core::metadata::Metadata<'static> = (&self.metadata).into(); tracing_core::Event::child_of( - self.parent_id.map(|i|tracing_core::span::Id::from_u64(i)), + self.parent_id.map(tracing_core::span::Id::from_u64), &metadata, &tracing::valueset!{ metadata.fields(), target, name, file, line, module_path, ?params } )