From 4ee0d0d47d8ba964e7b9279b3c54986ef769737f Mon Sep 17 00:00:00 2001 From: Makro Date: Mon, 13 Jul 2026 21:01:41 +0000 Subject: [PATCH] perf: skip match usefulness analysis for bare bindings and wildcards Every let statement and every function parameter went through pattern lowering and the full usefulness engine (matrix construction, constructor splitting over the scrutinee type's shape) to conclude that a bare binding is irrefutable, which holds for any type. Answer the common shape directly; the only side-effect check from the full path that applies to a bare binding, the binding-named-same-as-variant error, still runs. --- .../rustc_mir_build/src/thir/pattern/check_match.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 7a38d9293724d..d415f43ad9007 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -674,6 +674,18 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { ) { let pattern_ty = pat.ty; + // Fast path: a bare binding or wildcard — the shape of almost every + // `let` statement and function parameter — is irrefutable for any + // type, so don't lower the pattern or run the usefulness engine on + // it. Anything nested (`x @ sub`, destructuring, constants) still + // runs the full analysis. The only per-pattern check from the full + // path that can apply to a bare binding is the binding-named-same- + // as-variant error, so run that directly. + if matches!(pat.kind, PatKind::Binding { subpattern: None, .. } | PatKind::Wild) { + check_for_bindings_named_same_as_variants(self, pat, Irrefutable); + return; + } + let Ok((cx, report)) = self.analyze_binding(pat, Irrefutable, scrut) else { return }; let witnesses = report.non_exhaustiveness_witnesses; if witnesses.is_empty() {