Auto merge of #5451 - xyzd:allow-uuid-format-digit-grouping, r=phansch
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 changelog: Allow UUID style formatting for `inconsistent_digit_grouping` lint
This commit is contained in:
commit
af5940b731
@ -186,6 +186,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 +199,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('_'))?;
|
||||
@ -266,6 +273,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.
|
||||
|
@ -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!();
|
||||
|
@ -34,6 +34,9 @@ macro_rules! mac2 {
|
||||
// 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!();
|
||||
|
Loading…
Reference in New Issue
Block a user