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()