about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2022-10-24 18:24:10 +0800
committeryukang <moorekang@gmail.com>2022-10-24 18:24:10 +0800
commita46af18cb1aedf7ccc5fbebd6f126c71da6cd4ee (patch)
tree309811e2acecde1b850f0851446fe87ab5436dd2
parentc0447b489ba890c740c1e5c41f3eacca148734a9 (diff)
downloadrust-a46af18cb1aedf7ccc5fbebd6f126c71da6cd4ee.tar.gz
rust-a46af18cb1aedf7ccc5fbebd6f126c71da6cd4ee.zip
fix parentheses surrounding spacing issue in parser
-rw-r--r--compiler/rustc_lint/src/unused.rs6
-rw-r--r--compiler/rustc_parse/src/errors.rs6
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs24
-rw-r--r--src/test/ui/lint/issue-103435-extra-parentheses.fixed6
-rw-r--r--src/test/ui/lint/issue-103435-extra-parentheses.rs6
-rw-r--r--src/test/ui/lint/issue-103435-extra-parentheses.stderr20
6 files changed, 52 insertions, 16 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs
index 1299444cd77..d4b083de276 100644
--- a/compiler/rustc_lint/src/unused.rs
+++ b/compiler/rustc_lint/src/unused.rs
@@ -568,8 +568,7 @@ trait UnusedDelimLint {
                 let sm = cx.sess().source_map();
                 let lo_replace =
                     if keep_space.0 &&
-                        let Ok(snip) = sm.span_to_snippet(lo.with_lo(lo.lo() - BytePos(1))) &&
-                        !snip.starts_with(" ") {
+                        let Ok(snip) = sm.span_to_prev_source(lo) && !snip.ends_with(" ") {
                         " ".to_string()
                         } else {
                             "".to_string()
@@ -577,8 +576,7 @@ trait UnusedDelimLint {
 
                 let hi_replace =
                     if keep_space.1 &&
-                        let Ok(snip) = sm.span_to_snippet(sm.next_point(hi)) &&
-                        !snip.starts_with(" ") {
+                        let Ok(snip) = sm.span_to_next_source(hi) && !snip.starts_with(" ") {
                         " ".to_string()
                         } else {
                             "".to_string()
diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs
index 9b177c5189b..a3fbc06293f 100644
--- a/compiler/rustc_parse/src/errors.rs
+++ b/compiler/rustc_parse/src/errors.rs
@@ -1122,10 +1122,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 887a4a6de33..e9549e89998 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -1641,15 +1641,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.
diff --git a/src/test/ui/lint/issue-103435-extra-parentheses.fixed b/src/test/ui/lint/issue-103435-extra-parentheses.fixed
index dbbcaa441dd..2b01b414baa 100644
--- a/src/test/ui/lint/issue-103435-extra-parentheses.fixed
+++ b/src/test/ui/lint/issue-103435-extra-parentheses.fixed
@@ -11,6 +11,8 @@ fn main() {
     if 2 == 1 {}
     //~^ ERROR unnecessary parentheses around `if` condition
 
-    // FIXME, auto recover from this one?
-    // for(_x in 1..10) {}
+    // reported by parser
+    for _x in 1..10 {}
+    //~^ ERROR expected one of
+    //~| ERROR unexpected parentheses surrounding
 }
diff --git a/src/test/ui/lint/issue-103435-extra-parentheses.rs b/src/test/ui/lint/issue-103435-extra-parentheses.rs
index f5c2a6664ed..8261610cf56 100644
--- a/src/test/ui/lint/issue-103435-extra-parentheses.rs
+++ b/src/test/ui/lint/issue-103435-extra-parentheses.rs
@@ -11,6 +11,8 @@ fn main() {
     if(2 == 1){}
     //~^ ERROR unnecessary parentheses around `if` condition
 
-    // FIXME, auto recover from this one?
-    // for(_x in 1..10) {}
+    // reported by parser
+    for(_x in 1..10){}
+    //~^ ERROR expected one of
+    //~| ERROR unexpected parentheses surrounding
 }
diff --git a/src/test/ui/lint/issue-103435-extra-parentheses.stderr b/src/test/ui/lint/issue-103435-extra-parentheses.stderr
index a3f2fbc51ab..29c41c91050 100644
--- a/src/test/ui/lint/issue-103435-extra-parentheses.stderr
+++ b/src/test/ui/lint/issue-103435-extra-parentheses.stderr
@@ -1,3 +1,21 @@
+error: expected one of `)`, `,`, `@`, or `|`, found keyword `in`
+  --> $DIR/issue-103435-extra-parentheses.rs:15:12
+   |
+LL |     for(_x in 1..10){}
+   |            ^^ expected one of `)`, `,`, `@`, or `|`
+
+error: unexpected parentheses surrounding `for` loop head
+  --> $DIR/issue-103435-extra-parentheses.rs:15:8
+   |
+LL |     for(_x in 1..10){}
+   |        ^           ^
+   |
+help: remove parentheses in `for` loop
+   |
+LL -     for(_x in 1..10){}
+LL +     for _x in 1..10 {}
+   |
+
 error: unnecessary parentheses around pattern
   --> $DIR/issue-103435-extra-parentheses.rs:5:11
    |
@@ -39,5 +57,5 @@ LL -     if(2 == 1){}
 LL +     if 2 == 1 {}
    |
 
-error: aborting due to 3 previous errors
+error: aborting due to 5 previous errors