Skip to content

Commit af5940b

Browse files
committed
Auto merge of rust-lang#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 rust-lang#5431 changelog: Allow UUID style formatting for `inconsistent_digit_grouping` lint
2 parents 97aa8dc + a296058 commit af5940b

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

clippy_lints/src/literal_representation.rs

+29
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ impl EarlyLintPass for LiteralDigitGrouping {
186186
}
187187
}
188188

189+
// Length of each UUID hyphenated group in hex digits.
190+
const UUID_GROUP_LENS: [usize; 5] = [8, 4, 4, 4, 12];
191+
189192
impl LiteralDigitGrouping {
190193
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
191194
if_chain! {
@@ -196,6 +199,10 @@ impl LiteralDigitGrouping {
196199
return;
197200
}
198201

202+
if Self::is_literal_uuid_formatted(&mut num_lit) {
203+
return;
204+
}
205+
199206
let result = (|| {
200207

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

276+
/// Checks whether the numeric literal matches the formatting of a UUID.
277+
///
278+
/// Returns `true` if the radix is hexadecimal, and the groups match the
279+
/// UUID format of 8-4-4-4-12.
280+
fn is_literal_uuid_formatted(num_lit: &mut NumericLiteral<'_>) -> bool {
281+
if num_lit.radix != Radix::Hexadecimal {
282+
return false;
283+
}
284+
285+
// UUIDs should not have a fraction
286+
if num_lit.fraction.is_some() {
287+
return false;
288+
}
289+
290+
let group_sizes: Vec<usize> = num_lit.integer.split('_').map(str::len).collect();
291+
if UUID_GROUP_LENS.len() == group_sizes.len() {
292+
UUID_GROUP_LENS.iter().zip(&group_sizes).all(|(&a, &b)| a == b)
293+
} else {
294+
false
295+
}
296+
}
297+
269298
/// Given the sizes of the digit groups of both integral and fractional
270299
/// parts, and the length
271300
/// of both parts, determine if the digits have been grouped consistently.

tests/ui/inconsistent_digit_grouping.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ fn main() {
3434
// Test suggestion when fraction has no digits
3535
let _: f32 = 123_456.;
3636

37+
// Test UUID formatted literal
38+
let _: u128 = 0x12345678_1234_1234_1234_123456789012;
39+
3740
// Ignore literals in macros
3841
let _ = mac1!();
3942
let _ = mac2!();

tests/ui/inconsistent_digit_grouping.rs

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ fn main() {
3434
// Test suggestion when fraction has no digits
3535
let _: f32 = 1_23_456.;
3636

37+
// Test UUID formatted literal
38+
let _: u128 = 0x12345678_1234_1234_1234_123456789012;
39+
3740
// Ignore literals in macros
3841
let _ = mac1!();
3942
let _ = mac2!();

0 commit comments

Comments
 (0)