about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDan B <xyzd@users.noreply.github.com>2020-04-12 01:24:37 +0100
committerDan B <xyzd@users.noreply.github.com>2020-04-12 01:24:37 +0100
commita296058e50b7c5b2613e6655e80243b7e33fcce9 (patch)
treeff1465ef9ad2550fa54db05e022e80f34e6e649e
parent97aa8dc778031f929a9563980b7766da0f8b9cf5 (diff)
downloadrust-a296058e50b7c5b2613e6655e80243b7e33fcce9.tar.gz
rust-a296058e50b7c5b2613e6655e80243b7e33fcce9.zip
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
-rw-r--r--clippy_lints/src/literal_representation.rs29
-rw-r--r--tests/ui/inconsistent_digit_grouping.fixed3
-rw-r--r--tests/ui/inconsistent_digit_grouping.rs3
3 files changed, 35 insertions, 0 deletions
diff --git a/clippy_lints/src/literal_representation.rs b/clippy_lints/src/literal_representation.rs
index 0a6ffc1130a..ec7c4531ed7 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<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.
diff --git a/tests/ui/inconsistent_digit_grouping.fixed b/tests/ui/inconsistent_digit_grouping.fixed
index ae4d1806af4..b75f10917df 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 a1ac21746f6..79ce38be19b 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!();