Skip to content

Commit

Permalink
Merge pull request #302 from cuviper/lints
Browse files Browse the repository at this point in the history
Fix various rustc and clippy lints
  • Loading branch information
cuviper committed May 6, 2024
2 parents 9210db6 + 8f2666d commit a6f3f5b
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 67 deletions.
10 changes: 5 additions & 5 deletions ci/big_quickcheck/src/lib.rs
Expand Up @@ -42,7 +42,7 @@ fn quickcheck_signed_eq_symmetric(a: BigInt, b: BigInt) -> bool {

#[test]
fn quickcheck_arith_primitive() {
let gen = Gen::new(usize::max_value());
let gen = Gen::new(usize::MAX);
let mut qc = QuickCheck::new().gen(gen);

fn test_unsigned_add_primitive(a: usize, b: usize) -> TestResult {
Expand Down Expand Up @@ -280,7 +280,7 @@ fn quickcheck_signed_conversion(a: BigInt, radix: u8) -> TestResult {

#[test]
fn quicktest_shift() {
let gen = Gen::new(usize::max_value());
let gen = Gen::new(usize::MAX);
let mut qc = QuickCheck::new().gen(gen);

fn test_shr_unsigned(a: u64, shift: u8) -> TestResult {
Expand Down Expand Up @@ -317,7 +317,7 @@ fn quicktest_shift() {

#[test]
fn quickcheck_modpow() {
let gen = Gen::new(usize::max_value());
let gen = Gen::new(usize::MAX);
let mut qc = QuickCheck::new().gen(gen);

fn simple_modpow(base: &BigInt, exponent: &BigInt, modulus: &BigInt) -> BigInt {
Expand Down Expand Up @@ -360,7 +360,7 @@ fn quickcheck_modpow() {

#[test]
fn quickcheck_modinv() {
let gen = Gen::new(usize::max_value());
let gen = Gen::new(usize::MAX);
let mut qc = QuickCheck::new().gen(gen);

fn test_modinv(value: i128, modulus: i128) -> TestResult {
Expand Down Expand Up @@ -417,7 +417,7 @@ fn quickcheck_modinv() {

#[test]
fn quickcheck_to_float_equals_i128_cast() {
let gen = Gen::new(usize::max_value());
let gen = Gen::new(usize::MAX);
let mut qc = QuickCheck::new().gen(gen).tests(1_000_000);

fn to_f32_equals_i128_cast(value: i128) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion ci/big_serde/src/lib.rs
Expand Up @@ -155,7 +155,7 @@ fn bad_size_hint<T: Debug + DeserializeOwned + One + PartialEq + Serialize>(
assert_tokens(&T::one(), &tokens);

tokens[prefix.len()] = Token::Seq {
len: Some(usize::max_value()),
len: Some(usize::MAX),
};

catch_unwind(|| assert_ser_tokens(&T::one(), &tokens)).unwrap_err();
Expand Down
2 changes: 0 additions & 2 deletions src/bigint.rs
Expand Up @@ -8,8 +8,6 @@ use core::fmt;
use core::hash;
use core::ops::{Neg, Not};
use core::str;
use core::{i128, u128};
use core::{i64, u64};

use num_integer::{Integer, Roots};
use num_traits::{ConstZero, Num, One, Pow, Signed, Zero};
Expand Down
4 changes: 2 additions & 2 deletions src/bigint/bits.rs
Expand Up @@ -36,7 +36,7 @@ fn negate_carry(a: BigDigit, acc: &mut DoubleBigDigit) -> BigDigit {
// + 1 & -ff = ...0 01 & ...f 01 = ...0 01 = + 1
// +ff & - 1 = ...0 ff & ...f ff = ...0 ff = +ff
// answer is pos, has length of a
fn bitand_pos_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) {
fn bitand_pos_neg(a: &mut [BigDigit], b: &[BigDigit]) {
let mut carry_b = 1;
for (ai, &bi) in a.iter_mut().zip(b.iter()) {
let twos_b = negate_carry(bi, &mut carry_b);
Expand Down Expand Up @@ -202,7 +202,7 @@ fn bitor_pos_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) {
// - 1 | +ff = ...f ff | ...0 ff = ...f ff = - 1
// -ff | + 1 = ...f 01 | ...0 01 = ...f 01 = -ff
// answer is neg, has length of a
fn bitor_neg_pos(a: &mut Vec<BigDigit>, b: &[BigDigit]) {
fn bitor_neg_pos(a: &mut [BigDigit], b: &[BigDigit]) {
let mut carry_a = 1;
let mut carry_or = 1;
for (ai, &bi) in a.iter_mut().zip(b.iter()) {
Expand Down
8 changes: 4 additions & 4 deletions src/bigint/convert.rs
Expand Up @@ -50,7 +50,7 @@ impl ToPrimitive for BigInt {
let m: u64 = 1 << 63;
match n.cmp(&m) {
Less => Some(-(n as i64)),
Equal => Some(core::i64::MIN),
Equal => Some(i64::MIN),
Greater => None,
}
}
Expand All @@ -67,7 +67,7 @@ impl ToPrimitive for BigInt {
let m: u128 = 1 << 127;
match n.cmp(&m) {
Less => Some(-(n as i128)),
Equal => Some(core::i128::MIN),
Equal => Some(i128::MIN),
Greater => None,
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ impl From<i64> for BigInt {
if n >= 0 {
BigInt::from(n as u64)
} else {
let u = core::u64::MAX - (n as u64) + 1;
let u = u64::MAX - (n as u64) + 1;
BigInt {
sign: Minus,
data: BigUint::from(u),
Expand All @@ -194,7 +194,7 @@ impl From<i128> for BigInt {
if n >= 0 {
BigInt::from(n as u128)
} else {
let u = core::u128::MAX - (n as u128) + 1;
let u = u128::MAX - (n as u128) + 1;
BigInt {
sign: Minus,
data: BigUint::from(u),
Expand Down
44 changes: 15 additions & 29 deletions src/biguint.rs
Expand Up @@ -8,7 +8,6 @@ use core::fmt;
use core::hash;
use core::mem;
use core::str;
use core::{u32, u64, u8};

use num_integer::{Integer, Roots};
use num_traits::{ConstZero, Num, One, Pow, ToPrimitive, Unsigned, Zero};
Expand Down Expand Up @@ -410,7 +409,7 @@ impl Roots for BigUint {
_ => {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2ⁿᵏ), its nth root ≈ (ⁿ√x * 2ᵏ)
let extra_bits = bits - (core::f64::MAX_EXP as u64 - 1);
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = Integer::div_ceil(&extra_bits, &n64);
let scale = root_scale * n64;
if scale < bits && bits - scale > n64 {
Expand Down Expand Up @@ -458,7 +457,7 @@ impl Roots for BigUint {
_ => {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2²ᵏ), its sqrt ≈ (√x * 2ᵏ)
let extra_bits = bits - (core::f64::MAX_EXP as u64 - 1);
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = (extra_bits + 1) / 2;
let scale = root_scale * 2;
(self >> scale).sqrt() << root_scale
Expand Down Expand Up @@ -499,7 +498,7 @@ impl Roots for BigUint {
_ => {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2³ᵏ), its cbrt ≈ (∛x * 2ᵏ)
let extra_bits = bits - (core::f64::MAX_EXP as u64 - 1);
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = (extra_bits + 2) / 3;
let scale = root_scale * 3;
(self >> scale).cbrt() << root_scale
Expand Down Expand Up @@ -1032,9 +1031,7 @@ impl BigUint {
// Note: we're saturating `digit_index` and `new_len` -- any such case is guaranteed to
// fail allocation, and that's more consistent than adding our own overflow panics.
let bits_per_digit = u64::from(big_digit::BITS);
let digit_index = (bit / bits_per_digit)
.to_usize()
.unwrap_or(core::usize::MAX);
let digit_index = (bit / bits_per_digit).to_usize().unwrap_or(usize::MAX);
let bit_mask = (1 as BigDigit) << (bit % bits_per_digit);
if value {
if digit_index >= self.data.len() {
Expand Down Expand Up @@ -1176,28 +1173,17 @@ cfg_digit!(
fn test_u32_u128() {
assert_eq!(u32_from_u128(0u128), (0, 0, 0, 0));
assert_eq!(
u32_from_u128(u128::max_value()),
(
u32::max_value(),
u32::max_value(),
u32::max_value(),
u32::max_value()
)
u32_from_u128(u128::MAX),
(u32::MAX, u32::MAX, u32::MAX, u32::MAX)
);

assert_eq!(
u32_from_u128(u32::max_value() as u128),
(0, 0, 0, u32::max_value())
);
assert_eq!(u32_from_u128(u32::MAX as u128), (0, 0, 0, u32::MAX));

assert_eq!(
u32_from_u128(u64::max_value() as u128),
(0, 0, u32::max_value(), u32::max_value())
);
assert_eq!(u32_from_u128(u64::MAX as u128), (0, 0, u32::MAX, u32::MAX));

assert_eq!(
u32_from_u128((u64::max_value() as u128) + u32::max_value() as u128),
(0, 1, 0, u32::max_value() - 1)
u32_from_u128((u64::MAX as u128) + u32::MAX as u128),
(0, 1, 0, u32::MAX - 1)
);

assert_eq!(u32_from_u128(36_893_488_151_714_070_528), (0, 2, 1, 0));
Expand All @@ -1209,11 +1195,11 @@ fn test_u128_u32_roundtrip() {
let values = vec![
0u128,
1u128,
u64::max_value() as u128 * 3,
u32::max_value() as u128,
u64::max_value() as u128,
(u64::max_value() as u128) + u32::max_value() as u128,
u128::max_value(),
u64::MAX as u128 * 3,
u32::MAX as u128,
u64::MAX as u128,
(u64::MAX as u128) + u32::MAX as u128,
u128::MAX,
];

for val in &values {
Expand Down
2 changes: 1 addition & 1 deletion src/biguint/addition.rs
Expand Up @@ -194,7 +194,7 @@ impl AddAssign<u128> for BigUint {
cfg_digit!(
#[inline]
fn add_assign(&mut self, other: u128) {
if other <= u128::from(u64::max_value()) {
if other <= u128::from(u64::MAX) {
*self += other as u64
} else {
let (a, b, c, d) = super::u32_from_u128(other);
Expand Down
20 changes: 10 additions & 10 deletions src/biguint/convert.rs
Expand Up @@ -69,7 +69,7 @@ fn from_inexact_bitwise_digits_le(v: &[u8], bits: u8) -> BigUint {
let total_bits = (v.len() as u64).saturating_mul(bits.into());
let big_digits = Integer::div_ceil(&total_bits, &big_digit::BITS.into())
.to_usize()
.unwrap_or(core::usize::MAX);
.unwrap_or(usize::MAX);
let mut data = Vec::with_capacity(big_digits);

let mut d = 0;
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Num for BigUint {
b'a'..=b'z' => b - b'a' + 10,
b'A'..=b'Z' => b - b'A' + 10,
b'_' => continue,
_ => core::u8::MAX,
_ => u8::MAX,
};
if d < radix as u8 {
v.push(d);
Expand Down Expand Up @@ -372,8 +372,8 @@ impl ToPrimitive for BigUint {
let mantissa = high_bits_to_u64(self);
let exponent = self.bits() - u64::from(fls(mantissa));

if exponent > core::f32::MAX_EXP as u64 {
Some(core::f32::INFINITY)
if exponent > f32::MAX_EXP as u64 {
Some(f32::INFINITY)
} else {
Some((mantissa as f32) * 2.0f32.powi(exponent as i32))
}
Expand All @@ -384,8 +384,8 @@ impl ToPrimitive for BigUint {
let mantissa = high_bits_to_u64(self);
let exponent = self.bits() - u64::from(fls(mantissa));

if exponent > core::f64::MAX_EXP as u64 {
Some(core::f64::INFINITY)
if exponent > f64::MAX_EXP as u64 {
Some(f64::INFINITY)
} else {
Some((mantissa as f64) * 2.0f64.powi(exponent as i32))
}
Expand Down Expand Up @@ -607,7 +607,7 @@ pub(super) fn to_bitwise_digits_le(u: &BigUint, bits: u8) -> Vec<u8> {
let digits_per_big_digit = big_digit::BITS / bits;
let digits = Integer::div_ceil(&u.bits(), &u64::from(bits))
.to_usize()
.unwrap_or(core::usize::MAX);
.unwrap_or(usize::MAX);
let mut res = Vec::with_capacity(digits);

for mut r in u.data[..last_i].iter().cloned() {
Expand All @@ -633,7 +633,7 @@ fn to_inexact_bitwise_digits_le(u: &BigUint, bits: u8) -> Vec<u8> {
let mask: BigDigit = (1 << bits) - 1;
let digits = Integer::div_ceil(&u.bits(), &u64::from(bits))
.to_usize()
.unwrap_or(core::usize::MAX);
.unwrap_or(usize::MAX);
let mut res = Vec::with_capacity(digits);

let mut r = 0;
Expand Down Expand Up @@ -845,7 +845,7 @@ fn test_radix_bases() {
for radix in 3u32..256 {
if !radix.is_power_of_two() {
let (base, power) = get_radix_base(radix);
let radix = BigDigit::try_from(radix).unwrap();
let radix = BigDigit::from(radix);
let power = u32::try_from(power).unwrap();
assert_eq!(base, radix.pow(power));
assert!(radix.checked_pow(power + 1).is_none());
Expand All @@ -858,7 +858,7 @@ fn test_half_radix_bases() {
for radix in 3u32..256 {
if !radix.is_power_of_two() {
let (base, power) = get_half_radix_base(radix);
let radix = BigDigit::try_from(radix).unwrap();
let radix = BigDigit::from(radix);
let power = u32::try_from(power).unwrap();
assert_eq!(base, radix.pow(power));
assert!(radix.pow(power + 1) > big_digit::HALF);
Expand Down
2 changes: 1 addition & 1 deletion src/biguint/shift.rs
Expand Up @@ -58,7 +58,7 @@ fn biguint_shr<T: PrimInt>(n: Cow<'_, BigUint>, shift: T) -> BigUint {
return n.into_owned();
}
let bits = T::from(big_digit::BITS).unwrap();
let digits = (shift / bits).to_usize().unwrap_or(core::usize::MAX);
let digits = (shift / bits).to_usize().unwrap_or(usize::MAX);
let shift = (shift % bits).to_u8().unwrap();
biguint_shr2(n, digits, shift)
}
Expand Down
6 changes: 3 additions & 3 deletions src/macros.rs
Expand Up @@ -2,19 +2,19 @@

macro_rules! cfg_32 {
($($any:tt)+) => {
#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "128")))] $($any)+
#[cfg(not(target_pointer_width = "64"))] $($any)+
}
}

macro_rules! cfg_32_or_test {
($($any:tt)+) => {
#[cfg(any(not(any(target_pointer_width = "64", target_pointer_width = "128")), test))] $($any)+
#[cfg(any(not(target_pointer_width = "64"), test))] $($any)+
}
}

macro_rules! cfg_64 {
($($any:tt)+) => {
#[cfg(any(target_pointer_width = "64", target_pointer_width = "128"))] $($any)+
#[cfg(target_pointer_width = "64")] $($any)+
}
}

Expand Down
3 changes: 0 additions & 3 deletions tests/bigint.rs
Expand Up @@ -8,9 +8,6 @@ use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::repeat;
use std::ops::Neg;
use std::{f32, f64};
use std::{i128, u128};
use std::{i16, i32, i64, i8, isize};
use std::{u16, u32, u64, u8, usize};

use num_integer::Integer;
use num_traits::{
Expand Down
1 change: 0 additions & 1 deletion tests/bigint_bitwise.rs
@@ -1,6 +1,5 @@
use num_bigint::{BigInt, Sign, ToBigInt};
use num_traits::ToPrimitive;
use std::{i32, i64, u32};

enum ValueVec {
N,
Expand Down
3 changes: 0 additions & 3 deletions tests/biguint.rs
Expand Up @@ -6,12 +6,9 @@ use num_integer::Integer;
use std::cmp::Ordering::{Equal, Greater, Less};
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash, Hasher};
use std::i64;
use std::iter::repeat;
use std::str::FromStr;
use std::{f32, f64};
use std::{i128, u128};
use std::{u16, u32, u64, u8, usize};

use num_traits::{
pow, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Euclid, FromBytes, FromPrimitive, Num,
Expand Down
2 changes: 1 addition & 1 deletion tests/consts/mod.rs
Expand Up @@ -16,7 +16,7 @@ pub const SUM_TRIPLES: &[(&[u32], &[u32], &[u32])] = &[
(&[1, 2, 2, 1], &[N1, N2], &[0, 1, 3, 1]),
];

pub const M: u32 = ::std::u32::MAX;
pub const M: u32 = u32::MAX;
pub const MUL_TRIPLES: &[(&[u32], &[u32], &[u32])] = &[
(&[], &[], &[]),
(&[], &[1], &[]),
Expand Down
1 change: 0 additions & 1 deletion tests/roots.rs
@@ -1,7 +1,6 @@
mod biguint {
use num_bigint::BigUint;
use num_traits::{One, Zero};
use std::{i32, u32};

fn check<T: Into<BigUint>>(x: T, n: u32) {
let x: BigUint = x.into();
Expand Down

0 comments on commit a6f3f5b

Please sign in to comment.