@@ -22,6 +22,7 @@ impl<'a> StringReader<'a> {
22
22
matching_delim_spans : Vec :: new ( ) ,
23
23
last_unclosed_found_span : None ,
24
24
last_delim_empty_block_spans : FxHashMap :: default ( ) ,
25
+ matching_block_spans : Vec :: new ( ) ,
25
26
} ;
26
27
let res = tt_reader. parse_all_token_trees ( ) ;
27
28
( res, tt_reader. unmatched_braces )
@@ -42,6 +43,9 @@ struct TokenTreesReader<'a> {
42
43
last_unclosed_found_span : Option < Span > ,
43
44
/// Collect empty block spans that might have been auto-inserted by editors.
44
45
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 ) > ,
45
49
}
46
50
47
51
impl < ' a > TokenTreesReader < ' a > {
@@ -77,6 +81,7 @@ impl<'a> TokenTreesReader<'a> {
77
81
78
82
fn parse_token_tree ( & mut self ) -> PResult < ' a , TreeAndJoint > {
79
83
let sm = self . string_reader . sess . source_map ( ) ;
84
+
80
85
match self . token . kind {
81
86
token:: Eof => {
82
87
let msg = "this file contains an unclosed delimiter" ;
@@ -146,6 +151,8 @@ impl<'a> TokenTreesReader<'a> {
146
151
}
147
152
}
148
153
154
+ self . matching_block_spans . push ( ( open_brace_span, close_brace_span) ) ;
155
+
149
156
if self . open_braces . is_empty ( ) {
150
157
// Clear up these spans to avoid suggesting them as we've found
151
158
// properly matched delimiters so far for an entire block.
@@ -164,6 +171,8 @@ impl<'a> TokenTreesReader<'a> {
164
171
token:: CloseDelim ( other) => {
165
172
let mut unclosed_delimiter = None ;
166
173
let mut candidate = None ;
174
+
175
+
167
176
if self . last_unclosed_found_span != Some ( self . token . span ) {
168
177
// do not complain about the same unclosed delimiter multiple times
169
178
self . last_unclosed_found_span = Some ( self . token . span ) ;
@@ -225,10 +234,16 @@ impl<'a> TokenTreesReader<'a> {
225
234
self . string_reader . sess . span_diagnostic . struct_span_err ( self . token . span , & msg) ;
226
235
227
236
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
+ }
232
247
}
233
248
err. span_label ( self . token . span , "unexpected closing delimiter" ) ;
234
249
Err ( err)
0 commit comments