Merge pull request #2504 from flip1995/lit_float_repr

Fix unreadable_literal lint for scientific float notation
This commit is contained in:
Oliver Schneider 2018-03-05 08:38:32 +01:00 committed by GitHub
commit f071d1994f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 56 deletions

View File

@ -134,7 +134,7 @@ impl<'a> DigitInfo<'a> {
let mut last_d = '\0';
for (d_idx, d) in sans_prefix.char_indices() {
if !float && (d == 'i' || d == 'u') || float && d == 'f' {
if !float && (d == 'i' || d == 'u') || float && (d == 'f' || d == 'e' || d == 'E') {
let suffix_start = if last_d == '_' { d_idx - 1 } else { d_idx };
let (digits, suffix) = sans_prefix.split_at(suffix_start);
return Self {
@ -285,9 +285,10 @@ impl EarlyLintPass for LiteralDigitGrouping {
impl LiteralDigitGrouping {
fn check_lit(&self, cx: &EarlyContext, lit: &Lit) {
match lit.node {
LitKind::Int(..) => {
// Lint integral literals.
if_chain! {
if let LitKind::Int(..) = lit.node;
if let Some(src) = snippet_opt(cx, lit.span);
if let Some(firstch) = src.chars().next();
if char::to_digit(firstch, 10).is_some();
@ -298,10 +299,10 @@ impl LiteralDigitGrouping {
});
}
}
},
LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
// Lint floating-point literals.
if_chain! {
if let LitKind::Float(..) = lit.node;
if let Some(src) = snippet_opt(cx, lit.span);
if let Some(firstch) = src.chars().next();
if char::to_digit(firstch, 10).is_some();
@ -340,6 +341,9 @@ impl LiteralDigitGrouping {
.map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, &lit.span));
}
}
},
_ => (),
}
}
/// Given the sizes of the digit groups of both integral and fractional

View File

@ -2,7 +2,7 @@
#[warn(approx_constant)]
#[allow(unused, shadow_unrelated, similar_names)]
#[allow(unused, shadow_unrelated, similar_names, unreadable_literal)]
fn main() {
let my_e = 2.7182;
let almost_e = 2.718;

View File

@ -5,4 +5,6 @@
fn main() {
let good = (0b1011_i64, 0o1_234_u32, 0x1_234_567, 1_2345_6789, 1234_f32, 1_234.12_f32, 1_234.123_f32, 1.123_4_f32);
let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
let good_sci = 1.1234e1;
let bad_sci = 1.12345e1;
}

View File

@ -24,5 +24,11 @@ error: long literal lacking separators
7 | let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
| ^^^^^^^^^^^ help: consider: `1.234_56_f32`
error: aborting due to 4 previous errors
error: long literal lacking separators
--> $DIR/unreadable_literal.rs:9:19
|
9 | let bad_sci = 1.12345e1;
| ^^^^^^^^^ help: consider: `1.123_45e1`
error: aborting due to 5 previous errors