Skip to content

Commit a58b980

Browse files
author
Michael Wright
committed
literal representation restructure 8
Store the digit parts directly in DigitInfo since we need them anyway.
1 parent b62543f commit a58b980

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,18 @@ impl Radix {
124124

125125
#[derive(Debug)]
126126
pub(super) struct DigitInfo<'a> {
127-
/// Characters of a literal between the radix prefix and type suffix.
128-
crate digits: &'a str,
129127
/// Which radix the literal was represented in.
130128
crate radix: Radix,
131129
/// The radix prefix, if present.
132130
crate prefix: Option<&'a str>,
131+
132+
/// The integer part of the number.
133+
integer: &'a str,
134+
/// The fraction part of the number.
135+
fraction: Option<&'a str>,
136+
/// The character used as exponent seperator (b'e' or b'E') and the exponent part.
137+
exponent: Option<(char, &'a str)>,
138+
133139
/// The type suffix, including preceding underscore if present.
134140
crate suffix: Option<&'a str>,
135141
/// True for floating-point literals.
@@ -158,6 +164,9 @@ impl<'a> DigitInfo<'a> {
158164
(Some(p), s)
159165
};
160166

167+
let mut digits = sans_prefix;
168+
let mut suffix = None;
169+
161170
let len = sans_prefix.len();
162171
let mut last_d = '\0';
163172
for (d_idx, d) in sans_prefix.char_indices() {
@@ -168,36 +177,33 @@ impl<'a> DigitInfo<'a> {
168177
|| ((d == 'E' || d == 'e') && !has_possible_float_suffix(&sans_prefix)))
169178
|| !float && (d == 'i' || d == 'u' || is_possible_suffix_index(&sans_prefix, suffix_start, len))
170179
{
171-
let (digits, suffix) = sans_prefix.split_at(suffix_start);
172-
return Self {
173-
digits,
174-
radix,
175-
prefix,
176-
suffix: Some(suffix),
177-
float,
178-
};
180+
let (d, s) = sans_prefix.split_at(suffix_start);
181+
digits = d;
182+
suffix = Some(s);
183+
break;
179184
}
180185
last_d = d
181186
}
182187

183-
// No suffix found
188+
let (integer, fraction, exponent) = Self::split_digit_parts(digits, float);
189+
184190
Self {
185-
digits: sans_prefix,
186191
radix,
187192
prefix,
188-
suffix: None,
193+
integer,
194+
fraction,
195+
exponent,
196+
suffix,
189197
float,
190198
}
191199
}
192200

193-
fn split_digit_parts(&self) -> (&str, Option<&str>, Option<(char, &str)>) {
194-
let digits = self.digits;
195-
201+
fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
196202
let mut integer = digits;
197203
let mut fraction = None;
198204
let mut exponent = None;
199205

200-
if self.float {
206+
if float {
201207
for (i, c) in digits.char_indices() {
202208
match c {
203209
'.' => {
@@ -231,17 +237,21 @@ impl<'a> DigitInfo<'a> {
231237

232238
let group_size = self.radix.suggest_grouping();
233239

234-
let (integer, fraction, exponent) = &self.split_digit_parts();
240+
Self::group_digits(
241+
&mut output,
242+
self.integer,
243+
group_size,
244+
true,
245+
self.radix == Radix::Hexadecimal,
246+
);
235247

236-
Self::group_digits(&mut output, integer, group_size, true, self.radix == Radix::Hexadecimal);
237-
238-
if let Some(fraction) = fraction {
248+
if let Some(fraction) = self.fraction {
239249
output.push('.');
240250
Self::group_digits(&mut output, fraction, group_size, false, false);
241251
}
242252

243-
if let Some((separator, exponent)) = exponent {
244-
output.push(*separator);
253+
if let Some((separator, exponent)) = self.exponent {
254+
output.push(separator);
245255
Self::group_digits(&mut output, exponent, group_size, true, false);
246256
}
247257

@@ -395,15 +405,13 @@ impl LiteralDigitGrouping {
395405
}
396406
}
397407

398-
let (integer, fraction, _) = digit_info.split_digit_parts();
399-
400-
let integral_group_size = Self::get_group_size(integer.split('_'), in_macro)?;
401-
if let Some(fraction) = fraction {
408+
let integral_group_size = Self::get_group_size(digit_info.integer.split('_'), in_macro)?;
409+
if let Some(fraction) = digit_info.fraction {
402410
let fractional_group_size = Self::get_group_size(fraction.rsplit('_'), in_macro)?;
403411

404412
let consistent = Self::parts_consistent(integral_group_size,
405413
fractional_group_size,
406-
integer.len(),
414+
digit_info.integer.len(),
407415
fraction.len());
408416
if !consistent {
409417
return Err(WarningType::InconsistentDigitGrouping);
@@ -503,7 +511,7 @@ impl DecimalLiteralRepresentation {
503511
then {
504512
let hex = format!("{:#X}", val);
505513
let digit_info = DigitInfo::new(&hex, false);
506-
let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
514+
let _ = Self::do_lint(digit_info.integer).map_err(|warning_type| {
507515
warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
508516
});
509517
}

0 commit comments

Comments
 (0)