From ddfa3434753022877fdced077c5a72a6695d91dd Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Thu, 11 Jul 2024 01:44:13 +0000 Subject: [PATCH] perf(diagnostic): use `Cow<'static, str>` over `String` (#4175) Allows us to use `&'static str` for error and help messages without allocating them into `String`s. --- crates/oxc_diagnostics/src/lib.rs | 11 ++++++----- .../oxc_linter/src/rules/oxc/no_optional_chaining.rs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/oxc_diagnostics/src/lib.rs b/crates/oxc_diagnostics/src/lib.rs index e955dab7cc23..34fa0ed9945b 100644 --- a/crates/oxc_diagnostics/src/lib.rs +++ b/crates/oxc_diagnostics/src/lib.rs @@ -7,6 +7,7 @@ mod reporter; mod service; use std::{ + borrow::Cow, fmt::{self, Display}, ops::Deref, }; @@ -42,9 +43,9 @@ impl Deref for OxcDiagnostic { #[derive(Debug, Clone)] pub struct OxcDiagnosticInner { - pub message: String, + pub message: Cow<'static, str>, pub labels: Option>, - pub help: Option, + pub help: Option>, pub severity: Severity, } @@ -76,7 +77,7 @@ impl Diagnostic for OxcDiagnostic { impl OxcDiagnostic { #[must_use] - pub fn error>(message: T) -> Self { + pub fn error>>(message: T) -> Self { Self { inner: Box::new(OxcDiagnosticInner { message: message.into(), @@ -88,7 +89,7 @@ impl OxcDiagnostic { } #[must_use] - pub fn warn>(message: T) -> Self { + pub fn warn>>(message: T) -> Self { Self { inner: Box::new(OxcDiagnosticInner { message: message.into(), @@ -106,7 +107,7 @@ impl OxcDiagnostic { } #[must_use] - pub fn with_help>(mut self, help: T) -> Self { + pub fn with_help>>(mut self, help: T) -> Self { self.inner.help = Some(help.into()); self } diff --git a/crates/oxc_linter/src/rules/oxc/no_optional_chaining.rs b/crates/oxc_linter/src/rules/oxc/no_optional_chaining.rs index 363d0f5b5bd8..b2e967e648cb 100644 --- a/crates/oxc_linter/src/rules/oxc/no_optional_chaining.rs +++ b/crates/oxc_linter/src/rules/oxc/no_optional_chaining.rs @@ -11,7 +11,7 @@ fn no_optional_chaining_diagnostic(span0: Span, x1: &str) -> OxcDiagnostic { .with_label(span0) } else { OxcDiagnostic::warn("oxc(no-optional-chaining): Optional chaining is not allowed.") - .with_help(x1) + .with_help(x1.to_owned()) .with_label(span0) } }