Skip to content

Commit 4071b29

Browse files
committed
Auto merge of #4099 - flip1995:ul_4094, r=oli-obk
Add macro check for unreadable_literal lint Closes #4094 changelog: Disable `unreadable_literal` lint inside macros
2 parents f4de904 + 3543f58 commit 4071b29

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Lints concerned with the grouping of digits with underscores in integral or
22
//! floating-point literal expressions.
33
4-
use crate::utils::{snippet_opt, span_lint_and_sugg};
4+
use crate::utils::{in_macro, snippet_opt, span_lint_and_sugg};
55
use if_chain::if_chain;
66
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
77
use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
@@ -355,6 +355,7 @@ impl EarlyLintPass for LiteralDigitGrouping {
355355

356356
impl LiteralDigitGrouping {
357357
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
358+
let in_macro = in_macro(lit.span);
358359
match lit.node {
359360
LitKind::Int(..) => {
360361
// Lint integral literals.
@@ -364,7 +365,7 @@ impl LiteralDigitGrouping {
364365
if char::to_digit(firstch, 10).is_some();
365366
then {
366367
let digit_info = DigitInfo::new(&src, false);
367-
let _ = Self::do_lint(digit_info.digits, digit_info.suffix).map_err(|warning_type| {
368+
let _ = Self::do_lint(digit_info.digits, digit_info.suffix, in_macro).map_err(|warning_type| {
368369
warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
369370
});
370371
}
@@ -386,12 +387,12 @@ impl LiteralDigitGrouping {
386387

387388
// Lint integral and fractional parts separately, and then check consistency of digit
388389
// groups if both pass.
389-
let _ = Self::do_lint(parts[0], digit_info.suffix)
390+
let _ = Self::do_lint(parts[0], digit_info.suffix, in_macro)
390391
.map(|integral_group_size| {
391392
if parts.len() > 1 {
392393
// Lint the fractional part of literal just like integral part, but reversed.
393394
let fractional_part = &parts[1].chars().rev().collect::<String>();
394-
let _ = Self::do_lint(fractional_part, None)
395+
let _ = Self::do_lint(fractional_part, None, in_macro)
395396
.map(|fractional_group_size| {
396397
let consistent = Self::parts_consistent(integral_group_size,
397398
fractional_group_size,
@@ -436,7 +437,7 @@ impl LiteralDigitGrouping {
436437

437438
/// Performs lint on `digits` (no decimal point) and returns the group
438439
/// size on success or `WarningType` when emitting a warning.
439-
fn do_lint(digits: &str, suffix: Option<&str>) -> Result<usize, WarningType> {
440+
fn do_lint(digits: &str, suffix: Option<&str>, in_macro: bool) -> Result<usize, WarningType> {
440441
if let Some(suffix) = suffix {
441442
if is_mistyped_suffix(suffix) {
442443
return Err(WarningType::MistypedLiteralSuffix);
@@ -452,7 +453,7 @@ impl LiteralDigitGrouping {
452453

453454
if underscore_positions.is_empty() {
454455
// Check if literal needs underscores.
455-
if digits.len() > 5 {
456+
if !in_macro && digits.len() > 5 {
456457
Err(WarningType::UnreadableLiteral)
457458
} else {
458459
Ok(0)

tests/ui/unreadable_literal.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// run-rustfix
22

3+
struct Foo(u64);
4+
5+
macro_rules! foo {
6+
() => {
7+
Foo(123123123123)
8+
};
9+
}
10+
311
#[warn(clippy::unreadable_literal)]
412
#[allow(unused_variables)]
513
fn main() {
@@ -22,4 +30,6 @@ fn main() {
2230
let fail10: u32 = 0xBAFE_BAFE;
2331
let fail11 = 0x0abc_deff;
2432
let fail12: i128 = 0x00ab_cabc_abca_bcab_cabc;
33+
34+
let _ = foo!();
2535
}

tests/ui/unreadable_literal.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// run-rustfix
22

3+
struct Foo(u64);
4+
5+
macro_rules! foo {
6+
() => {
7+
Foo(123123123123)
8+
};
9+
}
10+
311
#[warn(clippy::unreadable_literal)]
412
#[allow(unused_variables)]
513
fn main() {
@@ -22,4 +30,6 @@ fn main() {
2230
let fail10: u32 = 0xBAFEBAFE;
2331
let fail11 = 0xabcdeff;
2432
let fail12: i128 = 0xabcabcabcabcabcabc;
33+
34+
let _ = foo!();
2535
}

tests/ui/unreadable_literal.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
error: long literal lacking separators
2-
--> $DIR/unreadable_literal.rs:17:16
2+
--> $DIR/unreadable_literal.rs:25:16
33
|
44
LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32);
55
| ^^^^^^^^^^^^ help: consider: `0b11_0110_i64`
66
|
77
= note: `-D clippy::unreadable-literal` implied by `-D warnings`
88

99
error: long literal lacking separators
10-
--> $DIR/unreadable_literal.rs:17:30
10+
--> $DIR/unreadable_literal.rs:25:30
1111
|
1212
LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32);
1313
| ^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize`
1414

1515
error: long literal lacking separators
16-
--> $DIR/unreadable_literal.rs:17:51
16+
--> $DIR/unreadable_literal.rs:25:51
1717
|
1818
LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32);
1919
| ^^^^^^^^^^ help: consider: `123_456_f32`
2020

2121
error: long literal lacking separators
22-
--> $DIR/unreadable_literal.rs:17:63
22+
--> $DIR/unreadable_literal.rs:25:63
2323
|
2424
LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32);
2525
| ^^^^^^^^^^^^ help: consider: `1.234_567_f32`
2626

2727
error: long literal lacking separators
28-
--> $DIR/unreadable_literal.rs:19:19
28+
--> $DIR/unreadable_literal.rs:27:19
2929
|
3030
LL | let bad_sci = 1.123456e1;
3131
| ^^^^^^^^^^ help: consider: `1.123_456e1`
3232

3333
error: long literal lacking separators
34-
--> $DIR/unreadable_literal.rs:21:17
34+
--> $DIR/unreadable_literal.rs:29:17
3535
|
3636
LL | let fail9 = 0xabcdef;
3737
| ^^^^^^^^ help: consider: `0x00ab_cdef`
3838

3939
error: long literal lacking separators
40-
--> $DIR/unreadable_literal.rs:22:23
40+
--> $DIR/unreadable_literal.rs:30:23
4141
|
4242
LL | let fail10: u32 = 0xBAFEBAFE;
4343
| ^^^^^^^^^^ help: consider: `0xBAFE_BAFE`
4444

4545
error: long literal lacking separators
46-
--> $DIR/unreadable_literal.rs:23:18
46+
--> $DIR/unreadable_literal.rs:31:18
4747
|
4848
LL | let fail11 = 0xabcdeff;
4949
| ^^^^^^^^^ help: consider: `0x0abc_deff`
5050

5151
error: long literal lacking separators
52-
--> $DIR/unreadable_literal.rs:24:24
52+
--> $DIR/unreadable_literal.rs:32:24
5353
|
5454
LL | let fail12: i128 = 0xabcabcabcabcabcabc;
5555
| ^^^^^^^^^^^^^^^^^^^^ help: consider: `0x00ab_cabc_abca_bcab_cabc`

0 commit comments

Comments
 (0)