Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macro check for unreadable_literal lint #4099

Merged
merged 1 commit into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions clippy_lints/src/literal_representation.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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.
Expand All @@ -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)
});
}
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/unreadable_literal.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// run-rustfix

struct Foo(u64);

macro_rules! foo {
() => {
Foo(123123123123)
};
}

#[warn(clippy::unreadable_literal)]
#[allow(unused_variables)]
fn main() {
Expand All @@ -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!();
}
10 changes: 10 additions & 0 deletions tests/ui/unreadable_literal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// run-rustfix

struct Foo(u64);

macro_rules! foo {
() => {
Foo(123123123123)
};
}

#[warn(clippy::unreadable_literal)]
#[allow(unused_variables)]
fn main() {
Expand All @@ -22,4 +30,6 @@ fn main() {
let fail10: u32 = 0xBAFEBAFE;
let fail11 = 0xabcdeff;
let fail12: i128 = 0xabcabcabcabcabcabc;

let _ = foo!();
}
18 changes: 9 additions & 9 deletions tests/ui/unreadable_literal.stderr
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
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`
|
= 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`
Expand Down