Skip to content

Commit f434c6e

Browse files
committed
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.
1 parent fa1f547 commit f434c6e

File tree

3 files changed

+238
-66
lines changed

3 files changed

+238
-66
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,6 @@ impl<'a> Parser<'a> {
648648
if !matches!(with_postfix.kind, ExprKind::Cast(_, _) | ExprKind::Type(_, _))
649649
|| after_hash != before_hash
650650
{
651-
let expr_str = self.span_to_snippet(span);
652-
653651
let msg = format!(
654652
"casts cannot be followed by {}",
655653
match with_postfix.kind {
@@ -663,22 +661,20 @@ impl<'a> Parser<'a> {
663661
}
664662
);
665663
let mut err = self.struct_span_err(span, &msg);
666-
let suggestion = "try surrounding the expression in parentheses";
667664
// if type ascription is "likely an error", the user will already be getting a useful
668665
// help message, and doesn't need a second.
669666
if self.last_type_ascription.map_or(false, |last_ascription| last_ascription.1) {
670667
self.maybe_annotate_with_ascription(&mut err, false);
671668
} else {
672-
if let Ok(expr_str) = expr_str {
673-
err.span_suggestion(
674-
span,
675-
suggestion,
676-
format!("({})", expr_str),
677-
Applicability::MachineApplicable,
678-
);
679-
} else {
680-
err.span_help(span, suggestion);
681-
}
669+
let suggestions = vec![
670+
(span.shrink_to_lo(), "(".to_string()),
671+
(span.shrink_to_hi(), ")".to_string()),
672+
];
673+
err.multipart_suggestion(
674+
"try surrounding the expression in parentheses",
675+
suggestions,
676+
Applicability::MachineApplicable,
677+
);
682678
}
683679
err.emit();
684680
};

src/test/ui/parser/issue-35813-postfix-after-cast.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ pub fn cast_cast_method_call() {
5858
//~^ ERROR: casts cannot be followed by a method call
5959
}
6060

61+
pub fn multiline_error() {
62+
let _ = 0
63+
as i32
64+
.count_ones();
65+
//~^^^ ERROR: casts cannot be followed by a method call
66+
}
67+
6168
// this tests that the precedence for `!x as Y.Z` is still what we expect
6269
pub fn precedence() {
6370
let x: i32 = &vec![1, 2, 3] as &Vec<i32>[0];

0 commit comments

Comments
 (0)