Skip to content

Commit

Permalink
literal representation restructure 3
Browse files Browse the repository at this point in the history
Move suffix check into `check_lit` so that it isn't done repeatedly.
  • Loading branch information
Michael Wright committed Nov 13, 2019
1 parent 2dbd34f commit 2e8946a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions clippy_lints/src/literal_representation.rs
Expand Up @@ -372,9 +372,15 @@ impl LiteralDigitGrouping {
};

let result = (|| {
if let Some(suffix) = digit_info.suffix {
if is_mistyped_suffix(suffix) {
return Err(WarningType::MistypedLiteralSuffix);
}
}

match lit.kind {
LitKind::Int(..) => {
Self::do_lint(digit_info.digits, digit_info.suffix, in_macro)?;
Self::do_lint(digit_info.digits, in_macro)?;
},
LitKind::Float(..) => {
// Separate digits into integral and fractional parts.
Expand All @@ -385,11 +391,11 @@ impl LiteralDigitGrouping {

// Lint integral and fractional parts separately, and then check consistency of digit
// groups if both pass.
let integral_group_size = Self::do_lint(parts[0], digit_info.suffix, in_macro)?;
let integral_group_size = Self::do_lint(parts[0], in_macro)?;
if parts.len() > 1 {
// Lint the fractional part of literal just like integral part, but reversed.
let fractional_part = &parts[1].chars().rev().collect::<String>();
let fractional_group_size = Self::do_lint(fractional_part, None, in_macro)?;
let fractional_group_size = Self::do_lint(fractional_part, in_macro)?;
let consistent = Self::parts_consistent(integral_group_size,
fractional_group_size,
parts[0].len(),
Expand Down Expand Up @@ -432,12 +438,7 @@ impl LiteralDigitGrouping {

/// Performs lint on `digits` (no decimal point) and returns the group
/// size on success or `WarningType` when emitting a warning.
fn do_lint(digits: &str, suffix: Option<&str>, in_macro: bool) -> Result<usize, WarningType> {
if let Some(suffix) = suffix {
if is_mistyped_suffix(suffix) {
return Err(WarningType::MistypedLiteralSuffix);
}
}
fn do_lint(digits: &str, in_macro: bool) -> Result<usize, WarningType> {
// Grab underscore indices with respect to the units digit.
let underscore_positions: Vec<usize> = digits
.chars()
Expand Down

0 comments on commit 2e8946a

Please sign in to comment.