Skip to content

Commit 5524528

Browse files
Add tests for lossy whole number float literals
1 parent 0532f3e commit 5524528

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

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)