From 8df1e2472609921673f85118f37eeeee7c3d57a5 Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Tue, 14 Oct 2025 08:21:26 +0000 Subject: [PATCH] perf(linter): use top-level match for `const-comparisons` (#14581) Using a match means we only need to call one function instead of two. Also enables the linter codegen to analyze this rule. --- .../src/generated/rule_runner_impls.rs | 3 ++- .../src/rules/oxc/const_comparisons.rs | 25 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/oxc_linter/src/generated/rule_runner_impls.rs b/crates/oxc_linter/src/generated/rule_runner_impls.rs index 6767407aa369c..bfb91c6051c16 100644 --- a/crates/oxc_linter/src/generated/rule_runner_impls.rs +++ b/crates/oxc_linter/src/generated/rule_runner_impls.rs @@ -1854,7 +1854,8 @@ impl RuleRunner for crate::rules::oxc::branches_sharing_code::BranchesSharingCod } impl RuleRunner for crate::rules::oxc::const_comparisons::ConstComparisons { - const NODE_TYPES: Option<&AstTypesBitset> = None; + const NODE_TYPES: Option<&AstTypesBitset> = + Some(&AstTypesBitset::from_types(&[AstType::BinaryExpression, AstType::LogicalExpression])); const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run; } diff --git a/crates/oxc_linter/src/rules/oxc/const_comparisons.rs b/crates/oxc_linter/src/rules/oxc/const_comparisons.rs index 12b42599ba938..13d979a747e64 100644 --- a/crates/oxc_linter/src/rules/oxc/const_comparisons.rs +++ b/crates/oxc_linter/src/rules/oxc/const_comparisons.rs @@ -3,7 +3,7 @@ use std::cmp::Ordering; use oxc_ast::{ AstKind, - ast::{Expression, LogicalExpression, NumericLiteral, UnaryOperator}, + ast::{BinaryExpression, Expression, LogicalExpression, NumericLiteral, UnaryOperator}, }; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; @@ -111,17 +111,20 @@ declare_oxc_lint!( impl Rule for ConstComparisons { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { - Self::check_logical_expression(node, ctx); - Self::check_binary_expression(node, ctx); + match node.kind() { + AstKind::LogicalExpression(logical_expr) => { + Self::check_logical_expression(logical_expr, ctx); + } + AstKind::BinaryExpression(bin_expr) => { + Self::check_binary_expression(bin_expr, ctx); + } + _ => {} + } } } impl ConstComparisons { - fn check_logical_expression<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) { - let AstKind::LogicalExpression(logical_expr) = node.kind() else { - return; - }; - + fn check_logical_expression<'a>(logical_expr: &LogicalExpression<'a>, ctx: &LintContext<'a>) { Self::check_logical_expression_const_literal_comparison(logical_expr, ctx); Self::check_redundant_logical_expression(logical_expr, ctx); } @@ -256,11 +259,7 @@ impl ConstComparisons { } } - fn check_binary_expression<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) { - let AstKind::BinaryExpression(bin_expr) = node.kind() else { - return; - }; - + fn check_binary_expression<'a>(bin_expr: &BinaryExpression<'a>, ctx: &LintContext<'a>) { if matches!( bin_expr.operator, BinaryOperator::LessEqualThan