From e972232f276d8860d8f0abaf54dc3671b0ece46e Mon Sep 17 00:00:00 2001 From: cyrgani Date: Sun, 12 Apr 2026 20:56:45 +0000 Subject: [PATCH] reduce unnecessary allocations a bit --- compiler/rustc_expand/src/mbe/diagnostics.rs | 43 +++++++++----------- tests/ui/macros/typo-in-norepeat-expr.fixed | 2 +- tests/ui/macros/typo-in-norepeat-expr.rs | 2 +- tests/ui/macros/typo-in-norepeat-expr.stderr | 2 +- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/diagnostics.rs b/compiler/rustc_expand/src/mbe/diagnostics.rs index 3de94d9830a7..b8040c3d9c1a 100644 --- a/compiler/rustc_expand/src/mbe/diagnostics.rs +++ b/compiler/rustc_expand/src/mbe/diagnostics.rs @@ -288,27 +288,24 @@ pub(super) fn emit_frag_parse_err( _ => annotate_err_with_kind(&mut e, kind, site_span), }; - let mut bindings_rules = vec![]; - for rule in bindings { - let MacroRule::Func { lhs, .. } = rule else { continue }; - for param in lhs { - let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; - bindings_rules.push(*bind); - } - } - - let mut matched_rule_bindings_rules = vec![]; - for param in matched_rule_bindings { - let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; - matched_rule_bindings_rules.push(*bind); - } - - let matched_rule_bindings_names: Vec<_> = - matched_rule_bindings_rules.iter().map(|bind| bind.name).collect(); - let bindings_name: Vec<_> = bindings_rules.iter().map(|bind| bind.name).collect(); if parser.token.kind == token::Dollar { parser.bump(); if let token::Ident(name, _) = parser.token.kind { + let mut bindings_names = vec![]; + for rule in bindings { + let MacroRule::Func { lhs, .. } = rule else { continue }; + for param in lhs { + let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; + bindings_names.push(bind.name); + } + } + + let mut matched_rule_bindings_names = vec![]; + for param in matched_rule_bindings { + let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; + matched_rule_bindings_names.push(bind.name); + } + if let Some(matched_name) = rustc_span::edit_distance::find_best_match_for_name( &matched_rule_bindings_names[..], name, @@ -316,22 +313,22 @@ pub(super) fn emit_frag_parse_err( ) { e.span_suggestion_verbose( parser.token.span, - "there is a macro metavariable with similar name", - format!("{matched_name}"), + "there is a macro metavariable with a similar name", + matched_name, Applicability::MaybeIncorrect, ); - } else if bindings_name.contains(&name) { + } else if bindings_names.contains(&name) { e.span_label( parser.token.span, "there is an macro metavariable with this name in another macro matcher", ); } else if let Some(matched_name) = - rustc_span::edit_distance::find_best_match_for_name(&bindings_name[..], name, None) + rustc_span::edit_distance::find_best_match_for_name(&bindings_names[..], name, None) { e.span_suggestion_verbose( parser.token.span, "there is a macro metavariable with a similar name in another macro matcher", - format!("{matched_name}"), + matched_name, Applicability::MaybeIncorrect, ); } else { diff --git a/tests/ui/macros/typo-in-norepeat-expr.fixed b/tests/ui/macros/typo-in-norepeat-expr.fixed index a59f461e6312..b06337e45204 100644 --- a/tests/ui/macros/typo-in-norepeat-expr.fixed +++ b/tests/ui/macros/typo-in-norepeat-expr.fixed @@ -2,7 +2,7 @@ macro_rules! m { (begin $ard:ident end) => { [$ard] //~ ERROR: expected expression, found `$` - //~^ HELP: there is a macro metavariable with similar name + //~^ HELP: there is a macro metavariable with a similar name }; } diff --git a/tests/ui/macros/typo-in-norepeat-expr.rs b/tests/ui/macros/typo-in-norepeat-expr.rs index fe554f07e755..8a155c50cec8 100644 --- a/tests/ui/macros/typo-in-norepeat-expr.rs +++ b/tests/ui/macros/typo-in-norepeat-expr.rs @@ -2,7 +2,7 @@ macro_rules! m { (begin $ard:ident end) => { [$arg] //~ ERROR: expected expression, found `$` - //~^ HELP: there is a macro metavariable with similar name + //~^ HELP: there is a macro metavariable with a similar name }; } diff --git a/tests/ui/macros/typo-in-norepeat-expr.stderr b/tests/ui/macros/typo-in-norepeat-expr.stderr index 8f37957ea98e..9a25e36922c1 100644 --- a/tests/ui/macros/typo-in-norepeat-expr.stderr +++ b/tests/ui/macros/typo-in-norepeat-expr.stderr @@ -8,7 +8,7 @@ LL | let _ = m![begin x end]; | --------------- in this macro invocation | = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) -help: there is a macro metavariable with similar name +help: there is a macro metavariable with a similar name | LL - [$arg] LL + [$ard]