From a296058e50b7c5b2613e6655e80243b7e33fcce9 Mon Sep 17 00:00:00 2001 From: Dan B Date: Sun, 12 Apr 2020 01:24:37 +0100 Subject: [PATCH] Allow UUID style formatting for `inconsistent_digit_grouping` lint 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 #5431 --- clippy_lints/src/literal_representation.rs | 29 ++++++++++++++++++++++ tests/ui/inconsistent_digit_grouping.fixed | 3 +++ tests/ui/inconsistent_digit_grouping.rs | 3 +++ 3 files changed, 35 insertions(+) diff --git a/clippy_lints/src/literal_representation.rs b/clippy_lints/src/literal_representation.rs index 0a6ffc1130a3..ec7c4531ed71 100644 --- a/clippy_lints/src/literal_representation.rs +++ b/clippy_lints/src/literal_representation.rs @@ -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! { @@ -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('_'))?; @@ -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 = 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. diff --git a/tests/ui/inconsistent_digit_grouping.fixed b/tests/ui/inconsistent_digit_grouping.fixed index ae4d1806af49..b75f10917df1 100644 --- a/tests/ui/inconsistent_digit_grouping.fixed +++ b/tests/ui/inconsistent_digit_grouping.fixed @@ -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!(); diff --git a/tests/ui/inconsistent_digit_grouping.rs b/tests/ui/inconsistent_digit_grouping.rs index a1ac21746f64..79ce38be19bd 100644 --- a/tests/ui/inconsistent_digit_grouping.rs +++ b/tests/ui/inconsistent_digit_grouping.rs @@ -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!();