Skip to content

Commit 89a51a1

Browse files
committed
use proper words in help message for floating point
1 parent 84629a5 commit 89a51a1

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

compiler/rustc_typeck/src/check/expr.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -2184,10 +2184,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21842184
let suffix = chars.collect::<String>();
21852185
suffix.is_empty() || suffix == "f32" || suffix == "f64"
21862186
};
2187-
let is_likely_suffix = |fist_chars: &[char], field: &str| {
2188-
field.len() >= 1
2189-
&& field.to_lowercase().starts_with(fist_chars)
2187+
let maybe_partial_suffix = |field: &str| -> Option<&str> {
2188+
let first_chars = ['f', 'l'];
2189+
if field.len() >= 1
2190+
&& field.to_lowercase().starts_with(first_chars)
21902191
&& field[1..].chars().all(|c| c.is_ascii_digit())
2192+
{
2193+
if field.to_lowercase().starts_with(['f']) { Some("f32") } else { Some("f64") }
2194+
} else {
2195+
None
2196+
}
21912197
};
21922198
if let ty::Infer(ty::IntVar(_)) = expr_t.kind()
21932199
&& let ExprKind::Lit(Spanned {
@@ -2196,19 +2202,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21962202
}) = base.kind
21972203
&& !base.span.from_expansion()
21982204
{
2199-
let msg = "If the number is meant to be a floating point number, consider adding a `0` after the period";
22002205
if is_valid_suffix(&field_name) {
22012206
err.span_suggestion_verbose(
22022207
field.span.shrink_to_lo(),
2203-
msg,
2208+
"if intended to be a floating point literal, consider adding a `0` after the period",
22042209
'0',
22052210
Applicability::MaybeIncorrect,
22062211
);
2207-
} else if is_likely_suffix(&['f', 'l'], &field_name) {
2212+
} else if let Some(correct_suffix) = maybe_partial_suffix(&field_name) {
22082213
err.span_suggestion_verbose(
22092214
field.span,
2210-
format!("{}, valid float format are `f32` and `f64`", msg),
2211-
"0f32",
2215+
format!("if intended to be a floating point literal, consider adding a `0` after the period and a `{correct_suffix}` suffix"),
2216+
format!("0{correct_suffix}"),
22122217
Applicability::MaybeIncorrect,
22132218
);
22142219
}

src/test/ui/attempted-access-non-fatal.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
1616
LL | let _ = 0.f;
1717
| ^
1818
|
19-
help: If the number is meant to be a floating point number, consider adding a `0` after the period, valid float format are `f32` and `f64`
19+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f32` suffix
2020
|
2121
LL | let _ = 0.0f32;
2222
| ~~~~
@@ -27,9 +27,9 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
2727
LL | let _ = 2.l;
2828
| ^
2929
|
30-
help: If the number is meant to be a floating point number, consider adding a `0` after the period, valid float format are `f32` and `f64`
30+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f64` suffix
3131
|
32-
LL | let _ = 2.0f32;
32+
LL | let _ = 2.0f64;
3333
| ~~~~
3434

3535
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
@@ -38,7 +38,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
3838
LL | let _ = 12.F;
3939
| ^
4040
|
41-
help: If the number is meant to be a floating point number, consider adding a `0` after the period, valid float format are `f32` and `f64`
41+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f32` suffix
4242
|
4343
LL | let _ = 12.0f32;
4444
| ~~~~
@@ -49,9 +49,9 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
4949
LL | let _ = 34.L;
5050
| ^
5151
|
52-
help: If the number is meant to be a floating point number, consider adding a `0` after the period, valid float format are `f32` and `f64`
52+
help: if intended to be a floating point literal, consider adding a `0` after the period and a `f64` suffix
5353
|
54-
LL | let _ = 34.0f32;
54+
LL | let _ = 34.0f64;
5555
| ~~~~
5656

5757
error: aborting due to 6 previous errors

src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
44
LL | 2.e1;
55
| ^^
66
|
7-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
7+
help: if intended to be a floating point literal, consider adding a `0` after the period
88
|
99
LL | 2.0e1;
1010
| +
@@ -15,7 +15,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
1515
LL | 2.E1;
1616
| ^^
1717
|
18-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
18+
help: if intended to be a floating point literal, consider adding a `0` after the period
1919
|
2020
LL | 2.0E1;
2121
| +
@@ -26,7 +26,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
2626
LL | 2.f32;
2727
| ^^^
2828
|
29-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
29+
help: if intended to be a floating point literal, consider adding a `0` after the period
3030
|
3131
LL | 2.0f32;
3232
| +
@@ -37,7 +37,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
3737
LL | 2.f64;
3838
| ^^^
3939
|
40-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
40+
help: if intended to be a floating point literal, consider adding a `0` after the period
4141
|
4242
LL | 2.0f64;
4343
| +
@@ -48,7 +48,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
4848
LL | 2.e+12;
4949
| ^
5050
|
51-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
51+
help: if intended to be a floating point literal, consider adding a `0` after the period
5252
|
5353
LL | 2.0e+12;
5454
| +
@@ -59,7 +59,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
5959
LL | 2.e-12;
6060
| ^
6161
|
62-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
62+
help: if intended to be a floating point literal, consider adding a `0` after the period
6363
|
6464
LL | 2.0e-12;
6565
| +
@@ -70,7 +70,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
7070
LL | 2.e1f32;
7171
| ^^^^^
7272
|
73-
help: If the number is meant to be a floating point number, consider adding a `0` after the period
73+
help: if intended to be a floating point literal, consider adding a `0` after the period
7474
|
7575
LL | 2.0e1f32;
7676
| +

0 commit comments

Comments
 (0)