X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_utils%2Fsrc%2Fnumeric_literal.rs;h=98f65039b7da9fb2765e72d7a6f6e44bd8ab077f;hb=6bf5c0b185e37f64661e16938e18d68abd2accc4;hp=d02603d7702c7f3c6a8f7f48d30b29a027ff7e00;hpb=f64149dd04514e850374d2b94ecc91ce18d1b39e;p=rust.git diff --git a/clippy_utils/src/numeric_literal.rs b/clippy_utils/src/numeric_literal.rs index d02603d7702..98f65039b7d 100644 --- a/clippy_utils/src/numeric_literal.rs +++ b/clippy_utils/src/numeric_literal.rs @@ -1,4 +1,5 @@ use rustc_ast::ast::{Lit, LitFloatType, LitIntType, LitKind}; +use std::iter; #[derive(Debug, PartialEq, Copy, Clone)] pub enum Radix { @@ -51,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 { @@ -161,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); } @@ -173,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 { @@ -192,7 +203,7 @@ pub fn group_digits(output: &mut String, input: &str, group_size: usize, partial } } - for (c, i) in digits.zip((0..group_size).cycle()) { + for (c, i) in iter::zip(digits, (0..group_size).cycle()) { if i == 0 { output.push('_'); }