]> git.lizzy.rs Git - rust.git/commitdiff
Keep better fix suggestion if type ascription is likely unintended
authorDavid Ross <daboross@daboross.net>
Sun, 16 Feb 2020 00:18:50 +0000 (16:18 -0800)
committerDavid Ross <daboross@daboross.net>
Sun, 16 Feb 2020 03:51:02 +0000 (19:51 -0800)
src/librustc_parse/parser/expr.rs

index 645e680d15f4844ba2165d5f340ee159eca5dbe2..fe5570f26abd53287049ef33a0d76156fd82f172 100644 (file)
@@ -665,17 +665,23 @@ fn parse_and_disallow_postfix_after_cast(
             );
             let mut err = self.struct_span_err(span, &msg);
             let suggestion = "try surrounding the expression in parentheses";
-            if let Ok(expr_str) = expr_str {
-                err.span_suggestion(
-                    span,
-                    suggestion,
-                    format!("({})", expr_str),
-                    Applicability::MachineApplicable,
-                )
+            // if type ascription is "likely an error", the user will already be getting a useful
+            // help message, and doesn't need a second.
+            if self.last_type_ascription.map_or(false, |last_ascription| last_ascription.1) {
+                self.maybe_annotate_with_ascription(&mut err, false);
             } else {
-                err.span_help(span, suggestion)
+                if let Ok(expr_str) = expr_str {
+                    err.span_suggestion(
+                        span,
+                        suggestion,
+                        format!("({})", expr_str),
+                        Applicability::MachineApplicable,
+                    );
+                } else {
+                    err.span_help(span, suggestion);
+                }
             }
-            .emit();
+            err.emit();
         };
         Ok(with_postfix)
     }