]> git.lizzy.rs Git - rust.git/blobdiff - clippy_utils/src/numeric_literal.rs
Correctly handle signs in exponents in numeric_literal::format()
[rust.git] / clippy_utils / src / numeric_literal.rs
index 268bc5b320533536ea5fba32dd5d7bcf8c0ce9bf..98f65039b7da9fb2765e72d7a6f6e44bd8ab077f 100644 (file)
@@ -52,7 +52,7 @@ pub fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
 
     pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option<NumericLiteral<'a>> {
         if lit_kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
-            let (unsuffixed, suffix) = split_suffix(&src, lit_kind);
+            let (unsuffixed, suffix) = split_suffix(src, lit_kind);
             let float = matches!(lit_kind, LitKind::Float(..));
             Some(NumericLiteral::new(unsuffixed, suffix, float))
         } else {
@@ -162,6 +162,9 @@ pub fn format(&self) -> String {
         }
 
         if let Some(suffix) = self.suffix {
+            if output.ends_with('.') {
+                output.push('0');
+            }
             output.push('_');
             output.push_str(suffix);
         }
@@ -174,6 +177,13 @@ pub fn group_digits(output: &mut String, input: &str, group_size: usize, partial
 
         let mut digits = input.chars().filter(|&c| c != '_');
 
+        // The exponent may have a sign, output it early, otherwise it will be
+        // treated as a digit
+        if let Some('-') = digits.clone().next() {
+            let _ = digits.next();
+            output.push('-');
+        }
+
         let first_group_size;
 
         if partial_group_first {