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
This commit is contained in:
honeywest
2021-03-08 18:35:26 +08:00
committed by GitHub
parent f39c2cf3f5
commit b3f8e064f2
10 changed files with 37 additions and 46 deletions
@@ -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));
@@ -92,7 +92,7 @@ pub trait FixedPointNumber:
///
/// Returns `None` if `int` exceeds accuracy.
fn checked_from_integer(int: Self::Inner) -> Option<Self> {
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)
}
}
+2 -2
View File
@@ -203,7 +203,7 @@ pub fn normalize<T>(input: &[T], targeted_sum: T) -> Result<Vec<T>, &'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<T>(input: &[T], targeted_sum: T) -> Result<Vec<T>, &'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()
}
@@ -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()
@@ -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((
+2 -2
View File
@@ -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)
-2
View File
@@ -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")]
-2
View File
@@ -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]) {
@@ -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 {
+16 -17
View File
@@ -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<u8> 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<Vec<WasmFieldName>> for WasmFields {
fn from(v: Vec<WasmFieldName>) -> 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 }
)