1
Fork 0

suggest adding a missing zero to a floating point number

This commit is contained in:
Takayuki Maeda 2022-07-06 20:09:39 +09:00
parent 5b8cf49c51
commit 6f65b7e64b
4 changed files with 70 additions and 3 deletions

View file

@ -2162,14 +2162,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if !expr_t.is_primitive_ty() {
self.ban_nonexisting_field(field, base, expr, expr_t);
} else {
type_error_struct!(
let field_name = field.to_string();
let mut err = type_error_struct!(
self.tcx().sess,
field.span,
expr_t,
E0610,
"`{expr_t}` is a primitive type and therefore doesn't have fields",
)
.emit();
);
if expr_t.is_integral()
&& (field_name
.strip_prefix('e')
.or_else(|| field_name.strip_prefix('E'))
.map(|prefix| prefix.chars().all(|c| c.is_numeric()))
.unwrap_or_default()
|| field.name == sym::f32
|| field.name == sym::f64)
{
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,
);
}
err.emit();
}
self.tcx().ty_error()

View file

@ -0,0 +1,7 @@
// run-rustfix
fn main() {
2.0e1; //~ERROR `{integer}` is a primitive type and therefore doesn't have fields
2.0f32; //~ERROR `{integer}` is a primitive type and therefore doesn't have fields
2.0f64; //~ERROR `{integer}` is a primitive type and therefore doesn't have fields
}

View file

@ -0,0 +1,7 @@
// run-rustfix
fn main() {
2.e1; //~ERROR `{integer}` is a primitive type and therefore doesn't have fields
2.f32; //~ERROR `{integer}` is a primitive type and therefore doesn't have fields
2.f64; //~ERROR `{integer}` is a primitive type and therefore doesn't have fields
}

View file

@ -0,0 +1,36 @@
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/suggest-adding-missing-zero-to-floating-point-number.rs:4:7
|
LL | 2.e1;
| ^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
|
LL | 2.0e1;
| +
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/suggest-adding-missing-zero-to-floating-point-number.rs:5:7
|
LL | 2.f32;
| ^^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
|
LL | 2.0f32;
| +
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
--> $DIR/suggest-adding-missing-zero-to-floating-point-number.rs:6:7
|
LL | 2.f64;
| ^^^
|
help: If the number is meant to be a floating point number, consider adding a `0` after the period
|
LL | 2.0f64;
| +
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0610`.