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
This commit is contained in:
bors 2019-05-19 08:46:43 +00:00
commit 4071b2996e
4 changed files with 36 additions and 15 deletions

View file

@ -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::<String>();
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<usize, WarningType> {
fn do_lint(digits: &str, suffix: Option<&str>, in_macro: bool) -> Result<usize, WarningType> {
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)

View file

@ -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!();
}

View file

@ -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!();
}

View file

@ -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`