Skip to content

Commit b85d588

Browse files
committed
Check if the suggestion's this block is empty... span is in the last properly closed block.
1 parent af16794 commit b85d588

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/librustc_parse/lexer/tokentrees.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl<'a> StringReader<'a> {
2222
matching_delim_spans: Vec::new(),
2323
last_unclosed_found_span: None,
2424
last_delim_empty_block_spans: FxHashMap::default(),
25+
matching_block_spans: Vec::new(),
2526
};
2627
let res = tt_reader.parse_all_token_trees();
2728
(res, tt_reader.unmatched_braces)
@@ -42,6 +43,9 @@ struct TokenTreesReader<'a> {
4243
last_unclosed_found_span: Option<Span>,
4344
/// Collect empty block spans that might have been auto-inserted by editors.
4445
last_delim_empty_block_spans: FxHashMap<token::DelimToken, Span>,
46+
/// Collect the spans of braces (Open, Close). Used only
47+
/// for detecting if blocks are empty
48+
matching_block_spans: Vec<(Span, Span)>,
4549
}
4650

4751
impl<'a> TokenTreesReader<'a> {
@@ -77,6 +81,7 @@ impl<'a> TokenTreesReader<'a> {
7781

7882
fn parse_token_tree(&mut self) -> PResult<'a, TreeAndJoint> {
7983
let sm = self.string_reader.sess.source_map();
84+
8085
match self.token.kind {
8186
token::Eof => {
8287
let msg = "this file contains an unclosed delimiter";
@@ -146,6 +151,8 @@ impl<'a> TokenTreesReader<'a> {
146151
}
147152
}
148153

154+
self.matching_block_spans.push((open_brace_span, close_brace_span));
155+
149156
if self.open_braces.is_empty() {
150157
// Clear up these spans to avoid suggesting them as we've found
151158
// properly matched delimiters so far for an entire block.
@@ -164,6 +171,8 @@ impl<'a> TokenTreesReader<'a> {
164171
token::CloseDelim(other) => {
165172
let mut unclosed_delimiter = None;
166173
let mut candidate = None;
174+
175+
167176
if self.last_unclosed_found_span != Some(self.token.span) {
168177
// do not complain about the same unclosed delimiter multiple times
169178
self.last_unclosed_found_span = Some(self.token.span);
@@ -225,10 +234,16 @@ impl<'a> TokenTreesReader<'a> {
225234
self.string_reader.sess.span_diagnostic.struct_span_err(self.token.span, &msg);
226235

227236
if let Some(span) = self.last_delim_empty_block_spans.remove(&delim) {
228-
err.span_label(
229-
span,
230-
"this block is empty, you might have not meant to close it",
231-
);
237+
// Braces are added at the end, so the last element is the biggest block
238+
if let Some(parent) = self.matching_block_spans.last() {
239+
// Check if the (empty block) is in the last properly closed block
240+
if (parent.0.to(parent.1)).contains(span) {
241+
err.span_label(
242+
span,
243+
"this block is empty, you might have not meant to close it",
244+
);
245+
}
246+
}
232247
}
233248
err.span_label(self.token.span, "unexpected closing delimiter");
234249
Err(err)

0 commit comments

Comments
 (0)