Skip to content

Commit e013911

Browse files
committed
Fix or allow clippy warnings
1 parent 560b11d commit e013911

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/lib.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
//!
4040
//! println!("Input ({}) with 10 decimals: {} vs {})", input, dec, float);
4141
//! ```
42+
#![allow(clippy::unreadable_literal)]
43+
#![allow(clippy::needless_return)]
44+
#![allow(clippy::suspicious_arithmetic_impl)]
45+
#![allow(clippy::suspicious_op_assign_impl)]
46+
#![allow(clippy::redundant_field_names)]
4247

4348
extern crate num_bigint;
4449
extern crate num_integer;
@@ -61,6 +66,8 @@ use num_bigint::{BigInt, ParseBigIntError, Sign, ToBigInt};
6166
use num_integer::Integer;
6267
pub use traits::{FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
6368

69+
const LOG2_10: f64 = 3.321928094887362_f64;
70+
6471
#[macro_use]
6572
mod macros;
6673

@@ -93,7 +100,7 @@ fn count_decimal_digits(int: &BigInt) -> u64 {
93100
// guess number of digits based on number of bits in UInt
94101
let mut digits = (int.bits() as f64 / 3.3219280949) as u64;
95102
let mut num = ten_to_the(digits);
96-
while int >= &num {
103+
while *int >= num {
97104
num *= 10u8;
98105
digits += 1;
99106
}
@@ -118,11 +125,11 @@ fn get_rounding_term(num: &BigInt) -> u8 {
118125

119126
// loop-method
120127
loop {
121-
if num < &n {
128+
if *num < n {
122129
return 1;
123130
}
124131
n *= 5;
125-
if num < &n {
132+
if *num < n {
126133
return 0;
127134
}
128135
n *= 2;
@@ -389,15 +396,14 @@ impl BigDecimal {
389396

390397
// make guess
391398
let guess = {
392-
let log2_10 = 3.32192809488736234787031942948939018_f64;
393399
let magic_guess_scale = 1.1951678538495576_f64;
394-
let initial_guess = (self.int_val.bits() as f64 - self.scale as f64 * log2_10) / 2.0;
400+
let initial_guess = (self.int_val.bits() as f64 - self.scale as f64 * LOG2_10) / 2.0;
395401
let res = magic_guess_scale * initial_guess.exp2();
396402
if res.is_normal() {
397403
BigDecimal::try_from(res).unwrap()
398404
} else {
399405
// can't guess with float - just guess magnitude
400-
let scale = (self.int_val.bits() as f64 / -log2_10 + self.scale as f64).round() as i64;
406+
let scale = (self.int_val.bits() as f64 / -LOG2_10 + self.scale as f64).round() as i64;
401407
BigDecimal::new(BigInt::from(1), scale / 2)
402408
}
403409
};
@@ -462,15 +468,14 @@ impl BigDecimal {
462468

463469
// make guess
464470
let guess = {
465-
let log2_10 = 3.32192809488736234787031942948939018_f64;
466471
let magic_guess_scale = 1.124960491619939_f64;
467-
let initial_guess = (self.int_val.bits() as f64 - self.scale as f64 * log2_10) / 3.0;
472+
let initial_guess = (self.int_val.bits() as f64 - self.scale as f64 * LOG2_10) / 3.0;
468473
let res = magic_guess_scale * initial_guess.exp2();
469474
if res.is_normal() {
470475
BigDecimal::try_from(res).unwrap()
471476
} else {
472477
// can't guess with float - just guess magnitude
473-
let scale = (self.int_val.bits() as f64 / log2_10 - self.scale as f64).round() as i64;
478+
let scale = (self.int_val.bits() as f64 / LOG2_10 - self.scale as f64).round() as i64;
474479
BigDecimal::new(BigInt::from(1), -scale / 3)
475480
}
476481
};
@@ -530,16 +535,15 @@ impl BigDecimal {
530535
let bits = self.int_val.bits() as f64;
531536
let scale = self.scale as f64;
532537

533-
let log2_10 = 3.32192809488736234787031942948939018_f64;
534538
let magic_factor = 0.721507597259061_f64;
535-
let initial_guess = scale * log2_10 - bits;
539+
let initial_guess = scale * LOG2_10 - bits;
536540
let res = magic_factor * initial_guess.exp2();
537541

538542
if res.is_normal() {
539543
BigDecimal::try_from(res).unwrap()
540544
} else {
541545
// can't guess with float - just guess magnitude
542-
let scale = (bits / log2_10 + scale).round() as i64;
546+
let scale = (bits / LOG2_10 + scale).round() as i64;
543547
BigDecimal::new(BigInt::from(1), -scale)
544548
}
545549
};
@@ -1271,7 +1275,7 @@ fn impl_division(mut num: BigInt, den: &BigInt, mut scale: i64, max_precision: u
12711275
}
12721276

12731277
// shift digits until numerator is larger than denominator (set scale appropriately)
1274-
while &num < den {
1278+
while num < *den {
12751279
scale += 1;
12761280
num *= 10;
12771281
}
@@ -1325,7 +1329,7 @@ impl Div<BigDecimal> for BigDecimal {
13251329

13261330
let scale = self.scale - other.scale;
13271331

1328-
if &self.int_val == &other.int_val {
1332+
if self.int_val == other.int_val {
13291333
return BigDecimal {
13301334
int_val: 1.into(),
13311335
scale: scale,
@@ -1351,7 +1355,7 @@ impl<'a> Div<&'a BigDecimal> for BigDecimal {
13511355

13521356
let scale = self.scale - other.scale;
13531357

1354-
if &self.int_val == &other.int_val {
1358+
if self.int_val == other.int_val {
13551359
return BigDecimal {
13561360
int_val: 1.into(),
13571361
scale: scale,
@@ -1739,6 +1743,7 @@ macro_rules! impl_from_type {
17391743
($FromType:ty, $AsType:ty) => {
17401744
impl From<$FromType> for BigDecimal {
17411745
#[inline]
1746+
#[allow(clippy::cast_lossless)]
17421747
fn from(n: $FromType) -> Self {
17431748
BigDecimal::from(n as $AsType)
17441749
}
@@ -1972,7 +1977,7 @@ mod bigdecimal_serde {
19721977
}
19731978
}
19741979

1975-
#[cfg_attr(rustfmt, rustfmt_skip)]
1980+
#[rustfmt::skip]
19761981
#[cfg(test)]
19771982
mod bigdecimal_tests {
19781983
use BigDecimal;

src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ macro_rules! impl_div_for_float_primitive {
179179
type Output = BigDecimal;
180180

181181
#[inline]
182+
#[allow(clippy::float_cmp)]
182183
fn div(self, den: $res) -> Self::Output {
183184
if den.is_nan() {
184185
BigDecimal::zero()

0 commit comments

Comments
 (0)