Skip to content

Commit

Permalink
perf(diagnostic): use Cow<'static, str> over String (#4175)
Browse files Browse the repository at this point in the history
Allows us to use `&'static str` for  error and help messages without allocating them into `String`s.
  • Loading branch information
DonIsaac committed Jul 11, 2024
1 parent 7a059ab commit ddfa343
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions crates/oxc_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod reporter;
mod service;

use std::{
borrow::Cow,
fmt::{self, Display},
ops::Deref,
};
Expand Down Expand Up @@ -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<Vec<LabeledSpan>>,
pub help: Option<String>,
pub help: Option<Cow<'static, str>>,
pub severity: Severity,
}

Expand Down Expand Up @@ -76,7 +77,7 @@ impl Diagnostic for OxcDiagnostic {

impl OxcDiagnostic {
#[must_use]
pub fn error<T: Into<String>>(message: T) -> Self {
pub fn error<T: Into<Cow<'static, str>>>(message: T) -> Self {
Self {
inner: Box::new(OxcDiagnosticInner {
message: message.into(),
Expand All @@ -88,7 +89,7 @@ impl OxcDiagnostic {
}

#[must_use]
pub fn warn<T: Into<String>>(message: T) -> Self {
pub fn warn<T: Into<Cow<'static, str>>>(message: T) -> Self {
Self {
inner: Box::new(OxcDiagnosticInner {
message: message.into(),
Expand All @@ -106,7 +107,7 @@ impl OxcDiagnostic {
}

#[must_use]
pub fn with_help<T: Into<String>>(mut self, help: T) -> Self {
pub fn with_help<T: Into<Cow<'static, str>>>(mut self, help: T) -> Self {
self.inner.help = Some(help.into());
self
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/oxc/no_optional_chaining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit ddfa343

Please sign in to comment.