]> git.lizzy.rs Git - rust.git/commitdiff
suggest adding a missing zero to a floating point number
authorTakayuki Maeda <takoyaki0316@gmail.com>
Wed, 6 Jul 2022 11:09:39 +0000 (20:09 +0900)
committerTakayuki Maeda <takoyaki0316@gmail.com>
Wed, 6 Jul 2022 11:09:39 +0000 (20:09 +0900)
compiler/rustc_typeck/src/check/expr.rs
src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed [new file with mode: 0644]
src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs [new file with mode: 0644]
src/test/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr [new file with mode: 0644]

index 45ea04f2342889da9a88df839473013abfd3519c..a79257f0f5ebec46d425076f2057fa8dba4b0219 100644 (file)
@@ -2162,14 +2162,31 @@ fn check_field(
         } 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 (file)
index 0000000..01d3769
--- /dev/null
@@ -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 (file)
index 0000000..c2cbd5d
--- /dev/null
@@ -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 (file)
index 0000000..042f9c7
--- /dev/null
@@ -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`.