]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_resolve/src/late/diagnostics.rs
Detect bare blocks with type ascription that were meant to be a `struct` literal
[rust.git] / compiler / rustc_resolve / src / late / diagnostics.rs
index 45657f2d0f22a150b3f53bf4218fb33eabe6c53f..b2c0c7874655e0d6fe9fd6bf7a412ab1b5a4fb3b 100644 (file)
@@ -207,6 +207,16 @@ pub(crate) fn smart_resolve_report_errors(
         let code = source.error_code(res.is_some());
         let mut err = self.r.session.struct_span_err_with_code(base_span, &base_msg, code);
 
+        if let Some(span) = self.diagnostic_metadata.current_block_could_be_bare_struct_literal {
+            err.multipart_suggestion(
+                "you might have meant to write a `struct` literal",
+                vec![
+                    (span.shrink_to_lo(), "{ SomeStruct ".to_string()),
+                    (span.shrink_to_hi(), "}".to_string()),
+                ],
+                Applicability::HasPlaceholders,
+            );
+        }
         match (source, self.diagnostic_metadata.in_if_condition) {
             (PathSource::Expr(_), Some(Expr { span, kind: ExprKind::Assign(..), .. })) => {
                 err.span_suggestion_verbose(
@@ -2090,7 +2100,7 @@ fn suggestion(&self, sugg: String) -> Option<(Span, String)> {
                                 self.is_unnamed(),
                                 self.is_underscore(),
                                 self.is_named(),
-                                sugg.starts_with("&"),
+                                sugg.starts_with('&'),
                             ) {
                                 (true, _, _, false) => (self.span_unnamed_borrow(), sugg),
                                 (true, _, _, true) => {