Skip to content

Commit f0eac45

Browse files
committed
Auto merge of #6104 - mikerite:fix-6096, r=ebroto
Allow exponent separator Fixes #6096 changelog: Fixed bug in `clippy::inconsistent_digit_grouping` when floating point exponent is used
2 parents 8c9800a + e91202c commit f0eac45

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

clippy_lints/src/utils/numeric_literal.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ pub struct NumericLiteral<'a> {
3636
pub integer: &'a str,
3737
/// The fraction part of the number.
3838
pub fraction: Option<&'a str>,
39-
/// The character used as exponent separator (b'e' or b'E') and the exponent part.
40-
pub exponent: Option<(char, &'a str)>,
39+
/// The exponent separator (b'e' or b'E') including preceding underscore if present
40+
/// and the exponent part.
41+
pub exponent: Option<(&'a str, &'a str)>,
4142

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

103-
pub fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
104+
pub fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(&str, &str)>) {
104105
let mut integer = digits;
105106
let mut fraction = None;
106107
let mut exponent = None;
@@ -113,12 +114,14 @@ impl<'a> NumericLiteral<'a> {
113114
fraction = Some(&digits[i + 1..]);
114115
},
115116
'e' | 'E' => {
116-
if integer.len() > i {
117-
integer = &digits[..i];
117+
let exp_start = if digits[..i].ends_with('_') { i - 1 } else { i };
118+
119+
if integer.len() > exp_start {
120+
integer = &digits[..exp_start];
118121
} else {
119-
fraction = Some(&digits[integer.len() + 1..i]);
122+
fraction = Some(&digits[integer.len() + 1..exp_start]);
120123
};
121-
exponent = Some((c, &digits[i + 1..]));
124+
exponent = Some((&digits[exp_start..=i], &digits[i + 1..]));
122125
break;
123126
},
124127
_ => {},
@@ -153,7 +156,7 @@ impl<'a> NumericLiteral<'a> {
153156
}
154157

155158
if let Some((separator, exponent)) = self.exponent {
156-
output.push(separator);
159+
output.push_str(separator);
157160
Self::group_digits(&mut output, exponent, group_size, true, false);
158161
}
159162

tests/ui/inconsistent_digit_grouping.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ fn main() {
4040
// Ignore literals in macros
4141
let _ = mac1!();
4242
let _ = mac2!();
43+
44+
// Issue #6096
45+
// Allow separating exponent with '_'
46+
let _ = 1.025_011_10_E0;
4347
}

tests/ui/inconsistent_digit_grouping.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ fn main() {
4040
// Ignore literals in macros
4141
let _ = mac1!();
4242
let _ = mac2!();
43+
44+
// Issue #6096
45+
// Allow separating exponent with '_'
46+
let _ = 1.025_011_10_E0;
4347
}

0 commit comments

Comments
 (0)