diff --git a/clippy_lints/src/literal_representation.rs b/clippy_lints/src/literal_representation.rs index f95a27aae34..8aa0e872968 100644 --- a/clippy_lints/src/literal_representation.rs +++ b/clippy_lints/src/literal_representation.rs @@ -402,8 +402,6 @@ impl EarlyLintPass for LiteralDigitGrouping { impl LiteralDigitGrouping { fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) { - let in_macro = in_macro(lit.span); - if_chain! { if let Some(src) = snippet_opt(cx, lit.span); if let Some(mut num_lit) = NumericLiteral::from_lit(&src, &lit); @@ -414,9 +412,9 @@ impl LiteralDigitGrouping { let result = (|| { - let integral_group_size = Self::get_group_size(num_lit.integer.split('_'), in_macro)?; + let integral_group_size = Self::get_group_size(num_lit.integer.split('_'))?; if let Some(fraction) = num_lit.fraction { - let fractional_group_size = Self::get_group_size(fraction.rsplit('_'), in_macro)?; + let fractional_group_size = Self::get_group_size(fraction.rsplit('_'))?; let consistent = Self::parts_consistent(integral_group_size, fractional_group_size, @@ -431,7 +429,19 @@ impl LiteralDigitGrouping { if let Err(warning_type) = result { - warning_type.display(num_lit.format(), cx, lit.span) + let should_warn = match warning_type { + | WarningType::UnreadableLiteral + | WarningType::InconsistentDigitGrouping + | WarningType::LargeDigitGroups => { + !in_macro(lit.span) + } + WarningType::DecimalRepresentation | WarningType::MistypedLiteralSuffix => { + true + } + }; + if should_warn { + warning_type.display(num_lit.format(), cx, lit.span) + } } } } @@ -494,7 +504,7 @@ impl LiteralDigitGrouping { /// Returns the size of the digit groups (or None if ungrouped) if successful, /// otherwise returns a `WarningType` for linting. - fn get_group_size<'a>(groups: impl Iterator, in_macro: bool) -> Result, WarningType> { + fn get_group_size<'a>(groups: impl Iterator) -> Result, WarningType> { let mut groups = groups.map(str::len); let first = groups.next().expect("At least one group"); @@ -507,7 +517,7 @@ impl LiteralDigitGrouping { } else { Ok(Some(second)) } - } else if first > 5 && !in_macro { + } else if first > 5 { Err(WarningType::UnreadableLiteral) } else { Ok(None) diff --git a/tests/ui/inconsistent_digit_grouping.fixed b/tests/ui/inconsistent_digit_grouping.fixed index 6ae93d5375a..f10673adfb2 100644 --- a/tests/ui/inconsistent_digit_grouping.fixed +++ b/tests/ui/inconsistent_digit_grouping.fixed @@ -2,6 +2,17 @@ #[warn(clippy::inconsistent_digit_grouping)] #[allow(unused_variables, clippy::excessive_precision)] fn main() { + macro_rules! mac1 { + () => { + 1_23_456 + }; + } + macro_rules! mac2 { + () => { + 1_234.5678_f32 + }; + } + let good = ( 123, 1_234, @@ -21,4 +32,8 @@ fn main() { // Test suggestion when fraction has no digits let _: f32 = 123_456.; + + // 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 c594bdc64c5..b97df0865ee 100644 --- a/tests/ui/inconsistent_digit_grouping.rs +++ b/tests/ui/inconsistent_digit_grouping.rs @@ -2,6 +2,17 @@ #[warn(clippy::inconsistent_digit_grouping)] #[allow(unused_variables, clippy::excessive_precision)] fn main() { + macro_rules! mac1 { + () => { + 1_23_456 + }; + } + macro_rules! mac2 { + () => { + 1_234.5678_f32 + }; + } + let good = ( 123, 1_234, @@ -21,4 +32,8 @@ fn main() { // Test suggestion when fraction has no digits let _: f32 = 1_23_456.; + + // Ignore literals in macros + let _ = mac1!(); + let _ = mac2!(); } diff --git a/tests/ui/inconsistent_digit_grouping.stderr b/tests/ui/inconsistent_digit_grouping.stderr index 81c53af1d14..37211efcab5 100644 --- a/tests/ui/inconsistent_digit_grouping.stderr +++ b/tests/ui/inconsistent_digit_grouping.stderr @@ -1,5 +1,5 @@ error: digits grouped inconsistently by underscores - --> $DIR/inconsistent_digit_grouping.rs:14:16 + --> $DIR/inconsistent_digit_grouping.rs:25:16 | LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32); | ^^^^^^^^ help: consider: `123_456` @@ -7,31 +7,31 @@ LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f = note: `-D clippy::inconsistent-digit-grouping` implied by `-D warnings` error: digits grouped inconsistently by underscores - --> $DIR/inconsistent_digit_grouping.rs:14:26 + --> $DIR/inconsistent_digit_grouping.rs:25:26 | LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32); | ^^^^^^^^^^ help: consider: `12_345_678` error: digits grouped inconsistently by underscores - --> $DIR/inconsistent_digit_grouping.rs:14:38 + --> $DIR/inconsistent_digit_grouping.rs:25:38 | LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32); | ^^^^^^^^ help: consider: `1_234_567` error: digits grouped inconsistently by underscores - --> $DIR/inconsistent_digit_grouping.rs:14:48 + --> $DIR/inconsistent_digit_grouping.rs:25:48 | LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32); | ^^^^^^^^^^^^^^ help: consider: `1_234.567_8_f32` error: digits grouped inconsistently by underscores - --> $DIR/inconsistent_digit_grouping.rs:14:64 + --> $DIR/inconsistent_digit_grouping.rs:25:64 | LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32); | ^^^^^^^^^^^^^^ help: consider: `1.234_567_8_f32` error: long literal lacking separators - --> $DIR/inconsistent_digit_grouping.rs:17:13 + --> $DIR/inconsistent_digit_grouping.rs:28:13 | LL | let _ = 0x100000; | ^^^^^^^^ help: consider: `0x0010_0000` @@ -39,25 +39,25 @@ LL | let _ = 0x100000; = note: `-D clippy::unreadable-literal` implied by `-D warnings` error: long literal lacking separators - --> $DIR/inconsistent_digit_grouping.rs:18:13 + --> $DIR/inconsistent_digit_grouping.rs:29:13 | LL | let _ = 0x1000000; | ^^^^^^^^^ help: consider: `0x0100_0000` error: long literal lacking separators - --> $DIR/inconsistent_digit_grouping.rs:19:13 + --> $DIR/inconsistent_digit_grouping.rs:30:13 | LL | let _ = 0x10000000; | ^^^^^^^^^^ help: consider: `0x1000_0000` error: long literal lacking separators - --> $DIR/inconsistent_digit_grouping.rs:20:13 + --> $DIR/inconsistent_digit_grouping.rs:31:13 | LL | let _ = 0x100000000_u64; | ^^^^^^^^^^^^^^^ help: consider: `0x0001_0000_0000_u64` error: digits grouped inconsistently by underscores - --> $DIR/inconsistent_digit_grouping.rs:23:18 + --> $DIR/inconsistent_digit_grouping.rs:34:18 | LL | let _: f32 = 1_23_456.; | ^^^^^^^^^ help: consider: `123_456.` diff --git a/tests/ui/large_digit_groups.fixed b/tests/ui/large_digit_groups.fixed index cf8b36a499b..02daa22bb36 100644 --- a/tests/ui/large_digit_groups.fixed +++ b/tests/ui/large_digit_groups.fixed @@ -2,6 +2,12 @@ #[warn(clippy::large_digit_groups)] #[allow(unused_variables)] fn main() { + macro_rules! mac { + () => { + 0b1_10110_i64 + }; + } + let good = ( 0b1011_i64, 0o1_234_u32, @@ -20,4 +26,7 @@ fn main() { 123_456.123_45_f64, 123_456.123_456_f64, ); + + // Ignore literals in macros + let _ = mac!(); } diff --git a/tests/ui/large_digit_groups.rs b/tests/ui/large_digit_groups.rs index 5b9aa8c58d8..c1bb78c9d83 100644 --- a/tests/ui/large_digit_groups.rs +++ b/tests/ui/large_digit_groups.rs @@ -2,6 +2,12 @@ #[warn(clippy::large_digit_groups)] #[allow(unused_variables)] fn main() { + macro_rules! mac { + () => { + 0b1_10110_i64 + }; + } + let good = ( 0b1011_i64, 0o1_234_u32, @@ -20,4 +26,7 @@ fn main() { 1_23456.12345_f64, 1_23456.12345_6_f64, ); + + // Ignore literals in macros + let _ = mac!(); } diff --git a/tests/ui/large_digit_groups.stderr b/tests/ui/large_digit_groups.stderr index 4b5d0bd1a9f..ba8ea6b53e7 100644 --- a/tests/ui/large_digit_groups.stderr +++ b/tests/ui/large_digit_groups.stderr @@ -1,5 +1,5 @@ error: digit groups should be smaller - --> $DIR/large_digit_groups.rs:16:9 + --> $DIR/large_digit_groups.rs:22:9 | LL | 0b1_10110_i64, | ^^^^^^^^^^^^^ help: consider: `0b11_0110_i64` @@ -7,31 +7,31 @@ LL | 0b1_10110_i64, = note: `-D clippy::large-digit-groups` implied by `-D warnings` error: digit groups should be smaller - --> $DIR/large_digit_groups.rs:17:9 + --> $DIR/large_digit_groups.rs:23:9 | LL | 0x1_23456_78901_usize, | ^^^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize` error: digit groups should be smaller - --> $DIR/large_digit_groups.rs:18:9 + --> $DIR/large_digit_groups.rs:24:9 | LL | 1_23456_f32, | ^^^^^^^^^^^ help: consider: `123_456_f32` error: digit groups should be smaller - --> $DIR/large_digit_groups.rs:19:9 + --> $DIR/large_digit_groups.rs:25:9 | LL | 1_23456.12_f32, | ^^^^^^^^^^^^^^ help: consider: `123_456.12_f32` error: digit groups should be smaller - --> $DIR/large_digit_groups.rs:20:9 + --> $DIR/large_digit_groups.rs:26:9 | LL | 1_23456.12345_f64, | ^^^^^^^^^^^^^^^^^ help: consider: `123_456.123_45_f64` error: digit groups should be smaller - --> $DIR/large_digit_groups.rs:21:9 + --> $DIR/large_digit_groups.rs:27:9 | LL | 1_23456.12345_6_f64, | ^^^^^^^^^^^^^^^^^^^ help: consider: `123_456.123_456_f64`