Skip to content

Commit

Permalink
Allow exponent separator
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wright committed Oct 2, 2020
1 parent 8c9800a commit e91202c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
19 changes: 11 additions & 8 deletions clippy_lints/src/utils/numeric_literal.rs
Expand Up @@ -36,8 +36,9 @@ pub struct NumericLiteral<'a> {
pub integer: &'a str,
/// The fraction part of the number.
pub fraction: Option<&'a str>,
/// The character used as exponent separator (b'e' or b'E') and the exponent part.
pub exponent: Option<(char, &'a str)>,
/// The exponent separator (b'e' or b'E') including preceding underscore if present
/// and the exponent part.
pub exponent: Option<(&'a str, &'a str)>,

/// The type suffix, including preceding underscore if present.
pub suffix: Option<&'a str>,
Expand Down Expand Up @@ -100,7 +101,7 @@ impl<'a> NumericLiteral<'a> {
self.radix == Radix::Decimal
}

pub fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
pub fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(&str, &str)>) {
let mut integer = digits;
let mut fraction = None;
let mut exponent = None;
Expand All @@ -113,12 +114,14 @@ impl<'a> NumericLiteral<'a> {
fraction = Some(&digits[i + 1..]);
},
'e' | 'E' => {
if integer.len() > i {
integer = &digits[..i];
let exp_start = if digits[..i].ends_with('_') { i - 1 } else { i };

if integer.len() > exp_start {
integer = &digits[..exp_start];
} else {
fraction = Some(&digits[integer.len() + 1..i]);
fraction = Some(&digits[integer.len() + 1..exp_start]);
};
exponent = Some((c, &digits[i + 1..]));
exponent = Some((&digits[exp_start..=i], &digits[i + 1..]));
break;
},
_ => {},
Expand Down Expand Up @@ -153,7 +156,7 @@ impl<'a> NumericLiteral<'a> {
}

if let Some((separator, exponent)) = self.exponent {
output.push(separator);
output.push_str(separator);
Self::group_digits(&mut output, exponent, group_size, true, false);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/inconsistent_digit_grouping.fixed
Expand Up @@ -40,4 +40,8 @@ fn main() {
// Ignore literals in macros
let _ = mac1!();
let _ = mac2!();

// Issue #6096
// Allow separating exponent with '_'
let _ = 1.025_011_10_E0;
}
4 changes: 4 additions & 0 deletions tests/ui/inconsistent_digit_grouping.rs
Expand Up @@ -40,4 +40,8 @@ fn main() {
// Ignore literals in macros
let _ = mac1!();
let _ = mac2!();

// Issue #6096
// Allow separating exponent with '_'
let _ = 1.025_011_10_E0;
}

0 comments on commit e91202c

Please sign in to comment.