Skip to content

Commit ceb0b2d

Browse files
author
Michael Wright
committed
literal repr: ignore more warnings in macros
1 parent 75e2dcf commit ceb0b2d

7 files changed

+81
-23
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,6 @@ impl EarlyLintPass for LiteralDigitGrouping {
402402

403403
impl LiteralDigitGrouping {
404404
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
405-
let in_macro = in_macro(lit.span);
406-
407405
if_chain! {
408406
if let Some(src) = snippet_opt(cx, lit.span);
409407
if let Some(mut num_lit) = NumericLiteral::from_lit(&src, &lit);
@@ -414,9 +412,9 @@ impl LiteralDigitGrouping {
414412

415413
let result = (|| {
416414

417-
let integral_group_size = Self::get_group_size(num_lit.integer.split('_'), in_macro)?;
415+
let integral_group_size = Self::get_group_size(num_lit.integer.split('_'))?;
418416
if let Some(fraction) = num_lit.fraction {
419-
let fractional_group_size = Self::get_group_size(fraction.rsplit('_'), in_macro)?;
417+
let fractional_group_size = Self::get_group_size(fraction.rsplit('_'))?;
420418

421419
let consistent = Self::parts_consistent(integral_group_size,
422420
fractional_group_size,
@@ -431,7 +429,19 @@ impl LiteralDigitGrouping {
431429

432430

433431
if let Err(warning_type) = result {
434-
warning_type.display(num_lit.format(), cx, lit.span)
432+
let should_warn = match warning_type {
433+
| WarningType::UnreadableLiteral
434+
| WarningType::InconsistentDigitGrouping
435+
| WarningType::LargeDigitGroups => {
436+
!in_macro(lit.span)
437+
}
438+
WarningType::DecimalRepresentation | WarningType::MistypedLiteralSuffix => {
439+
true
440+
}
441+
};
442+
if should_warn {
443+
warning_type.display(num_lit.format(), cx, lit.span)
444+
}
435445
}
436446
}
437447
}
@@ -494,7 +504,7 @@ impl LiteralDigitGrouping {
494504

495505
/// Returns the size of the digit groups (or None if ungrouped) if successful,
496506
/// otherwise returns a `WarningType` for linting.
497-
fn get_group_size<'a>(groups: impl Iterator<Item = &'a str>, in_macro: bool) -> Result<Option<usize>, WarningType> {
507+
fn get_group_size<'a>(groups: impl Iterator<Item = &'a str>) -> Result<Option<usize>, WarningType> {
498508
let mut groups = groups.map(str::len);
499509

500510
let first = groups.next().expect("At least one group");
@@ -507,7 +517,7 @@ impl LiteralDigitGrouping {
507517
} else {
508518
Ok(Some(second))
509519
}
510-
} else if first > 5 && !in_macro {
520+
} else if first > 5 {
511521
Err(WarningType::UnreadableLiteral)
512522
} else {
513523
Ok(None)

tests/ui/inconsistent_digit_grouping.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#[warn(clippy::inconsistent_digit_grouping)]
33
#[allow(unused_variables, clippy::excessive_precision)]
44
fn main() {
5+
macro_rules! mac1 {
6+
() => {
7+
1_23_456
8+
};
9+
}
10+
macro_rules! mac2 {
11+
() => {
12+
1_234.5678_f32
13+
};
14+
}
15+
516
let good = (
617
123,
718
1_234,
@@ -21,4 +32,8 @@ fn main() {
2132

2233
// Test suggestion when fraction has no digits
2334
let _: f32 = 123_456.;
35+
36+
// Ignore literals in macros
37+
let _ = mac1!();
38+
let _ = mac2!();
2439
}

tests/ui/inconsistent_digit_grouping.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#[warn(clippy::inconsistent_digit_grouping)]
33
#[allow(unused_variables, clippy::excessive_precision)]
44
fn main() {
5+
macro_rules! mac1 {
6+
() => {
7+
1_23_456
8+
};
9+
}
10+
macro_rules! mac2 {
11+
() => {
12+
1_234.5678_f32
13+
};
14+
}
15+
516
let good = (
617
123,
718
1_234,
@@ -21,4 +32,8 @@ fn main() {
2132

2233
// Test suggestion when fraction has no digits
2334
let _: f32 = 1_23_456.;
35+
36+
// Ignore literals in macros
37+
let _ = mac1!();
38+
let _ = mac2!();
2439
}

tests/ui/inconsistent_digit_grouping.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
11
error: digits grouped inconsistently by underscores
2-
--> $DIR/inconsistent_digit_grouping.rs:14:16
2+
--> $DIR/inconsistent_digit_grouping.rs:25:16
33
|
44
LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32);
55
| ^^^^^^^^ help: consider: `123_456`
66
|
77
= note: `-D clippy::inconsistent-digit-grouping` implied by `-D warnings`
88

99
error: digits grouped inconsistently by underscores
10-
--> $DIR/inconsistent_digit_grouping.rs:14:26
10+
--> $DIR/inconsistent_digit_grouping.rs:25:26
1111
|
1212
LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32);
1313
| ^^^^^^^^^^ help: consider: `12_345_678`
1414

1515
error: digits grouped inconsistently by underscores
16-
--> $DIR/inconsistent_digit_grouping.rs:14:38
16+
--> $DIR/inconsistent_digit_grouping.rs:25:38
1717
|
1818
LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32);
1919
| ^^^^^^^^ help: consider: `1_234_567`
2020

2121
error: digits grouped inconsistently by underscores
22-
--> $DIR/inconsistent_digit_grouping.rs:14:48
22+
--> $DIR/inconsistent_digit_grouping.rs:25:48
2323
|
2424
LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32);
2525
| ^^^^^^^^^^^^^^ help: consider: `1_234.567_8_f32`
2626

2727
error: digits grouped inconsistently by underscores
28-
--> $DIR/inconsistent_digit_grouping.rs:14:64
28+
--> $DIR/inconsistent_digit_grouping.rs:25:64
2929
|
3030
LL | let bad = (1_23_456, 1_234_5678, 1234_567, 1_234.5678_f32, 1.234_5678_f32);
3131
| ^^^^^^^^^^^^^^ help: consider: `1.234_567_8_f32`
3232

3333
error: long literal lacking separators
34-
--> $DIR/inconsistent_digit_grouping.rs:17:13
34+
--> $DIR/inconsistent_digit_grouping.rs:28:13
3535
|
3636
LL | let _ = 0x100000;
3737
| ^^^^^^^^ help: consider: `0x0010_0000`
3838
|
3939
= note: `-D clippy::unreadable-literal` implied by `-D warnings`
4040

4141
error: long literal lacking separators
42-
--> $DIR/inconsistent_digit_grouping.rs:18:13
42+
--> $DIR/inconsistent_digit_grouping.rs:29:13
4343
|
4444
LL | let _ = 0x1000000;
4545
| ^^^^^^^^^ help: consider: `0x0100_0000`
4646

4747
error: long literal lacking separators
48-
--> $DIR/inconsistent_digit_grouping.rs:19:13
48+
--> $DIR/inconsistent_digit_grouping.rs:30:13
4949
|
5050
LL | let _ = 0x10000000;
5151
| ^^^^^^^^^^ help: consider: `0x1000_0000`
5252

5353
error: long literal lacking separators
54-
--> $DIR/inconsistent_digit_grouping.rs:20:13
54+
--> $DIR/inconsistent_digit_grouping.rs:31:13
5555
|
5656
LL | let _ = 0x100000000_u64;
5757
| ^^^^^^^^^^^^^^^ help: consider: `0x0001_0000_0000_u64`
5858

5959
error: digits grouped inconsistently by underscores
60-
--> $DIR/inconsistent_digit_grouping.rs:23:18
60+
--> $DIR/inconsistent_digit_grouping.rs:34:18
6161
|
6262
LL | let _: f32 = 1_23_456.;
6363
| ^^^^^^^^^ help: consider: `123_456.`

tests/ui/large_digit_groups.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#[warn(clippy::large_digit_groups)]
33
#[allow(unused_variables)]
44
fn main() {
5+
macro_rules! mac {
6+
() => {
7+
0b1_10110_i64
8+
};
9+
}
10+
511
let good = (
612
0b1011_i64,
713
0o1_234_u32,
@@ -20,4 +26,7 @@ fn main() {
2026
123_456.123_45_f64,
2127
123_456.123_456_f64,
2228
);
29+
30+
// Ignore literals in macros
31+
let _ = mac!();
2332
}

tests/ui/large_digit_groups.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#[warn(clippy::large_digit_groups)]
33
#[allow(unused_variables)]
44
fn main() {
5+
macro_rules! mac {
6+
() => {
7+
0b1_10110_i64
8+
};
9+
}
10+
511
let good = (
612
0b1011_i64,
713
0o1_234_u32,
@@ -20,4 +26,7 @@ fn main() {
2026
1_23456.12345_f64,
2127
1_23456.12345_6_f64,
2228
);
29+
30+
// Ignore literals in macros
31+
let _ = mac!();
2332
}

tests/ui/large_digit_groups.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
error: digit groups should be smaller
2-
--> $DIR/large_digit_groups.rs:16:9
2+
--> $DIR/large_digit_groups.rs:22:9
33
|
44
LL | 0b1_10110_i64,
55
| ^^^^^^^^^^^^^ help: consider: `0b11_0110_i64`
66
|
77
= note: `-D clippy::large-digit-groups` implied by `-D warnings`
88

99
error: digit groups should be smaller
10-
--> $DIR/large_digit_groups.rs:17:9
10+
--> $DIR/large_digit_groups.rs:23:9
1111
|
1212
LL | 0x1_23456_78901_usize,
1313
| ^^^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize`
1414

1515
error: digit groups should be smaller
16-
--> $DIR/large_digit_groups.rs:18:9
16+
--> $DIR/large_digit_groups.rs:24:9
1717
|
1818
LL | 1_23456_f32,
1919
| ^^^^^^^^^^^ help: consider: `123_456_f32`
2020

2121
error: digit groups should be smaller
22-
--> $DIR/large_digit_groups.rs:19:9
22+
--> $DIR/large_digit_groups.rs:25:9
2323
|
2424
LL | 1_23456.12_f32,
2525
| ^^^^^^^^^^^^^^ help: consider: `123_456.12_f32`
2626

2727
error: digit groups should be smaller
28-
--> $DIR/large_digit_groups.rs:20:9
28+
--> $DIR/large_digit_groups.rs:26:9
2929
|
3030
LL | 1_23456.12345_f64,
3131
| ^^^^^^^^^^^^^^^^^ help: consider: `123_456.123_45_f64`
3232

3333
error: digit groups should be smaller
34-
--> $DIR/large_digit_groups.rs:21:9
34+
--> $DIR/large_digit_groups.rs:27:9
3535
|
3636
LL | 1_23456.12345_6_f64,
3737
| ^^^^^^^^^^^^^^^^^^^ help: consider: `123_456.123_456_f64`

0 commit comments

Comments
 (0)