about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-11-20 18:18:29 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2024-11-23 14:17:47 +0100
commit0d4b52f772a16932e0061a21a021d0a963a546cf (patch)
tree21630e05e87ea09c1f729edffae916841722a746
parentd318878c5fb164a29bbd7c12dafd564dfec16c8b (diff)
downloadrust-0d4b52f772a16932e0061a21a021d0a963a546cf.tar.gz
rust-0d4b52f772a16932e0061a21a021d0a963a546cf.zip
Improve code by using `unsigned_abs`
-rw-r--r--library/core/src/fmt/num.rs20
1 files changed, 2 insertions, 18 deletions
diff --git a/library/core/src/fmt/num.rs b/library/core/src/fmt/num.rs
index 01b5d46a8f6..683e45b35f7 100644
--- a/library/core/src/fmt/num.rs
+++ b/library/core/src/fmt/num.rs
@@ -219,29 +219,13 @@ 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 {
-                    #[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);
-                    }
-                    #[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);
-                    }
-                }
-
-                // If it's a positive integer.
                 #[cfg(not(feature = "optimize_for_size"))]
                 {
-                    (*self as $unsigned)._fmt(true, f)
+                    return self.unsigned_abs()._fmt(*self >= 0, f);
                 }
                 #[cfg(feature = "optimize_for_size")]
                 {
-                    $gen_name(self.$conv_fn(), true, f)
+                    return $gen_name(self.unsigned_abs().$conv_fn(), *self >= 0, f);
                 }
             }
         }