Rollup merge of #103468 - chenyukang:yukang/fix-103435-extra-parentheses, r=estebank

Fix unused lint and parser caring about spaces to won't produce invalid code

Fixes #103435
This commit is contained in:
Manish Goregaokar 2022-11-11 12:12:29 -05:00 committed by GitHub
commit fd5ff82f28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 149 additions and 20 deletions

View file

@ -1653,15 +1653,29 @@ impl<'a> Parser<'a> {
(token::CloseDelim(Delimiter::Parenthesis), Some(begin_par_sp)) => {
self.bump();
let sm = self.sess.source_map();
let left = begin_par_sp;
let right = self.prev_token.span;
let left_snippet = if let Ok(snip) = sm.span_to_prev_source(left) &&
!snip.ends_with(" ") {
" ".to_string()
} else {
"".to_string()
};
let right_snippet = if let Ok(snip) = sm.span_to_next_source(right) &&
!snip.starts_with(" ") {
" ".to_string()
} else {
"".to_string()
};
self.sess.emit_err(ParenthesesInForHead {
span: vec![begin_par_sp, self.prev_token.span],
span: vec![left, right],
// 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.
sugg: ParenthesesInForHeadSugg {
left: begin_par_sp,
right: self.prev_token.span,
},
sugg: ParenthesesInForHeadSugg { left, right, left_snippet, right_snippet },
});
// Unwrap `(pat)` into `pat` to avoid the `unused_parens` lint.