Check if the suggestion's this block is empty... span is in the last properly closed block.

This commit is contained in:
Kevin Per 2020-04-02 06:42:21 +00:00
parent af16794b2f
commit b85d588199

View file

@ -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)