Skip to content

Commit 74f5e34

Browse files
committed
Auto merge of #5185 - krishna-veerareddy:issue-5160-lossy-float-literal, r=flip1995
Lint lossy whole number float literals changelog: Extend `excessive_precision` to lint lossy whole number float literals Fixes #5160
2 parents b54be5f + 45936a6 commit 74f5e34

File tree

6 files changed

+180
-81
lines changed

6 files changed

+180
-81
lines changed

clippy_lints/src/excessive_precision.rs

+57-68
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,31 @@ use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
9-
use rustc_span::symbol::Symbol;
10-
use std::f32;
11-
use std::f64;
12-
use std::fmt;
9+
use std::{f32, f64, fmt};
1310
use syntax::ast::*;
1411

1512
declare_clippy_lint! {
1613
/// **What it does:** Checks for float literals with a precision greater
17-
/// than that supported by the underlying type
14+
/// than that supported by the underlying type.
1815
///
19-
/// **Why is this bad?** Rust will truncate the literal silently.
16+
/// **Why is this bad?** Rust will silently lose precision during conversion
17+
/// to a float.
2018
///
2119
/// **Known problems:** None.
2220
///
2321
/// **Example:**
2422
///
2523
/// ```rust
2624
/// // Bad
27-
/// let v: f32 = 0.123_456_789_9;
28-
/// println!("{}", v); // 0.123_456_789
25+
/// let a: f32 = 0.123_456_789_9; // 0.123_456_789
26+
/// let b: f32 = 16_777_217.0; // 16_777_216.0
2927
///
3028
/// // Good
31-
/// let v: f64 = 0.123_456_789_9;
32-
/// println!("{}", v); // 0.123_456_789_9
29+
/// let a: f64 = 0.123_456_789_9;
30+
/// let b: f64 = 16_777_216.0;
3331
/// ```
3432
pub EXCESSIVE_PRECISION,
35-
style,
33+
correctness,
3634
"excessive precision for float literal"
3735
}
3836

@@ -44,71 +42,62 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
4442
let ty = cx.tables.expr_ty(expr);
4543
if let ty::Float(fty) = ty.kind;
4644
if let hir::ExprKind::Lit(ref lit) = expr.kind;
47-
if let LitKind::Float(sym, _) = lit.node;
48-
if let Some(sugg) = Self::check(sym, fty);
45+
if let LitKind::Float(sym, lit_float_ty) = lit.node;
4946
then {
50-
span_lint_and_sugg(
51-
cx,
52-
EXCESSIVE_PRECISION,
53-
expr.span,
54-
"float has excessive precision",
55-
"consider changing the type or truncating it to",
56-
sugg,
57-
Applicability::MachineApplicable,
58-
);
59-
}
60-
}
61-
}
62-
}
63-
64-
impl ExcessivePrecision {
65-
// None if nothing to lint, Some(suggestion) if lint necessary
66-
#[must_use]
67-
fn check(sym: Symbol, fty: FloatTy) -> Option<String> {
68-
let max = max_digits(fty);
69-
let sym_str = sym.as_str();
70-
if dot_zero_exclusion(&sym_str) {
71-
return None;
72-
}
73-
// Try to bail out if the float is for sure fine.
74-
// If its within the 2 decimal digits of being out of precision we
75-
// check if the parsed representation is the same as the string
76-
// since we'll need the truncated string anyway.
77-
let digits = count_digits(&sym_str);
78-
if digits > max as usize {
79-
let formatter = FloatFormat::new(&sym_str);
80-
let sr = match fty {
81-
FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
82-
FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
83-
};
84-
// We know this will parse since we are in LatePass
85-
let s = sr.unwrap();
47+
let sym_str = sym.as_str();
48+
let formatter = FloatFormat::new(&sym_str);
49+
// Try to bail out if the float is for sure fine.
50+
// If its within the 2 decimal digits of being out of precision we
51+
// check if the parsed representation is the same as the string
52+
// since we'll need the truncated string anyway.
53+
let digits = count_digits(&sym_str);
54+
let max = max_digits(fty);
55+
let float_str = match fty {
56+
FloatTy::F32 => sym_str.parse::<f32>().map(|f| formatter.format(f)),
57+
FloatTy::F64 => sym_str.parse::<f64>().map(|f| formatter.format(f)),
58+
}.unwrap();
59+
let type_suffix = match lit_float_ty {
60+
LitFloatType::Suffixed(FloatTy::F32) => Some("f32"),
61+
LitFloatType::Suffixed(FloatTy::F64) => Some("f64"),
62+
_ => None
63+
};
8664

87-
if sym_str == s {
88-
None
89-
} else {
90-
Some(format_numeric_literal(&s, None, true))
65+
if is_whole_number(&sym_str, fty) {
66+
// Normalize the literal by stripping the fractional portion
67+
if sym_str.split('.').next().unwrap() != float_str {
68+
span_lint_and_sugg(
69+
cx,
70+
EXCESSIVE_PRECISION,
71+
expr.span,
72+
"literal cannot be represented as the underlying type without loss of precision",
73+
"consider changing the type or replacing it with",
74+
format_numeric_literal(format!("{}.0", float_str).as_str(), type_suffix, true),
75+
Applicability::MachineApplicable,
76+
);
77+
}
78+
} else if digits > max as usize && sym_str != float_str {
79+
span_lint_and_sugg(
80+
cx,
81+
EXCESSIVE_PRECISION,
82+
expr.span,
83+
"float has excessive precision",
84+
"consider changing the type or truncating it to",
85+
format_numeric_literal(&float_str, type_suffix, true),
86+
Applicability::MachineApplicable,
87+
);
88+
}
9189
}
92-
} else {
93-
None
9490
}
9591
}
9692
}
9793

98-
/// Should we exclude the float because it has a `.0` or `.` suffix
99-
/// Ex `1_000_000_000.0`
100-
/// Ex `1_000_000_000.`
94+
// Checks whether a float literal is a whole number
10195
#[must_use]
102-
fn dot_zero_exclusion(s: &str) -> bool {
103-
s.split('.').nth(1).map_or(false, |after_dec| {
104-
let mut decpart = after_dec.chars().take_while(|c| *c != 'e' || *c != 'E');
105-
106-
match decpart.next() {
107-
Some('0') => decpart.count() == 0,
108-
Some(_) => false,
109-
None => true,
110-
}
111-
})
96+
fn is_whole_number(sym_str: &str, fty: FloatTy) -> bool {
97+
match fty {
98+
FloatTy::F32 => sym_str.parse::<f32>().unwrap().fract() == 0.0,
99+
FloatTy::F64 => sym_str.parse::<f64>().unwrap().fract() == 0.0,
100+
}
112101
}
113102

114103
#[must_use]

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13861386
LintId::of(&enum_variants::MODULE_INCEPTION),
13871387
LintId::of(&eq_op::OP_REF),
13881388
LintId::of(&eta_reduction::REDUNDANT_CLOSURE),
1389-
LintId::of(&excessive_precision::EXCESSIVE_PRECISION),
13901389
LintId::of(&formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING),
13911390
LintId::of(&formatting::SUSPICIOUS_ELSE_FORMATTING),
13921391
LintId::of(&formatting::SUSPICIOUS_UNARY_OP_FORMATTING),
@@ -1567,6 +1566,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15671566
LintId::of(&enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT),
15681567
LintId::of(&eq_op::EQ_OP),
15691568
LintId::of(&erasing_op::ERASING_OP),
1569+
LintId::of(&excessive_precision::EXCESSIVE_PRECISION),
15701570
LintId::of(&formatting::POSSIBLE_MISSING_COMMA),
15711571
LintId::of(&functions::NOT_UNSAFE_PTR_ARG_DEREF),
15721572
LintId::of(&indexing_slicing::OUT_OF_BOUNDS_INDEXING),

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub const ALL_LINTS: [Lint; 355] = [
492492
},
493493
Lint {
494494
name: "excessive_precision",
495-
group: "style",
495+
group: "correctness",
496496
desc: "excessive precision for float literal",
497497
deprecation: None,
498498
module: "excessive_precision",

tests/ui/excessive_precision.fixed

+27-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ fn main() {
1212
const GOOD64_SM: f32 = 0.000_000_000_000_000_1;
1313
const GOOD64_DOT: f32 = 10_000_000_000_000_000.0;
1414

15-
const BAD32_1: f32 = 0.123_456_79;
15+
const BAD32_1: f32 = 0.123_456_79_f32;
1616
const BAD32_2: f32 = 0.123_456_79;
1717
const BAD32_3: f32 = 0.1;
1818
const BAD32_EDGE: f32 = 1.000_001;
1919

20-
const BAD64_1: f64 = 0.123_456_789_012_345_66;
20+
const BAD64_1: f64 = 0.123_456_789_012_345_66_f64;
2121
const BAD64_2: f64 = 0.123_456_789_012_345_66;
2222
const BAD64_3: f64 = 0.1;
2323

@@ -34,11 +34,11 @@ fn main() {
3434
let good64_inf = 0.123_456_789_012;
3535

3636
let bad32: f32 = 1.123_456_8;
37-
let bad32_suf: f32 = 1.123_456_8;
38-
let bad32_inf = 1.123_456_8;
37+
let bad32_suf: f32 = 1.123_456_8_f32;
38+
let bad32_inf = 1.123_456_8_f32;
3939

4040
let bad64: f64 = 0.123_456_789_012_345_66;
41-
let bad64_suf: f64 = 0.123_456_789_012_345_66;
41+
let bad64_suf: f64 = 0.123_456_789_012_345_66_f64;
4242
let bad64_inf = 0.123_456_789_012_345_66;
4343

4444
// Vectors
@@ -60,4 +60,26 @@ fn main() {
6060

6161
// issue #2840
6262
let num = 0.000_000_000_01e-10f64;
63+
64+
// Lossy whole-number float literals
65+
let _: f32 = 16_777_216.0;
66+
let _: f32 = 16_777_220.0;
67+
let _: f32 = 16_777_220.0;
68+
let _: f32 = 16_777_220.0;
69+
let _ = 16_777_220.0_f32;
70+
let _: f32 = -16_777_220.0;
71+
let _: f64 = 9_007_199_254_740_992.0;
72+
let _: f64 = 9_007_199_254_740_992.0;
73+
let _: f64 = 9_007_199_254_740_992.0;
74+
let _ = 9_007_199_254_740_992.0_f64;
75+
let _: f64 = -9_007_199_254_740_992.0;
76+
77+
// Lossless whole number float literals
78+
let _: f32 = 16_777_216.0;
79+
let _: f32 = 16_777_218.0;
80+
let _: f32 = 16_777_220.0;
81+
let _: f32 = -16_777_216.0;
82+
let _: f32 = -16_777_220.0;
83+
let _: f64 = 9_007_199_254_740_992.0;
84+
let _: f64 = -9_007_199_254_740_992.0;
6385
}

tests/ui/excessive_precision.rs

+22
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,26 @@ fn main() {
6060

6161
// issue #2840
6262
let num = 0.000_000_000_01e-10f64;
63+
64+
// Lossy whole-number float literals
65+
let _: f32 = 16_777_217.0;
66+
let _: f32 = 16_777_219.0;
67+
let _: f32 = 16_777_219.;
68+
let _: f32 = 16_777_219.000;
69+
let _ = 16_777_219f32;
70+
let _: f32 = -16_777_219.0;
71+
let _: f64 = 9_007_199_254_740_993.0;
72+
let _: f64 = 9_007_199_254_740_993.;
73+
let _: f64 = 9_007_199_254_740_993.000;
74+
let _ = 9_007_199_254_740_993f64;
75+
let _: f64 = -9_007_199_254_740_993.0;
76+
77+
// Lossless whole number float literals
78+
let _: f32 = 16_777_216.0;
79+
let _: f32 = 16_777_218.0;
80+
let _: f32 = 16_777_220.0;
81+
let _: f32 = -16_777_216.0;
82+
let _: f32 = -16_777_220.0;
83+
let _: f64 = 9_007_199_254_740_992.0;
84+
let _: f64 = -9_007_199_254_740_992.0;
6385
}

tests/ui/excessive_precision.stderr

+72-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: float has excessive precision
22
--> $DIR/excessive_precision.rs:15:26
33
|
44
LL | const BAD32_1: f32 = 0.123_456_789_f32;
5-
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
5+
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79_f32`
66
|
77
= note: `-D clippy::excessive-precision` implied by `-D warnings`
88

@@ -28,7 +28,7 @@ error: float has excessive precision
2828
--> $DIR/excessive_precision.rs:20:26
2929
|
3030
LL | const BAD64_1: f64 = 0.123_456_789_012_345_67f64;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66_f64`
3232

3333
error: float has excessive precision
3434
--> $DIR/excessive_precision.rs:21:26
@@ -58,13 +58,13 @@ error: float has excessive precision
5858
--> $DIR/excessive_precision.rs:37:26
5959
|
6060
LL | let bad32_suf: f32 = 1.123_456_789_f32;
61-
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8`
61+
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
6262

6363
error: float has excessive precision
6464
--> $DIR/excessive_precision.rs:38:21
6565
|
6666
LL | let bad32_inf = 1.123_456_789_f32;
67-
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8`
67+
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
6868

6969
error: float has excessive precision
7070
--> $DIR/excessive_precision.rs:40:22
@@ -76,7 +76,7 @@ error: float has excessive precision
7676
--> $DIR/excessive_precision.rs:41:26
7777
|
7878
LL | let bad64_suf: f64 = 0.123_456_789_012_345_67f64;
79-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66_f64`
8080

8181
error: float has excessive precision
8282
--> $DIR/excessive_precision.rs:42:21
@@ -108,5 +108,71 @@ error: float has excessive precision
108108
LL | let bad_bige32: f32 = 1.123_456_788_888E-10;
109109
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10`
110110

111-
error: aborting due to 18 previous errors
111+
error: literal cannot be represented as the underlying type without loss of precision
112+
--> $DIR/excessive_precision.rs:65:18
113+
|
114+
LL | let _: f32 = 16_777_217.0;
115+
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_216.0`
116+
117+
error: literal cannot be represented as the underlying type without loss of precision
118+
--> $DIR/excessive_precision.rs:66:18
119+
|
120+
LL | let _: f32 = 16_777_219.0;
121+
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
122+
123+
error: literal cannot be represented as the underlying type without loss of precision
124+
--> $DIR/excessive_precision.rs:67:18
125+
|
126+
LL | let _: f32 = 16_777_219.;
127+
| ^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
128+
129+
error: literal cannot be represented as the underlying type without loss of precision
130+
--> $DIR/excessive_precision.rs:68:18
131+
|
132+
LL | let _: f32 = 16_777_219.000;
133+
| ^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
134+
135+
error: literal cannot be represented as the underlying type without loss of precision
136+
--> $DIR/excessive_precision.rs:69:13
137+
|
138+
LL | let _ = 16_777_219f32;
139+
| ^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0_f32`
140+
141+
error: literal cannot be represented as the underlying type without loss of precision
142+
--> $DIR/excessive_precision.rs:70:19
143+
|
144+
LL | let _: f32 = -16_777_219.0;
145+
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
146+
147+
error: literal cannot be represented as the underlying type without loss of precision
148+
--> $DIR/excessive_precision.rs:71:18
149+
|
150+
LL | let _: f64 = 9_007_199_254_740_993.0;
151+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
152+
153+
error: literal cannot be represented as the underlying type without loss of precision
154+
--> $DIR/excessive_precision.rs:72:18
155+
|
156+
LL | let _: f64 = 9_007_199_254_740_993.;
157+
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
158+
159+
error: literal cannot be represented as the underlying type without loss of precision
160+
--> $DIR/excessive_precision.rs:73:18
161+
|
162+
LL | let _: f64 = 9_007_199_254_740_993.000;
163+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
164+
165+
error: literal cannot be represented as the underlying type without loss of precision
166+
--> $DIR/excessive_precision.rs:74:13
167+
|
168+
LL | let _ = 9_007_199_254_740_993f64;
169+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0_f64`
170+
171+
error: literal cannot be represented as the underlying type without loss of precision
172+
--> $DIR/excessive_precision.rs:75:19
173+
|
174+
LL | let _: f64 = -9_007_199_254_740_993.0;
175+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
176+
177+
error: aborting due to 29 previous errors
112178

0 commit comments

Comments
 (0)