about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKevin Per <kevin.per@protonmail.com>2020-04-02 06:42:21 +0000
committerKevin Per <kevin.per@protonmail.com>2020-04-02 06:42:21 +0000
commitb85d5881992c07c51fb3f3eb113b00a5abbf74f0 (patch)
treedc8929032e7abd0184a63262f0254b8ac3624bf7 /src
parentaf16794b2f8be96cb485f8660c16f1e1616a99a4 (diff)
downloadrust-b85d5881992c07c51fb3f3eb113b00a5abbf74f0.tar.gz
rust-b85d5881992c07c51fb3f3eb113b00a5abbf74f0.zip
Check if the suggestion's `this block is empty...` span is in the last properly closed block.
Diffstat (limited to 'src')
-rw-r--r--src/librustc_parse/lexer/tokentrees.rs23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/librustc_parse/lexer/tokentrees.rs b/src/librustc_parse/lexer/tokentrees.rs
index b65b8941728..074f32adefd 100644
--- a/src/librustc_parse/lexer/tokentrees.rs
+++ b/src/librustc_parse/lexer/tokentrees.rs
@@ -22,6 +22,7 @@ impl<'a> StringReader<'a> {
             matching_delim_spans: Vec::new(),
             last_unclosed_found_span: None,
             last_delim_empty_block_spans: FxHashMap::default(),
+            matching_block_spans: Vec::new(),
         };
         let res = tt_reader.parse_all_token_trees();
         (res, tt_reader.unmatched_braces)
@@ -42,6 +43,9 @@ struct TokenTreesReader<'a> {
     last_unclosed_found_span: Option<Span>,
     /// Collect empty block spans that might have been auto-inserted by editors.
     last_delim_empty_block_spans: FxHashMap<token::DelimToken, Span>,
+    /// Collect the spans of braces (Open, Close). Used only
+    /// for detecting if blocks are empty
+    matching_block_spans: Vec<(Span, Span)>,
 }
 
 impl<'a> TokenTreesReader<'a> {
@@ -77,6 +81,7 @@ impl<'a> TokenTreesReader<'a> {
 
     fn parse_token_tree(&mut self) -> PResult<'a, TreeAndJoint> {
         let sm = self.string_reader.sess.source_map();
+
         match self.token.kind {
             token::Eof => {
                 let msg = "this file contains an unclosed delimiter";
@@ -146,6 +151,8 @@ impl<'a> TokenTreesReader<'a> {
                             }
                         }
 
+                        self.matching_block_spans.push((open_brace_span, close_brace_span));
+
                         if self.open_braces.is_empty() {
                             // Clear up these spans to avoid suggesting them as we've found
                             // properly matched delimiters so far for an entire block.
@@ -164,6 +171,8 @@ impl<'a> TokenTreesReader<'a> {
                     token::CloseDelim(other) => {
                         let mut unclosed_delimiter = None;
                         let mut candidate = None;
+
+
                         if self.last_unclosed_found_span != Some(self.token.span) {
                             // do not complain about the same unclosed delimiter multiple times
                             self.last_unclosed_found_span = Some(self.token.span);
@@ -225,10 +234,16 @@ impl<'a> TokenTreesReader<'a> {
                     self.string_reader.sess.span_diagnostic.struct_span_err(self.token.span, &msg);
 
                 if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
-                    err.span_label(
-                        span,
-                        "this block is empty, you might have not meant to close it",
-                    );
+                    // Braces are added at the end, so the last element is the biggest block
+                    if let Some(parent) = self.matching_block_spans.last() {
+                        // Check if the (empty block) is in the last properly closed block
+                        if (parent.0.to(parent.1)).contains(span) {
+                            err.span_label(
+                                span,
+                                "this block is empty, you might have not meant to close it",
+                            );
+                        }
+                    }
                 }
                 err.span_label(self.token.span, "unexpected closing delimiter");
                 Err(err)