Skip to content

Commit

Permalink
Rollup merge of #111492 - calebcartwright:by-ref-tokentree-1, r=compi…
Browse files Browse the repository at this point in the history
…ler-errors

use by ref TokenTree iterator to avoid a few clones

Just a handful of swaps from the by-value cursor to by-ref cursor so as to avoid some unnecessary clones.

I've been doing some analysis on internal cleanup opportunities within rustfmt and as part of that yak-shave I found myself perusing broader token stream and tree usage (which we use within rustfmt). As reflected in some inline comments on the cursor structs (not part of this diff), there's probably many other such cases throughout the code, but figured I'd start small with these while I had the time. May take a look at the other sites in the future
  • Loading branch information
matthiaskrgr committed May 12, 2023
2 parents 4c12f5d + 05c5df5 commit 9039de6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/concat_idents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn expand_concat_idents<'cx>(
}

let mut res_str = String::new();
for (i, e) in tts.into_trees().enumerate() {
for (i, e) in tts.trees().enumerate() {
if i & 1 == 1 {
match e {
TokenTree::Token(Token { kind: token::Comma, .. }, _) => {}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/trace_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn expand_trace_macros(
sp: Span,
tt: TokenStream,
) -> Box<dyn base::MacResult + 'static> {
let mut cursor = tt.into_trees();
let mut cursor = tt.trees();
let mut err = false;
let value = match &cursor.next() {
Some(TokenTree::Token(token, _)) if token.is_keyword(kw::True) => true,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,8 +1882,8 @@ declare_lint_pass!(
struct UnderMacro(bool);

impl KeywordIdents {
fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: TokenStream) {
for tt in tokens.into_trees() {
fn check_tokens(&mut self, cx: &EarlyContext<'_>, tokens: &TokenStream) {
for tt in tokens.trees() {
match tt {
// Only report non-raw idents.
TokenTree::Token(token, _) => {
Expand Down Expand Up @@ -1944,10 +1944,10 @@ impl KeywordIdents {

impl EarlyLintPass for KeywordIdents {
fn check_mac_def(&mut self, cx: &EarlyContext<'_>, mac_def: &ast::MacroDef) {
self.check_tokens(cx, mac_def.body.tokens.clone());
self.check_tokens(cx, &mac_def.body.tokens);
}
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::MacCall) {
self.check_tokens(cx, mac.args.tokens.clone());
self.check_tokens(cx, &mac.args.tokens);
}
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
self.check_ident_token(cx, UnderMacro(false), ident);
Expand Down

0 comments on commit 9039de6

Please sign in to comment.