Avoid some token cloning in filter_tokens_from_list.

Now the cloning only happens on some paths, instead of all paths.
This commit is contained in:
Nicholas Nethercote 2023-07-27 10:50:06 +10:00
parent 55a732461d
commit 853f453d57

View File

@ -2347,19 +2347,19 @@ fn get_all_import_attributes<'hir>(
} }
fn filter_tokens_from_list( fn filter_tokens_from_list(
args_tokens: TokenStream, args_tokens: &TokenStream,
should_retain: impl Fn(&TokenTree) -> bool, should_retain: impl Fn(&TokenTree) -> bool,
) -> Vec<TokenTree> { ) -> Vec<TokenTree> {
let mut tokens = Vec::with_capacity(args_tokens.len()); let mut tokens = Vec::with_capacity(args_tokens.len());
let mut skip_next_comma = false; let mut skip_next_comma = false;
for token in args_tokens.into_trees() { for token in args_tokens.trees() {
match token { match token {
TokenTree::Token(Token { kind: TokenKind::Comma, .. }, _) if skip_next_comma => { TokenTree::Token(Token { kind: TokenKind::Comma, .. }, _) if skip_next_comma => {
skip_next_comma = false; skip_next_comma = false;
} }
token if should_retain(&token) => { token if should_retain(token) => {
skip_next_comma = false; skip_next_comma = false;
tokens.push(token); tokens.push(token.clone());
} }
_ => { _ => {
skip_next_comma = true; skip_next_comma = true;
@ -2417,7 +2417,7 @@ fn add_without_unwanted_attributes<'hir>(
match normal.item.args { match normal.item.args {
ast::AttrArgs::Delimited(ref mut args) => { ast::AttrArgs::Delimited(ref mut args) => {
let tokens = let tokens =
filter_tokens_from_list(args.tokens.clone(), |token| { filter_tokens_from_list(&args.tokens, |token| {
!matches!( !matches!(
token, token,
TokenTree::Token( TokenTree::Token(