diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2022-11-11 12:12:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-11 12:12:29 -0500 |
| commit | fd5ff82f28be617c6116a7a16bb1a32b401e9459 (patch) | |
| tree | ae92031f0b9fc93db96cc59fabf83d2c240917f9 /compiler/rustc_parse/src | |
| parent | 9553fea23a8e04162684ec0679b1ca05e61159cb (diff) | |
| parent | a46af18cb1aedf7ccc5fbebd6f126c71da6cd4ee (diff) | |
| download | rust-fd5ff82f28be617c6116a7a16bb1a32b401e9459.tar.gz rust-fd5ff82f28be617c6116a7a16bb1a32b401e9459.zip | |
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
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/errors.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 24 |
2 files changed, 23 insertions, 7 deletions
diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index e3acc11811f..d59982f7063 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1165,10 +1165,12 @@ pub(crate) struct ParenthesesInForHead { #[derive(Subdiagnostic)] #[multipart_suggestion(suggestion, applicability = "machine-applicable")] pub(crate) struct ParenthesesInForHeadSugg { - #[suggestion_part(code = "")] + #[suggestion_part(code = "{left_snippet}")] pub left: Span, - #[suggestion_part(code = "")] + pub left_snippet: String, + #[suggestion_part(code = "{right_snippet}")] pub right: Span, + pub right_snippet: String, } #[derive(Diagnostic)] diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index c609aa93da3..0bbe073fe2a 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -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. |
