Skip to content

Commit 30fe4ba

Browse files
committed
Auto merge of #7722 - dtolnay-contrib:float, r=giraffate
Stop suggesting a float truncation that is not shorter Fixes #7721. Previously Clippy would say that a number has excessive precision even if it has the minimum possible precision for the floating point value that it corresponds to. changelog: Fix [`excessive_precision`] being triggered on floats that are already written in shortest form
2 parents fb61d04 + e63d692 commit 30fe4ba

File tree

3 files changed

+7
-37
lines changed

3 files changed

+7
-37
lines changed

clippy_lints/src/float_literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
111111
Applicability::MachineApplicable,
112112
);
113113
}
114-
} else if digits > max as usize && sym_str != float_str {
114+
} else if digits > max as usize && float_str.len() < sym_str.len() {
115115
span_lint_and_sugg(
116116
cx,
117117
EXCESSIVE_PRECISION,

tests/ui/excessive_precision.fixed

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ fn main() {
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_f64;
21-
const BAD64_2: f64 = 0.123_456_789_012_345_66;
20+
const BAD64_1: f64 = 0.123_456_789_012_345_67f64;
21+
const BAD64_2: f64 = 0.123_456_789_012_345_67;
2222
const BAD64_3: f64 = 0.1;
2323

2424
// Literal as param
@@ -37,9 +37,9 @@ fn main() {
3737
let bad32_suf: f32 = 1.123_456_8_f32;
3838
let bad32_inf = 1.123_456_8_f32;
3939

40-
let bad64: f64 = 0.123_456_789_012_345_66;
41-
let bad64_suf: f64 = 0.123_456_789_012_345_66_f64;
42-
let bad64_inf = 0.123_456_789_012_345_66;
40+
let bad64: f64 = 0.123_456_789_012_345_67;
41+
let bad64_suf: f64 = 0.123_456_789_012_345_67f64;
42+
let bad64_inf = 0.123_456_789_012_345_67;
4343

4444
// Vectors
4545
let good_vec32: Vec<f32> = vec![0.123_456];

tests/ui/excessive_precision.stderr

+1-31
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ error: float has excessive precision
2424
LL | const BAD32_EDGE: f32 = 1.000_000_9;
2525
| ^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.000_001`
2626

27-
error: float has excessive precision
28-
--> $DIR/excessive_precision.rs:20:26
29-
|
30-
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_f64`
32-
33-
error: float has excessive precision
34-
--> $DIR/excessive_precision.rs:21:26
35-
|
36-
LL | const BAD64_2: f64 = 0.123_456_789_012_345_67;
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
38-
3927
error: float has excessive precision
4028
--> $DIR/excessive_precision.rs:22:26
4129
|
@@ -66,24 +54,6 @@ error: float has excessive precision
6654
LL | let bad32_inf = 1.123_456_789_f32;
6755
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
6856

69-
error: float has excessive precision
70-
--> $DIR/excessive_precision.rs:40:22
71-
|
72-
LL | let bad64: f64 = 0.123_456_789_012_345_67;
73-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
74-
75-
error: float has excessive precision
76-
--> $DIR/excessive_precision.rs:41:26
77-
|
78-
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_f64`
80-
81-
error: float has excessive precision
82-
--> $DIR/excessive_precision.rs:42:21
83-
|
84-
LL | let bad64_inf = 0.123_456_789_012_345_67;
85-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
86-
8757
error: float has excessive precision
8858
--> $DIR/excessive_precision.rs:48:36
8959
|
@@ -108,5 +78,5 @@ error: float has excessive precision
10878
LL | let bad_bige32: f32 = 1.123_456_788_888E-10;
10979
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10`
11080

111-
error: aborting due to 18 previous errors
81+
error: aborting due to 13 previous errors
11282

0 commit comments

Comments
 (0)