Skip to content

Commit 32021c8

Browse files
committed
Support f16 in lossy_float_literal lint
1 parent f80861d commit 32021c8

File tree

4 files changed

+95
-20
lines changed

4 files changed

+95
-20
lines changed

clippy_lints/src/float_literal.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
9999
LitFloatType::Unsuffixed => None,
100100
};
101101
let (is_whole, is_inf, mut float_str) = match fty {
102-
FloatTy::F16 | FloatTy::F128 => {
102+
FloatTy::F128 => {
103103
// FIXME(f16_f128): do a check like the others when parsing is available
104104
return;
105105
},
106+
FloatTy::F16 => {
107+
let value = sym_str.parse::<f16>().unwrap();
108+
109+
(value.fract() == 0.0, value.is_infinite(), formatter.format(value))
110+
},
106111
FloatTy::F32 => {
107112
let value = sym_str.parse::<f32>().unwrap();
108113

tests/ui/lossy_float_literal.fixed

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55

66
fn main() {
77
// Lossy whole-number float literals
8-
let _: f16 = 4_097.0;
9-
let _: f16 = 4_097.;
10-
let _: f16 = 4_097.000;
11-
let _ = 4_097f16;
12-
let _: f16 = -4_097.0;
8+
let _: f16 = 4_096.0;
9+
//~^ lossy_float_literal
10+
let _: f16 = 4_096.0;
11+
//~^ lossy_float_literal
12+
let _: f16 = 4_096.0;
13+
//~^ lossy_float_literal
14+
let _ = 4_096_f16;
15+
//~^ lossy_float_literal
16+
let _: f16 = -4_096.0;
17+
//~^ lossy_float_literal
1318

1419
let _: f32 = 16_777_216.0;
1520
//~^ lossy_float_literal

tests/ui/lossy_float_literal.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
fn main() {
77
// Lossy whole-number float literals
88
let _: f16 = 4_097.0;
9+
//~^ lossy_float_literal
910
let _: f16 = 4_097.;
11+
//~^ lossy_float_literal
1012
let _: f16 = 4_097.000;
13+
//~^ lossy_float_literal
1114
let _ = 4_097f16;
15+
//~^ lossy_float_literal
1216
let _: f16 = -4_097.0;
17+
//~^ lossy_float_literal
1318

1419
let _: f32 = 16_777_217.0;
1520
//~^ lossy_float_literal

tests/ui/lossy_float_literal.stderr

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,79 @@
11
error: literal cannot be represented as the underlying type without loss of precision
2-
--> tests/ui/lossy_float_literal.rs:14:18
2+
--> tests/ui/lossy_float_literal.rs:8:18
33
|
4-
LL | let _: f32 = 16_777_217.0;
5-
| ^^^^^^^^^^^^
4+
LL | let _: f16 = 4_097.0;
5+
| ^^^^^^^
66
|
77
= note: `-D clippy::lossy-float-literal` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::lossy_float_literal)]`
99
help: consider changing the type or replacing it with
1010
|
11+
LL - let _: f16 = 4_097.0;
12+
LL + let _: f16 = 4_096.0;
13+
|
14+
15+
error: literal cannot be represented as the underlying type without loss of precision
16+
--> tests/ui/lossy_float_literal.rs:10:18
17+
|
18+
LL | let _: f16 = 4_097.;
19+
| ^^^^^^
20+
|
21+
help: consider changing the type or replacing it with
22+
|
23+
LL - let _: f16 = 4_097.;
24+
LL + let _: f16 = 4_096.0;
25+
|
26+
27+
error: literal cannot be represented as the underlying type without loss of precision
28+
--> tests/ui/lossy_float_literal.rs:12:18
29+
|
30+
LL | let _: f16 = 4_097.000;
31+
| ^^^^^^^^^
32+
|
33+
help: consider changing the type or replacing it with
34+
|
35+
LL - let _: f16 = 4_097.000;
36+
LL + let _: f16 = 4_096.0;
37+
|
38+
39+
error: literal cannot be represented as the underlying type without loss of precision
40+
--> tests/ui/lossy_float_literal.rs:14:13
41+
|
42+
LL | let _ = 4_097f16;
43+
| ^^^^^^^^
44+
|
45+
help: consider changing the type or replacing it with
46+
|
47+
LL - let _ = 4_097f16;
48+
LL + let _ = 4_096_f16;
49+
|
50+
51+
error: literal cannot be represented as the underlying type without loss of precision
52+
--> tests/ui/lossy_float_literal.rs:16:19
53+
|
54+
LL | let _: f16 = -4_097.0;
55+
| ^^^^^^^
56+
|
57+
help: consider changing the type or replacing it with
58+
|
59+
LL - let _: f16 = -4_097.0;
60+
LL + let _: f16 = -4_096.0;
61+
|
62+
63+
error: literal cannot be represented as the underlying type without loss of precision
64+
--> tests/ui/lossy_float_literal.rs:19:18
65+
|
66+
LL | let _: f32 = 16_777_217.0;
67+
| ^^^^^^^^^^^^
68+
|
69+
help: consider changing the type or replacing it with
70+
|
1171
LL - let _: f32 = 16_777_217.0;
1272
LL + let _: f32 = 16_777_216.0;
1373
|
1474

1575
error: literal cannot be represented as the underlying type without loss of precision
16-
--> tests/ui/lossy_float_literal.rs:16:18
76+
--> tests/ui/lossy_float_literal.rs:21:18
1777
|
1878
LL | let _: f32 = 16_777_219.0;
1979
| ^^^^^^^^^^^^
@@ -25,7 +85,7 @@ LL + let _: f32 = 16_777_220.0;
2585
|
2686

2787
error: literal cannot be represented as the underlying type without loss of precision
28-
--> tests/ui/lossy_float_literal.rs:18:18
88+
--> tests/ui/lossy_float_literal.rs:23:18
2989
|
3090
LL | let _: f32 = 16_777_219.;
3191
| ^^^^^^^^^^^
@@ -37,7 +97,7 @@ LL + let _: f32 = 16_777_220.0;
3797
|
3898

3999
error: literal cannot be represented as the underlying type without loss of precision
40-
--> tests/ui/lossy_float_literal.rs:20:18
100+
--> tests/ui/lossy_float_literal.rs:25:18
41101
|
42102
LL | let _: f32 = 16_777_219.000;
43103
| ^^^^^^^^^^^^^^
@@ -49,7 +109,7 @@ LL + let _: f32 = 16_777_220.0;
49109
|
50110

51111
error: literal cannot be represented as the underlying type without loss of precision
52-
--> tests/ui/lossy_float_literal.rs:22:13
112+
--> tests/ui/lossy_float_literal.rs:27:13
53113
|
54114
LL | let _ = 16_777_219f32;
55115
| ^^^^^^^^^^^^^
@@ -61,7 +121,7 @@ LL + let _ = 16_777_220_f32;
61121
|
62122

63123
error: literal cannot be represented as the underlying type without loss of precision
64-
--> tests/ui/lossy_float_literal.rs:24:19
124+
--> tests/ui/lossy_float_literal.rs:29:19
65125
|
66126
LL | let _: f32 = -16_777_219.0;
67127
| ^^^^^^^^^^^^
@@ -73,7 +133,7 @@ LL + let _: f32 = -16_777_220.0;
73133
|
74134

75135
error: literal cannot be represented as the underlying type without loss of precision
76-
--> tests/ui/lossy_float_literal.rs:27:18
136+
--> tests/ui/lossy_float_literal.rs:32:18
77137
|
78138
LL | let _: f64 = 9_007_199_254_740_993.0;
79139
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -85,7 +145,7 @@ LL + let _: f64 = 9_007_199_254_740_992.0;
85145
|
86146

87147
error: literal cannot be represented as the underlying type without loss of precision
88-
--> tests/ui/lossy_float_literal.rs:29:18
148+
--> tests/ui/lossy_float_literal.rs:34:18
89149
|
90150
LL | let _: f64 = 9_007_199_254_740_993.;
91151
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -97,7 +157,7 @@ LL + let _: f64 = 9_007_199_254_740_992.0;
97157
|
98158

99159
error: literal cannot be represented as the underlying type without loss of precision
100-
--> tests/ui/lossy_float_literal.rs:31:18
160+
--> tests/ui/lossy_float_literal.rs:36:18
101161
|
102162
LL | let _: f64 = 9_007_199_254_740_993.00;
103163
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -109,7 +169,7 @@ LL + let _: f64 = 9_007_199_254_740_992.0;
109169
|
110170

111171
error: literal cannot be represented as the underlying type without loss of precision
112-
--> tests/ui/lossy_float_literal.rs:33:13
172+
--> tests/ui/lossy_float_literal.rs:38:13
113173
|
114174
LL | let _ = 9_007_199_254_740_993f64;
115175
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -121,7 +181,7 @@ LL + let _ = 9_007_199_254_740_992_f64;
121181
|
122182

123183
error: literal cannot be represented as the underlying type without loss of precision
124-
--> tests/ui/lossy_float_literal.rs:35:19
184+
--> tests/ui/lossy_float_literal.rs:40:19
125185
|
126186
LL | let _: f64 = -9_007_199_254_740_993.0;
127187
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -132,5 +192,5 @@ LL - let _: f64 = -9_007_199_254_740_993.0;
132192
LL + let _: f64 = -9_007_199_254_740_992.0;
133193
|
134194

135-
error: aborting due to 11 previous errors
195+
error: aborting due to 16 previous errors
136196

0 commit comments

Comments
 (0)