X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_utils%2Fsrc%2Fnumeric_literal.rs;h=68dd1b29845a570cc4f6b30af834414fc34d70c5;hb=03fed75c89880e5ed919eec1ba93dbe769d463a2;hp=268bc5b320533536ea5fba32dd5d7bcf8c0ce9bf;hpb=b094bb1bd7f1cc702823c91ca509f338fedee24a;p=rust.git diff --git a/clippy_utils/src/numeric_literal.rs b/clippy_utils/src/numeric_literal.rs index 268bc5b3205..68dd1b29845 100644 --- a/clippy_utils/src/numeric_literal.rs +++ b/clippy_utils/src/numeric_literal.rs @@ -52,7 +52,7 @@ pub fn from_lit(src: &'a str, lit: &Lit) -> Option> { pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option> { 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 { @@ -74,7 +74,7 @@ pub fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self { }; // Grab part of the literal after prefix, if present. - let (prefix, mut sans_prefix) = if let Radix::Decimal = radix { + let (prefix, mut sans_prefix) = if radix == Radix::Decimal { (None, lit) } else { let (p, s) = lit.split_at(2); @@ -157,11 +157,16 @@ pub fn format(&self) -> String { } if let Some((separator, exponent)) = self.exponent { - output.push_str(separator); - Self::group_digits(&mut output, exponent, group_size, true, false); + if exponent != "0" { + output.push_str(separator); + Self::group_digits(&mut output, exponent, group_size, true, false); + } } if let Some(suffix) = self.suffix { + if output.ends_with('.') { + output.push('0'); + } output.push('_'); output.push_str(suffix); } @@ -174,6 +179,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 digits.clone().next() == Some('-') { + let _ = digits.next(); + output.push('-'); + } + let first_group_size; if partial_group_first {