Extend invalid floating point literal suffix suggestion

This commit is contained in:
yukang 2022-08-16 10:50:04 +08:00
parent 1603a70f82
commit 84629a5ab1
3 changed files with 71 additions and 9 deletions

View file

@ -2159,7 +2159,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
E0610,
"`{expr_t}` is a primitive type and therefore doesn't have fields",
);
let is_valid_suffix = |field: String| {
let is_valid_suffix = |field: &str| {
if field == "f32" || field == "f64" {
return true;
}
@ -2184,20 +2184,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let suffix = chars.collect::<String>();
suffix.is_empty() || suffix == "f32" || suffix == "f64"
};
let is_likely_suffix = |fist_chars: &[char], field: &str| {
field.len() >= 1
&& field.to_lowercase().starts_with(fist_chars)
&& field[1..].chars().all(|c| c.is_ascii_digit())
};
if let ty::Infer(ty::IntVar(_)) = expr_t.kind()
&& let ExprKind::Lit(Spanned {
node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed),
..
}) = base.kind
&& !base.span.from_expansion()
&& is_valid_suffix(field_name)
{
err.span_suggestion_verbose(
field.span.shrink_to_lo(),
"If the number is meant to be a floating point number, consider adding a `0` after the period",
'0',
Applicability::MaybeIncorrect,
);
let msg = "If the number is meant to be a floating point number, consider adding a `0` after the period";
if is_valid_suffix(&field_name) {
err.span_suggestion_verbose(
field.span.shrink_to_lo(),
msg,
'0',
Applicability::MaybeIncorrect,
);
} else if is_likely_suffix(&['f', 'l'], &field_name) {
err.span_suggestion_verbose(
field.span,
format!("{}, valid float format are `f32` and `f64`", msg),
"0f32",
Applicability::MaybeIncorrect,
);
}
}
err.emit();
}

View file

@ -3,4 +3,8 @@ fn main() {
let x = 0;
let _ = x.foo; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = x.bar; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 0.f; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 2.l; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 12.F; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
let _ = 34.L; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
}

View file

@ -10,6 +10,50 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | let _ = x.bar;
| ^^^
error: aborting due to 2 previous errors
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:6:15
|
LL | let _ = 0.f;
| ^
|
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`
|
LL | let _ = 0.0f32;
| ~~~~
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:7:15
|
LL | let _ = 2.l;
| ^
|
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`
|
LL | let _ = 2.0f32;
| ~~~~
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:8:16
|
LL | let _ = 12.F;
| ^
|
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`
|
LL | let _ = 12.0f32;
| ~~~~
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/attempted-access-non-fatal.rs:9:16
|
LL | let _ = 34.L;
| ^
|
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`
|
LL | let _ = 34.0f32;
| ~~~~
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0610`.