From 75f1ff7e5f9e536ff846cf15e48b7a3ebb471242 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 20 Nov 2024 18:18:29 +0100 Subject: [PATCH] Improve code by using `wrapping_neg` --- library/core/src/fmt/num.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs index 01b5d46a8f696..ab7d6a9527223 100644 --- a/library/core/src/fmt/num.rs +++ b/library/core/src/fmt/num.rs @@ -219,18 +219,14 @@ macro_rules! impl_Display { #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for $signed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let is_nonnegative = *self >= 0; - - if !is_nonnegative { + if *self < 0 { #[cfg(not(feature = "optimize_for_size"))] { - // convert the negative num to positive by summing 1 to its 2s complement - return (!self as $unsigned).wrapping_add(1)._fmt(false, f); + return (self.wrapping_neg() as $unsigned)._fmt(false, f); } #[cfg(feature = "optimize_for_size")] { - // convert the negative num to positive by summing 1 to its 2s complement - return $gen_name((!self.$conv_fn()).wrapping_add(1), false, f); + return $gen_name(self.wrapping_neg().$conv_fn(), false, f); } }