diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 45ea04f2342..a79257f0f5e 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -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() diff --git a/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed b/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed new file mode 100644 index 00000000000..01d3769c1f3 --- /dev/null +++ b/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed @@ -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 +} diff --git a/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs b/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs new file mode 100644 index 00000000000..c2cbd5d5189 --- /dev/null +++ b/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs @@ -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 +} diff --git a/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr b/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr new file mode 100644 index 00000000000..042f9c727cc --- /dev/null +++ b/src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr @@ -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`.