Skip to content

Commit 0cf2049

Browse files
committed
Keep better fix suggestion if type ascription is likely unintended
1 parent 4fc0532 commit 0cf2049

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -665,17 +665,23 @@ impl<'a> Parser<'a> {
665665
);
666666
let mut err = self.struct_span_err(span, &msg);
667667
let suggestion = "try surrounding the expression in parentheses";
668-
if let Ok(expr_str) = expr_str {
669-
err.span_suggestion(
670-
span,
671-
suggestion,
672-
format!("({})", expr_str),
673-
Applicability::MachineApplicable,
674-
)
668+
// if type ascription is "likely an error", the user will already be getting a useful
669+
// help message, and doesn't need a second.
670+
if self.last_type_ascription.map_or(false, |last_ascription| last_ascription.1) {
671+
self.maybe_annotate_with_ascription(&mut err, false);
675672
} else {
676-
err.span_help(span, suggestion)
673+
if let Ok(expr_str) = expr_str {
674+
err.span_suggestion(
675+
span,
676+
suggestion,
677+
format!("({})", expr_str),
678+
Applicability::MachineApplicable,
679+
);
680+
} else {
681+
err.span_help(span, suggestion);
682+
}
677683
}
678-
.emit();
684+
err.emit();
679685
};
680686
Ok(with_postfix)
681687
}

0 commit comments

Comments
 (0)