]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/literal_representation.rs
clippy: support `QPath::LangItem`
[rust.git] / clippy_lints / src / literal_representation.rs
index 0a6ffc1130a306f78bc214bb83408403958205fa..a36fdca5d5de6a5816d8ff369501fad9c651b3dd 100644 (file)
     /// **Example:**
     ///
     /// ```rust
+    /// // Bad
     /// let x: u64 = 61864918973511;
+    ///
+    /// // Good
+    /// let x: u64 = 61_864_918_973_511;
     /// ```
     pub UNREADABLE_LITERAL,
     pedantic,
     /// **Example:**
     ///
     /// ```rust
+    /// // Probably mistyped
     /// 2_32;
+    ///
+    /// // Good
+    /// 2_i32;
     /// ```
     pub MISTYPED_LITERAL_SUFFIXES,
     correctness,
     /// **Example:**
     ///
     /// ```rust
+    /// // Bad
     /// let x: u64 = 618_64_9189_73_511;
+    ///
+    /// // Good
+    /// let x: u64 = 61_864_918_973_511;
     /// ```
     pub INCONSISTENT_DIGIT_GROUPING,
     style,
@@ -186,6 +198,9 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
     }
 }
 
+// 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 +211,10 @@ fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
                     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('_'))?;
@@ -245,10 +264,13 @@ fn check_for_mistyped_suffix(
 
         let (part, mistyped_suffixes, missing_char) = if let Some((_, exponent)) = &mut num_lit.exponent {
             (exponent, &["32", "64"][..], 'f')
-        } else if let Some(fraction) = &mut num_lit.fraction {
-            (fraction, &["32", "64"][..], 'f')
         } else {
-            (&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i')
+            num_lit
+                .fraction
+                .as_mut()
+                .map_or((&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i'), |fraction| {
+                    (fraction, &["32", "64"][..], 'f')
+                })
         };
 
         let mut split = part.rsplit('_');
@@ -266,6 +288,28 @@ fn check_for_mistyped_suffix(
         }
     }
 
+    /// 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.