Skip to content

Commit e9bf73c

Browse files
committed
Use multipart_suggestion
1 parent 58765d6 commit e9bf73c

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -1334,31 +1334,25 @@ impl<'a> Parser<'a> {
13341334
pub(super) fn recover_parens_around_for_head(
13351335
&mut self,
13361336
pat: P<Pat>,
1337-
expr: &Expr,
13381337
begin_paren: Option<Span>,
13391338
) -> P<Pat> {
13401339
match (&self.token.kind, begin_paren) {
13411340
(token::CloseDelim(token::Paren), Some(begin_par_sp)) => {
13421341
self.bump();
13431342

1344-
let pat_str = self
1345-
// Remove the `(` from the span of the pattern:
1346-
.span_to_snippet(pat.span.trim_start(begin_par_sp).unwrap())
1347-
.unwrap_or_else(|_| pprust::pat_to_string(&pat));
1348-
1349-
let sp = MultiSpan::from_spans(vec![begin_par_sp, self.prev_token.span]);
1350-
1351-
self.struct_span_err(sp, "unexpected parenthesis surrounding `for` loop head")
1352-
.span_suggestion(
1353-
begin_par_sp.to(self.prev_token.span),
1354-
"remove parenthesis in `for` loop",
1355-
format!("{} in {}", pat_str, pprust::expr_to_string(&expr)),
1356-
// With e.g. `for (x) in y)` this would replace `(x) in y)`
1357-
// with `x) in y)` which is syntactically invalid.
1358-
// However, this is prevented before we get here.
1359-
Applicability::MachineApplicable,
1360-
)
1361-
.emit();
1343+
self.struct_span_err(
1344+
MultiSpan::from_spans(vec![begin_par_sp, self.prev_token.span]),
1345+
"unexpected parenthesis surrounding `for` loop head",
1346+
)
1347+
.multipart_suggestion(
1348+
"remove parenthesis in `for` loop",
1349+
vec![(begin_par_sp, String::new()), (self.prev_token.span, String::new())],
1350+
// With e.g. `for (x) in y)` this would replace `(x) in y)`
1351+
// with `x) in y)` which is syntactically invalid.
1352+
// However, this is prevented before we get here.
1353+
Applicability::MachineApplicable,
1354+
)
1355+
.emit();
13621356

13631357
// Unwrap `(pat)` into `pat` to avoid the `unused_parens` lint.
13641358
pat.and_then(|pat| match pat.kind {

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ impl<'a> Parser<'a> {
20422042
self.check_for_for_in_in_typo(self.prev_token.span);
20432043
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
20442044

2045-
let pat = self.recover_parens_around_for_head(pat, &expr, begin_paren);
2045+
let pat = self.recover_parens_around_for_head(pat, begin_paren);
20462046

20472047
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
20482048
attrs.extend(iattrs);

src/test/ui/parser/recover-for-loop-parens-around-head.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ error: unexpected parenthesis surrounding `for` loop head
88
--> $DIR/recover-for-loop-parens-around-head.rs:10:9
99
|
1010
LL | for ( elem in vec ) {
11-
| ^-------------^
12-
| |
13-
| help: remove parenthesis in `for` loop: `elem in vec`
11+
| ^ ^
12+
|
13+
help: remove parenthesis in `for` loop
14+
|
15+
LL - for ( elem in vec ) {
16+
LL + for elem in vec {
17+
|
1418

1519
error[E0308]: mismatched types
1620
--> $DIR/recover-for-loop-parens-around-head.rs:13:38

0 commit comments

Comments
 (0)