Use multipart_suggestion

This commit is contained in:
Yuki Okushi 2021-09-17 13:39:26 +09:00
parent 58765d61ee
commit e9bf73cb2b
No known key found for this signature in database
GPG Key ID: DABA5B072961C18A
3 changed files with 21 additions and 23 deletions

View File

@ -1334,31 +1334,25 @@ pub(super) fn try_macro_suggestion(&mut self) -> PResult<'a, P<Expr>> {
pub(super) fn recover_parens_around_for_head(
&mut self,
pat: P<Pat>,
expr: &Expr,
begin_paren: Option<Span>,
) -> P<Pat> {
match (&self.token.kind, begin_paren) {
(token::CloseDelim(token::Paren), Some(begin_par_sp)) => {
self.bump();
let pat_str = self
// Remove the `(` from the span of the pattern:
.span_to_snippet(pat.span.trim_start(begin_par_sp).unwrap())
.unwrap_or_else(|_| pprust::pat_to_string(&pat));
let sp = MultiSpan::from_spans(vec![begin_par_sp, self.prev_token.span]);
self.struct_span_err(sp, "unexpected parenthesis surrounding `for` loop head")
.span_suggestion(
begin_par_sp.to(self.prev_token.span),
"remove parenthesis in `for` loop",
format!("{} in {}", pat_str, pprust::expr_to_string(&expr)),
// With e.g. `for (x) in y)` this would replace `(x) in y)`
// with `x) in y)` which is syntactically invalid.
// However, this is prevented before we get here.
Applicability::MachineApplicable,
)
.emit();
self.struct_span_err(
MultiSpan::from_spans(vec![begin_par_sp, self.prev_token.span]),
"unexpected parenthesis surrounding `for` loop head",
)
.multipart_suggestion(
"remove parenthesis in `for` loop",
vec![(begin_par_sp, String::new()), (self.prev_token.span, String::new())],
// With e.g. `for (x) in y)` this would replace `(x) in y)`
// with `x) in y)` which is syntactically invalid.
// However, this is prevented before we get here.
Applicability::MachineApplicable,
)
.emit();
// Unwrap `(pat)` into `pat` to avoid the `unused_parens` lint.
pat.and_then(|pat| match pat.kind {

View File

@ -2042,7 +2042,7 @@ fn parse_for_expr(
self.check_for_for_in_in_typo(self.prev_token.span);
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
let pat = self.recover_parens_around_for_head(pat, &expr, begin_paren);
let pat = self.recover_parens_around_for_head(pat, begin_paren);
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
attrs.extend(iattrs);

View File

@ -8,9 +8,13 @@ error: unexpected parenthesis surrounding `for` loop head
--> $DIR/recover-for-loop-parens-around-head.rs:10:9
|
LL | for ( elem in vec ) {
| ^-------------^
| |
| help: remove parenthesis in `for` loop: `elem in vec`
| ^ ^
|
help: remove parenthesis in `for` loop
|
LL - for ( elem in vec ) {
LL + for elem in vec {
|
error[E0308]: mismatched types
--> $DIR/recover-for-loop-parens-around-head.rs:13:38