Skip to content

Commit 0feb3c5

Browse files
authored
Fix Clippy lints in tests (#780)
1 parent 2f1b2ef commit 0feb3c5

File tree

11 files changed

+125
-123
lines changed

11 files changed

+125
-123
lines changed

.github/workflows/crypto-bigint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ jobs:
142142
with:
143143
toolchain: stable
144144
components: clippy
145-
- run: cargo clippy --all --all-features -- -D warnings
145+
- run: cargo clippy --all-targets --all-features -- -D warnings
146146

147147
rustfmt:
148148
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/int/cmp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ mod tests {
124124
}
125125

126126
#[test]
127+
#[allow(clippy::bool_assert_comparison)]
127128
fn test_gt() {
128129
// x > y
129130
assert!(I128::MAX > I128::ONE);
@@ -141,6 +142,7 @@ mod tests {
141142
}
142143

143144
#[test]
145+
#[allow(clippy::bool_assert_comparison)]
144146
fn test_lt() {
145147
// x < y
146148
assert!(I128::ONE < I128::MAX);

src/int/div.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ mod tests {
472472
use crate::{ConstChoice, I128, Int};
473473

474474
#[test]
475+
#[allow(clippy::init_numbered_fields)]
475476
fn test_checked_div() {
476477
let min_plus_one = Int {
477478
0: I128::MIN.0.wrapping_add(&I128::ONE.0),

src/int/mul.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ mod tests {
150150
use crate::{CheckedMul, ConstChoice, I128, I256, Int, U128, U256};
151151

152152
#[test]
153+
#[allow(clippy::init_numbered_fields)]
153154
fn test_checked_mul() {
154155
let min_plus_one = Int {
155156
0: I128::MIN.0.wrapping_add(&I128::ONE.0),

src/int/mul_uint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ mod tests {
191191
assert_eq!(result.unwrap(), I128::MIN.resize());
192192

193193
// rhs > 1
194-
let result = I256::ONE.checked_mul_uint_right(&I128::MAX.as_uint());
194+
let result = I256::ONE.checked_mul_uint_right(I128::MAX.as_uint());
195195
assert!(bool::from(result.is_some()));
196196
assert_eq!(result.unwrap(), I128::MAX.resize());
197197

198-
let result = I128::ONE.checked_mul_uint_right(&I256::MAX.as_uint());
198+
let result = I128::ONE.checked_mul_uint_right(I256::MAX.as_uint());
199199
assert!(bool::from(result.is_some()));
200200
assert_eq!(result.unwrap(), I256::MAX);
201201

src/int/sub.rs

Lines changed: 96 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -75,138 +75,135 @@ impl<const LIMBS: usize> WrappingSub for Int<LIMBS> {
7575
}
7676

7777
#[cfg(test)]
78+
#[allow(clippy::init_numbered_fields)]
7879
mod tests {
80+
use num_traits::WrappingSub;
7981

80-
#[cfg(test)]
81-
mod tests {
82-
use num_traits::WrappingSub;
82+
use crate::{CheckedSub, I128, Int, U128};
8383

84-
use crate::{CheckedSub, I128, Int, U128};
84+
#[test]
85+
fn checked_sub() {
86+
let min_plus_one = Int {
87+
0: I128::MIN.0.wrapping_add(&I128::ONE.0),
88+
};
89+
let max_minus_one = Int {
90+
0: I128::MAX.0.wrapping_sub(&I128::ONE.0),
91+
};
92+
let two = Int {
93+
0: U128::from(2u32),
94+
};
95+
let min_plus_two = Int {
96+
0: I128::MIN.0.wrapping_add(&two.0),
97+
};
8598

86-
#[test]
87-
fn checked_sub() {
88-
let min_plus_one = Int {
89-
0: I128::MIN.0.wrapping_add(&I128::ONE.0),
90-
};
91-
let max_minus_one = Int {
92-
0: I128::MAX.0.wrapping_sub(&I128::ONE.0),
93-
};
94-
let two = Int {
95-
0: U128::from(2u32),
96-
};
97-
let min_plus_two = Int {
98-
0: I128::MIN.0.wrapping_add(&two.0),
99-
};
99+
// lhs = MIN
100100

101-
// lhs = MIN
101+
let result = I128::MIN.checked_sub(&I128::MIN);
102+
assert_eq!(result.unwrap(), I128::ZERO);
102103

103-
let result = I128::MIN.checked_sub(&I128::MIN);
104-
assert_eq!(result.unwrap(), I128::ZERO);
104+
let result = I128::MIN.checked_sub(&I128::MINUS_ONE);
105+
assert_eq!(result.unwrap(), min_plus_one);
105106

106-
let result = I128::MIN.checked_sub(&I128::MINUS_ONE);
107-
assert_eq!(result.unwrap(), min_plus_one);
107+
let result = I128::MIN.checked_sub(&I128::ZERO);
108+
assert_eq!(result.unwrap(), I128::MIN);
108109

109-
let result = I128::MIN.checked_sub(&I128::ZERO);
110-
assert_eq!(result.unwrap(), I128::MIN);
110+
let result = I128::MIN.checked_sub(&I128::ONE);
111+
assert!(bool::from(result.is_none()));
111112

112-
let result = I128::MIN.checked_sub(&I128::ONE);
113-
assert!(bool::from(result.is_none()));
113+
let result = I128::MIN.checked_sub(&I128::MAX);
114+
assert!(bool::from(result.is_none()));
114115

115-
let result = I128::MIN.checked_sub(&I128::MAX);
116-
assert!(bool::from(result.is_none()));
116+
// lhs = -1
117117

118-
// lhs = -1
118+
let result = I128::MINUS_ONE.checked_sub(&I128::MIN);
119+
assert_eq!(result.unwrap(), I128::MAX);
119120

120-
let result = I128::MINUS_ONE.checked_sub(&I128::MIN);
121-
assert_eq!(result.unwrap(), I128::MAX);
121+
let result = I128::MINUS_ONE.checked_sub(&I128::MINUS_ONE);
122+
assert_eq!(result.unwrap(), I128::ZERO);
122123

123-
let result = I128::MINUS_ONE.checked_sub(&I128::MINUS_ONE);
124-
assert_eq!(result.unwrap(), I128::ZERO);
124+
let result = I128::MINUS_ONE.checked_sub(&I128::ZERO);
125+
assert_eq!(result.unwrap(), I128::MINUS_ONE);
125126

126-
let result = I128::MINUS_ONE.checked_sub(&I128::ZERO);
127-
assert_eq!(result.unwrap(), I128::MINUS_ONE);
127+
let result = I128::MINUS_ONE.checked_sub(&I128::ONE);
128+
assert_eq!(result.unwrap(), two.wrapping_neg());
128129

129-
let result = I128::MINUS_ONE.checked_sub(&I128::ONE);
130-
assert_eq!(result.unwrap(), two.wrapping_neg());
130+
let result = I128::MINUS_ONE.checked_sub(&I128::MAX);
131+
assert_eq!(result.unwrap(), I128::MIN);
131132

132-
let result = I128::MINUS_ONE.checked_sub(&I128::MAX);
133-
assert_eq!(result.unwrap(), I128::MIN);
133+
// lhs = 0
134134

135-
// lhs = 0
135+
let result = I128::ZERO.checked_sub(&I128::MIN);
136+
assert!(bool::from(result.is_none()));
136137

137-
let result = I128::ZERO.checked_sub(&I128::MIN);
138-
assert!(bool::from(result.is_none()));
138+
let result = I128::ZERO.checked_sub(&I128::MINUS_ONE);
139+
assert_eq!(result.unwrap(), I128::ONE);
139140

140-
let result = I128::ZERO.checked_sub(&I128::MINUS_ONE);
141-
assert_eq!(result.unwrap(), I128::ONE);
141+
let result = I128::ZERO.checked_sub(&I128::ZERO);
142+
assert_eq!(result.unwrap(), I128::ZERO);
142143

143-
let result = I128::ZERO.checked_sub(&I128::ZERO);
144-
assert_eq!(result.unwrap(), I128::ZERO);
144+
let result = I128::ZERO.checked_sub(&I128::ONE);
145+
assert_eq!(result.unwrap(), I128::MINUS_ONE);
145146

146-
let result = I128::ZERO.checked_sub(&I128::ONE);
147-
assert_eq!(result.unwrap(), I128::MINUS_ONE);
147+
let result = I128::ZERO.checked_sub(&I128::MAX);
148+
assert_eq!(result.unwrap(), min_plus_one);
148149

149-
let result = I128::ZERO.checked_sub(&I128::MAX);
150-
assert_eq!(result.unwrap(), min_plus_one);
150+
// lhs = 1
151151

152-
// lhs = 1
152+
let result = I128::ONE.checked_sub(&I128::MIN);
153+
assert!(bool::from(result.is_none()));
153154

154-
let result = I128::ONE.checked_sub(&I128::MIN);
155-
assert!(bool::from(result.is_none()));
155+
let result = I128::ONE.checked_sub(&I128::MINUS_ONE);
156+
assert_eq!(result.unwrap(), two);
156157

157-
let result = I128::ONE.checked_sub(&I128::MINUS_ONE);
158-
assert_eq!(result.unwrap(), two);
158+
let result = I128::ONE.checked_sub(&I128::ZERO);
159+
assert_eq!(result.unwrap(), I128::ONE);
159160

160-
let result = I128::ONE.checked_sub(&I128::ZERO);
161-
assert_eq!(result.unwrap(), I128::ONE);
161+
let result = I128::ONE.checked_sub(&I128::ONE);
162+
assert_eq!(result.unwrap(), I128::ZERO);
162163

163-
let result = I128::ONE.checked_sub(&I128::ONE);
164-
assert_eq!(result.unwrap(), I128::ZERO);
164+
let result = I128::ONE.checked_sub(&I128::MAX);
165+
assert_eq!(result.unwrap(), min_plus_two);
165166

166-
let result = I128::ONE.checked_sub(&I128::MAX);
167-
assert_eq!(result.unwrap(), min_plus_two);
167+
// lhs = MAX
168168

169-
// lhs = MAX
169+
let result = I128::MAX.checked_sub(&I128::MIN);
170+
assert!(bool::from(result.is_none()));
170171

171-
let result = I128::MAX.checked_sub(&I128::MIN);
172-
assert!(bool::from(result.is_none()));
172+
let result = I128::MAX.checked_sub(&I128::MINUS_ONE);
173+
assert!(bool::from(result.is_none()));
173174

174-
let result = I128::MAX.checked_sub(&I128::MINUS_ONE);
175-
assert!(bool::from(result.is_none()));
175+
let result = I128::MAX.checked_sub(&I128::ZERO);
176+
assert_eq!(result.unwrap(), I128::MAX);
176177

177-
let result = I128::MAX.checked_sub(&I128::ZERO);
178-
assert_eq!(result.unwrap(), I128::MAX);
178+
let result = I128::MAX.checked_sub(&I128::ONE);
179+
assert_eq!(result.unwrap(), max_minus_one);
179180

180-
let result = I128::MAX.checked_sub(&I128::ONE);
181-
assert_eq!(result.unwrap(), max_minus_one);
182-
183-
let result = I128::MAX.checked_sub(&I128::MAX);
184-
assert_eq!(result.unwrap(), I128::ZERO);
185-
}
186-
187-
#[test]
188-
fn wrapping_sub() {
189-
let min_plus_one = Int {
190-
0: I128::MIN.0.wrapping_add(&I128::ONE.0),
191-
};
192-
let two = Int {
193-
0: U128::from(2u32),
194-
};
195-
let max_minus_one = Int {
196-
0: I128::MAX.0.wrapping_sub(&I128::ONE.0),
197-
};
198-
199-
// + sub -
200-
let result = I128::ONE.wrapping_sub(&I128::MIN);
201-
assert_eq!(result, min_plus_one);
202-
203-
// 0 sub -
204-
let result = I128::ZERO.wrapping_sub(&I128::MIN);
205-
assert_eq!(result, I128::MIN);
181+
let result = I128::MAX.checked_sub(&I128::MAX);
182+
assert_eq!(result.unwrap(), I128::ZERO);
183+
}
206184

207-
// - sub +
208-
let result = I128::MIN.wrapping_sub(&two);
209-
assert_eq!(result, max_minus_one);
210-
}
185+
#[test]
186+
fn wrapping_sub() {
187+
let min_plus_one = Int {
188+
0: I128::MIN.0.wrapping_add(&I128::ONE.0),
189+
};
190+
let two = Int {
191+
0: U128::from(2u32),
192+
};
193+
let max_minus_one = Int {
194+
0: I128::MAX.0.wrapping_sub(&I128::ONE.0),
195+
};
196+
197+
// + sub -
198+
let result = I128::ONE.wrapping_sub(&I128::MIN);
199+
assert_eq!(result, min_plus_one);
200+
201+
// 0 sub -
202+
let result = I128::ZERO.wrapping_sub(&I128::MIN);
203+
assert_eq!(result, I128::MIN);
204+
205+
// - sub +
206+
let result = I128::MIN.wrapping_sub(&two);
207+
assert_eq!(result, max_minus_one);
211208
}
212209
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#![deny(unsafe_code)]
99
#![warn(
1010
clippy::mod_module_files,
11-
clippy::unwrap_used,
1211
missing_docs,
1312
missing_debug_implementations,
1413
missing_copy_implementations,
@@ -17,6 +16,7 @@
1716
trivial_numeric_casts,
1817
unused_qualifications
1918
)]
19+
#![cfg_attr(not(test), warn(clippy::unwrap_used))]
2020

2121
//! ## Usage
2222
//!

src/limb/rand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl RandomMod for Limb {
2424
let mut bytes = <Self as Encoding>::Repr::default();
2525

2626
let n_bits = modulus.bits() as usize;
27-
let n_bytes = (n_bits + 7) / 8;
27+
let n_bytes = n_bits.div_ceil(8);
2828
let mask = 0xffu8 >> (8 * n_bytes - n_bits);
2929

3030
loop {

src/uint/boxed/encoding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl BoxedUint {
2222
return Ok(Self::zero());
2323
}
2424

25-
if bytes.len() > (bits_precision as usize + 7) / 8 {
25+
if bytes.len() > (bits_precision as usize).div_ceil(8) {
2626
return Err(DecodeError::InputSize);
2727
}
2828

@@ -55,7 +55,7 @@ impl BoxedUint {
5555
return Ok(Self::zero());
5656
}
5757

58-
if bytes.len() > (bits_precision as usize + 7) / 8 {
58+
if bytes.len() > (bits_precision as usize).div_ceil(8) {
5959
return Err(DecodeError::InputSize);
6060
}
6161

0 commit comments

Comments
 (0)