Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow exponent separator #6104

Merged
merged 1 commit into from Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions clippy_lints/src/utils/numeric_literal.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}