Only call parse_token_tree once.

This code currently uses a `while` loop and gathers token trees into a
vector, but it only succeeds in the case where there is a single token
tree. We can instead just call `parse_token_tree` once and then look for
`Eof` to detect the success case.
This commit is contained in:
Nicholas Nethercote 2023-07-31 15:53:46 +10:00
parent db7b07aa02
commit 203cba76e0

View File

@ -76,14 +76,13 @@ fn snippet_equal_to_token(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Option<String
};
// Reparse a single token tree.
let mut reparsed_trees = Vec::new();
while parser.token != token::Eof {
reparsed_trees.push(parser.parse_token_tree());
}
if reparsed_trees.len() != 1 {
if parser.token == token::Eof {
return None;
}
let reparsed_tree = parser.parse_token_tree();
if parser.token != token::Eof {
return None;
}
let reparsed_tree = reparsed_trees.pop().unwrap();
// Compare against the original tree.
if reparsed_tree.eq_unspanned(matcher) { Some(snippet) } else { None }