about summary refs log tree commit diff
path: root/src/librustc_parse
diff options
context:
space:
mode:
authorDavid Ross <daboross@daboross.net>2020-02-22 12:33:06 -0800
committerDavid Ross <daboross@daboross.net>2020-02-22 12:33:06 -0800
commitf434c6e636eda6c6c4fe167eee5b2549f8524ec7 (patch)
tree80cff81d2f4212e98bb027268c6c6841ea7de291 /src/librustc_parse
parentfa1f547f82d66f986af2c33220199b042fcb5f99 (diff)
Use multipart suggestion
This is a modified version of estebank's suggestion, with a bit of
extra cleanup now that we don't need the different cases for if we can
turn a span into a string or not.
Diffstat (limited to 'src/librustc_parse')
-rw-r--r--src/librustc_parse/parser/expr.rs22
1 files changed, 9 insertions, 13 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs
index 3d0f746d395..12729019e9b 100644
--- a/src/librustc_parse/parser/expr.rs
+++ b/src/librustc_parse/parser/expr.rs
@@ -648,8 +648,6 @@ impl<'a> Parser<'a> {
         if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _))
             || after_hash != before_hash
         {
-            let expr_str = self.span_to_snippet(span);
-
             let msg = format!(
                 "casts cannot be followed by {}",
                 match with_postfix.kind {
@@ -663,22 +661,20 @@ impl<'a> Parser<'a> {
                 }
             );
             let mut err = self.struct_span_err(span, &msg);
-            let suggestion = "try surrounding the expression in parentheses";
             // 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 {
-                if let Ok(expr_str) = expr_str {
-                    err.span_suggestion(
-                        span,
-                        suggestion,
-                        format!("({})", expr_str),
-                        Applicability::MachineApplicable,
-                    );
-                } else {
-                    err.span_help(span, suggestion);
-                }
+                let suggestions = vec![
+                    (span.shrink_to_lo(), "(".to_string()),
+                    (span.shrink_to_hi(), ")".to_string()),
+                ];
+                err.multipart_suggestion(
+                    "try surrounding the expression in parentheses",
+                    suggestions,
+                    Applicability::MachineApplicable,
+                );
             }
             err.emit();
         };