Skip to content

Suggest adding a missing zero to a floating point number #98972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for you to check that the type we're dereferencing with . is a numerical literal? Specifically, I don't want it to fix:

let x = 1i32;
x.e10;

Copy link
Member

@compiler-errors compiler-errors Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if it already accounts for this, can you add a ui test for this case?

Copy link
Member

@compiler-errors compiler-errors Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To elaborate on this, I would probably just span_to_snippet and check that it parses as a valid number, to account for macro expansions. So you should also add a check for:

macro_rules! num { () => { 1 } }

fn main() {
    num!().e10;
}

&& (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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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`.