Skip to content

Commit

Permalink
Allow UUID style formatting for inconsistent_digit_grouping lint
Browse files Browse the repository at this point in the history
This change adds a check to the `inconsistent_digit_grouping` to add a check for
NumericLiterals that follow the UUID format of 8-4-4-4-12.

If the NumericLiteral matches the UUID format, no further inconsistent grouping checks
will be performed.

Closes rust-lang#5431
  • Loading branch information
xyzd committed Apr 12, 2020
1 parent 97aa8dc commit a296058
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions clippy_lints/src/literal_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ impl EarlyLintPass for LiteralDigitGrouping {
}
}

// Length of each UUID hyphenated group in hex digits.
const UUID_GROUP_LENS: [usize; 5] = [8, 4, 4, 4, 12];

impl LiteralDigitGrouping {
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
if_chain! {
Expand All @@ -196,6 +199,10 @@ impl LiteralDigitGrouping {
return;
}

if Self::is_literal_uuid_formatted(&mut num_lit) {
return;
}

let result = (|| {

let integral_group_size = Self::get_group_size(num_lit.integer.split('_'))?;
Expand Down Expand Up @@ -266,6 +273,28 @@ impl LiteralDigitGrouping {
}
}

/// Checks whether the numeric literal matches the formatting of a UUID.
///
/// Returns `true` if the radix is hexadecimal, and the groups match the
/// UUID format of 8-4-4-4-12.
fn is_literal_uuid_formatted(num_lit: &mut NumericLiteral<'_>) -> bool {
if num_lit.radix != Radix::Hexadecimal {
return false;
}

// UUIDs should not have a fraction
if num_lit.fraction.is_some() {
return false;
}

let group_sizes: Vec<usize> = num_lit.integer.split('_').map(str::len).collect();
if UUID_GROUP_LENS.len() == group_sizes.len() {
UUID_GROUP_LENS.iter().zip(&group_sizes).all(|(&a, &b)| a == b)
} else {
false
}
}

/// Given the sizes of the digit groups of both integral and fractional
/// parts, and the length
/// of both parts, determine if the digits have been grouped consistently.
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/inconsistent_digit_grouping.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ fn main() {
// Test suggestion when fraction has no digits
let _: f32 = 123_456.;

// Test UUID formatted literal
let _: u128 = 0x12345678_1234_1234_1234_123456789012;

// Ignore literals in macros
let _ = mac1!();
let _ = mac2!();
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/inconsistent_digit_grouping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ fn main() {
// Test suggestion when fraction has no digits
let _: f32 = 1_23_456.;

// Test UUID formatted literal
let _: u128 = 0x12345678_1234_1234_1234_123456789012;

// Ignore literals in macros
let _ = mac1!();
let _ = mac2!();
Expand Down

0 comments on commit a296058

Please sign in to comment.