Skip to content

Commit 75f1ff7

Browse files
Improve code by using wrapping_neg
1 parent d318878 commit 75f1ff7

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

library/core/src/fmt/num.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,14 @@ macro_rules! impl_Display {
219219
#[stable(feature = "rust1", since = "1.0.0")]
220220
impl fmt::Display for $signed {
221221
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222-
let is_nonnegative = *self >= 0;
223-
224-
if !is_nonnegative {
222+
if *self < 0 {
225223
#[cfg(not(feature = "optimize_for_size"))]
226224
{
227-
// convert the negative num to positive by summing 1 to its 2s complement
228-
return (!self as $unsigned).wrapping_add(1)._fmt(false, f);
225+
return (self.wrapping_neg() as $unsigned)._fmt(false, f);
229226
}
230227
#[cfg(feature = "optimize_for_size")]
231228
{
232-
// convert the negative num to positive by summing 1 to its 2s complement
233-
return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f);
229+
return $gen_name(self.wrapping_neg().$conv_fn(), false, f);
234230
}
235231
}
236232

0 commit comments

Comments
 (0)