From 3543f58c8449431f072316175da9dbf0a9c68ee4 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Wed, 15 May 2019 14:57:56 +0200 Subject: [PATCH] Add macro check for unreadable_literal lint Closes #4094 --- clippy_lints/src/literal_representation.rs | 13 +++++++------ tests/ui/unreadable_literal.fixed | 10 ++++++++++ tests/ui/unreadable_literal.rs | 10 ++++++++++ tests/ui/unreadable_literal.stderr | 18 +++++++++--------- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/literal_representation.rs b/clippy_lints/src/literal_representation.rs index 004e9f14b580..c2892a278d49 100644 --- a/clippy_lints/src/literal_representation.rs +++ b/clippy_lints/src/literal_representation.rs @@ -1,7 +1,7 @@ //! Lints concerned with the grouping of digits with underscores in integral or //! floating-point literal expressions. -use crate::utils::{snippet_opt, span_lint_and_sugg}; +use crate::utils::{in_macro, snippet_opt, span_lint_and_sugg}; use if_chain::if_chain; use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass}; @@ -355,6 +355,7 @@ impl EarlyLintPass for LiteralDigitGrouping { impl LiteralDigitGrouping { fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) { + let in_macro = in_macro(lit.span); match lit.node { LitKind::Int(..) => { // Lint integral literals. @@ -364,7 +365,7 @@ impl LiteralDigitGrouping { if char::to_digit(firstch, 10).is_some(); then { let digit_info = DigitInfo::new(&src, false); - let _ = Self::do_lint(digit_info.digits, digit_info.suffix).map_err(|warning_type| { + let _ = Self::do_lint(digit_info.digits, digit_info.suffix, in_macro).map_err(|warning_type| { warning_type.display(&digit_info.grouping_hint(), cx, lit.span) }); } @@ -386,12 +387,12 @@ impl LiteralDigitGrouping { // Lint integral and fractional parts separately, and then check consistency of digit // groups if both pass. - let _ = Self::do_lint(parts[0], digit_info.suffix) + let _ = Self::do_lint(parts[0], digit_info.suffix, in_macro) .map(|integral_group_size| { if parts.len() > 1 { // Lint the fractional part of literal just like integral part, but reversed. let fractional_part = &parts[1].chars().rev().collect::(); - let _ = Self::do_lint(fractional_part, None) + let _ = Self::do_lint(fractional_part, None, in_macro) .map(|fractional_group_size| { let consistent = Self::parts_consistent(integral_group_size, fractional_group_size, @@ -436,7 +437,7 @@ impl LiteralDigitGrouping { /// Performs lint on `digits` (no decimal point) and returns the group /// size on success or `WarningType` when emitting a warning. - fn do_lint(digits: &str, suffix: Option<&str>) -> Result { + fn do_lint(digits: &str, suffix: Option<&str>, in_macro: bool) -> Result { if let Some(suffix) = suffix { if is_mistyped_suffix(suffix) { return Err(WarningType::MistypedLiteralSuffix); @@ -452,7 +453,7 @@ impl LiteralDigitGrouping { if underscore_positions.is_empty() { // Check if literal needs underscores. - if digits.len() > 5 { + if !in_macro && digits.len() > 5 { Err(WarningType::UnreadableLiteral) } else { Ok(0) diff --git a/tests/ui/unreadable_literal.fixed b/tests/ui/unreadable_literal.fixed index 1fc93b9c3a8b..679b962756ca 100644 --- a/tests/ui/unreadable_literal.fixed +++ b/tests/ui/unreadable_literal.fixed @@ -1,5 +1,13 @@ // run-rustfix +struct Foo(u64); + +macro_rules! foo { + () => { + Foo(123123123123) + }; +} + #[warn(clippy::unreadable_literal)] #[allow(unused_variables)] fn main() { @@ -22,4 +30,6 @@ fn main() { let fail10: u32 = 0xBAFE_BAFE; let fail11 = 0x0abc_deff; let fail12: i128 = 0x00ab_cabc_abca_bcab_cabc; + + let _ = foo!(); } diff --git a/tests/ui/unreadable_literal.rs b/tests/ui/unreadable_literal.rs index ef0ef2b9b9e5..9922f01aa00f 100644 --- a/tests/ui/unreadable_literal.rs +++ b/tests/ui/unreadable_literal.rs @@ -1,5 +1,13 @@ // run-rustfix +struct Foo(u64); + +macro_rules! foo { + () => { + Foo(123123123123) + }; +} + #[warn(clippy::unreadable_literal)] #[allow(unused_variables)] fn main() { @@ -22,4 +30,6 @@ fn main() { let fail10: u32 = 0xBAFEBAFE; let fail11 = 0xabcdeff; let fail12: i128 = 0xabcabcabcabcabcabc; + + let _ = foo!(); } diff --git a/tests/ui/unreadable_literal.stderr b/tests/ui/unreadable_literal.stderr index 2ece53782875..a31ff75955ca 100644 --- a/tests/ui/unreadable_literal.stderr +++ b/tests/ui/unreadable_literal.stderr @@ -1,5 +1,5 @@ error: long literal lacking separators - --> $DIR/unreadable_literal.rs:17:16 + --> $DIR/unreadable_literal.rs:25:16 | LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32); | ^^^^^^^^^^^^ help: consider: `0b11_0110_i64` @@ -7,49 +7,49 @@ LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32) = note: `-D clippy::unreadable-literal` implied by `-D warnings` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:17:30 + --> $DIR/unreadable_literal.rs:25:30 | LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32); | ^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:17:51 + --> $DIR/unreadable_literal.rs:25:51 | LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32); | ^^^^^^^^^^ help: consider: `123_456_f32` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:17:63 + --> $DIR/unreadable_literal.rs:25:63 | LL | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32); | ^^^^^^^^^^^^ help: consider: `1.234_567_f32` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:19:19 + --> $DIR/unreadable_literal.rs:27:19 | LL | let bad_sci = 1.123456e1; | ^^^^^^^^^^ help: consider: `1.123_456e1` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:21:17 + --> $DIR/unreadable_literal.rs:29:17 | LL | let fail9 = 0xabcdef; | ^^^^^^^^ help: consider: `0x00ab_cdef` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:22:23 + --> $DIR/unreadable_literal.rs:30:23 | LL | let fail10: u32 = 0xBAFEBAFE; | ^^^^^^^^^^ help: consider: `0xBAFE_BAFE` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:23:18 + --> $DIR/unreadable_literal.rs:31:18 | LL | let fail11 = 0xabcdeff; | ^^^^^^^^^ help: consider: `0x0abc_deff` error: long literal lacking separators - --> $DIR/unreadable_literal.rs:24:24 + --> $DIR/unreadable_literal.rs:32:24 | LL | let fail12: i128 = 0xabcabcabcabcabcabc; | ^^^^^^^^^^^^^^^^^^^^ help: consider: `0x00ab_cabc_abca_bcab_cabc`