Skip to content

Commit 310ecb0

Browse files
committed
Auto merge of rust-lang#7899 - mikerite:fullint-20211030, r=xFrednet
Refactoring `FullInt` Refactoring `FullInt` changelog: None
2 parents 4c70c18 + e8c4046 commit 310ecb0

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

clippy_utils/src/consts.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -229,44 +229,35 @@ pub enum FullInt {
229229
U(u128),
230230
}
231231

232-
impl FullInt {
233-
#[allow(clippy::cast_sign_loss)]
234-
#[must_use]
235-
fn cmp_s_u(s: i128, u: u128) -> Ordering {
236-
if s < 0 {
237-
Ordering::Less
238-
} else if u > (i128::MAX as u128) {
239-
Ordering::Greater
240-
} else {
241-
(s as u128).cmp(&u)
242-
}
243-
}
244-
}
245-
246232
impl PartialEq for FullInt {
247233
#[must_use]
248234
fn eq(&self, other: &Self) -> bool {
249-
self.partial_cmp(other).expect("`partial_cmp` only returns `Some(_)`") == Ordering::Equal
235+
self.cmp(other) == Ordering::Equal
250236
}
251237
}
252238

253239
impl PartialOrd for FullInt {
254240
#[must_use]
255241
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
256-
Some(match (self, other) {
257-
(&Self::S(s), &Self::S(o)) => s.cmp(&o),
258-
(&Self::U(s), &Self::U(o)) => s.cmp(&o),
259-
(&Self::S(s), &Self::U(o)) => Self::cmp_s_u(s, o),
260-
(&Self::U(s), &Self::S(o)) => Self::cmp_s_u(o, s).reverse(),
261-
})
242+
Some(self.cmp(other))
262243
}
263244
}
264245

265246
impl Ord for FullInt {
266247
#[must_use]
267248
fn cmp(&self, other: &Self) -> Ordering {
268-
self.partial_cmp(other)
269-
.expect("`partial_cmp` for FullInt can never return `None`")
249+
use FullInt::{S, U};
250+
251+
fn cmp_s_u(s: i128, u: u128) -> Ordering {
252+
u128::try_from(s).map_or(Ordering::Less, |x| x.cmp(&u))
253+
}
254+
255+
match (*self, *other) {
256+
(S(s), S(o)) => s.cmp(&o),
257+
(U(s), U(o)) => s.cmp(&o),
258+
(S(s), U(o)) => cmp_s_u(s, o),
259+
(U(s), S(o)) => cmp_s_u(o, s).reverse(),
260+
}
270261
}
271262
}
272263

0 commit comments

Comments
 (0)