From bfe91866110e4b4ead19a0187a16b667e7f42149 Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Mon, 9 Sep 2024 22:00:33 +0800 Subject: [PATCH] perf(linter): use `cow_replace` instead of `replace` (#5643) Related to #5586 --- .../src/rules/eslint/no_loss_of_precision.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs b/crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs index 3f54df91d0b33..b5434ead7e86f 100644 --- a/crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs +++ b/crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; +use cow_utils::CowUtils; use oxc_ast::{ast::NumericLiteral, AstKind}; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; @@ -182,16 +183,9 @@ impl<'a> RawNum<'a> { } impl NoLossOfPrecision { - fn get_raw<'a>(node: &'a NumericLiteral) -> Cow<'a, str> { - if node.raw.contains('_') { - Cow::Owned(node.raw.replace('_', "")) - } else { - Cow::Borrowed(node.raw) - } - } - fn not_base_ten_loses_precision(node: &'_ NumericLiteral) -> bool { - let raw = Self::get_raw(node).to_uppercase(); + let raw = node.raw.cow_replace('_', ""); + let raw = raw.cow_to_uppercase(); #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] // AST always store number as f64, need a cast to format in bin/oct/hex let value = node.value as u64; @@ -206,7 +200,7 @@ impl NoLossOfPrecision { } fn base_ten_loses_precision(node: &'_ NumericLiteral) -> bool { - let raw = Self::get_raw(node); + let raw = node.raw.cow_replace('_', ""); let Some(raw) = Self::normalize(&raw) else { return true; };