Skip to content

Commit be048d2

Browse files
committedApr 4, 2025·
perf(linter): remove write! macro where unnecessary (#10232)
Same as #10230, but for linter. Replace usages of `write!` macro with either `fmt.write_str(...)` for static strings or `Display::fmt` / `Debug::fmt` for other values, where it's not being used to concatenate multiple values. `Formatter::write_str` is more performant, as it avoids various checks. `Display::fmt` / `Debug::fmt` may also perform a little better in some cases, and will be equivalent in others. But in all cases it should be better for compile times, due to avoiding macro expansion and trait resolution.
1 parent ec0035c commit be048d2

File tree

7 files changed

+34
-31
lines changed

7 files changed

+34
-31
lines changed
 

‎crates/oxc_linter/src/config/config_builder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
cell::{Ref, RefCell},
3-
fmt,
3+
fmt::{self, Debug, Display},
44
};
55

66
use itertools::Itertools;
@@ -371,7 +371,7 @@ impl TryFrom<Oxlintrc> for ConfigStoreBuilder {
371371
}
372372
}
373373

374-
impl fmt::Debug for ConfigStoreBuilder {
374+
impl Debug for ConfigStoreBuilder {
375375
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
376376
f.debug_struct("ConfigStoreBuilder")
377377
.field("rules", &self.rules)
@@ -389,13 +389,13 @@ pub enum ConfigBuilderError {
389389
InvalidConfigFile { file: String, reason: String },
390390
}
391391

392-
impl std::fmt::Display for ConfigBuilderError {
393-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
392+
impl Display for ConfigBuilderError {
393+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
394394
match self {
395395
ConfigBuilderError::UnknownRules { rules } => {
396-
write!(f, "unknown rules: ")?;
396+
f.write_str("unknown rules: ")?;
397397
for rule in rules {
398-
write!(f, "{}", rule.full_name())?;
398+
Display::fmt(&rule.full_name(), f)?;
399399
}
400400
Ok(())
401401
}

‎crates/oxc_linter/src/config/globals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Visitor<'_> for GlobalValueVisitor {
115115
type Value = GlobalValue;
116116

117117
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
118-
write!(formatter, "'readonly', 'writable', 'off', or a boolean")
118+
formatter.write_str("'readonly', 'writable', 'off', or a boolean")
119119
}
120120

121121
fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>

‎crates/oxc_linter/src/config/plugins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'de> Deserialize<'de> for LintPlugins {
156156
type Value = LintPlugins;
157157

158158
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
159-
write!(formatter, "a list of plugin names")
159+
formatter.write_str("a list of plugin names")
160160
}
161161

162162
fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {

‎crates/oxc_linter/src/loader/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ impl LoadError {
6666
impl fmt::Display for LoadError {
6767
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6868
match self {
69-
Self::TooLarge => write!(f, "file is too large. Only files up to 4GB are supported."),
70-
Self::NoExtension => write!(f, "no extension"),
69+
Self::TooLarge => f.write_str("file is too large. Only files up to 4GB are supported."),
70+
Self::NoExtension => f.write_str("no extension"),
7171
Self::UnsupportedFileType(ext) => write!(f, "unsupported file type: {ext}"),
7272
}
7373
}

‎crates/oxc_linter/src/rule.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,16 @@ impl TryFrom<&str> for RuleCategory {
148148

149149
impl fmt::Display for RuleCategory {
150150
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151-
match self {
152-
Self::Correctness => write!(f, "Correctness"),
153-
Self::Suspicious => write!(f, "Suspicious"),
154-
Self::Pedantic => write!(f, "Pedantic"),
155-
Self::Perf => write!(f, "Perf"),
156-
Self::Style => write!(f, "Style"),
157-
Self::Restriction => write!(f, "Restriction"),
158-
Self::Nursery => write!(f, "Nursery"),
159-
}
151+
let category_name = match self {
152+
Self::Correctness => "Correctness",
153+
Self::Suspicious => "Suspicious",
154+
Self::Pedantic => "Pedantic",
155+
Self::Perf => "Perf",
156+
Self::Style => "Style",
157+
Self::Restriction => "Restriction",
158+
Self::Nursery => "Nursery",
159+
};
160+
f.write_str(category_name)
160161
}
161162
}
162163

‎crates/oxc_linter/src/rules/eslint/sort_imports.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,13 @@ impl FromStr for ImportKind {
437437

438438
impl Display for ImportKind {
439439
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
440-
match self {
441-
ImportKind::None => write!(f, "None"),
442-
ImportKind::All => write!(f, "All"),
443-
ImportKind::Multiple => write!(f, "Multiple"),
444-
ImportKind::Single => write!(f, "Single"),
445-
}
440+
let kind_name = match self {
441+
ImportKind::None => "None",
442+
ImportKind::All => "All",
443+
ImportKind::Multiple => "Multiple",
444+
ImportKind::Single => "Single",
445+
};
446+
f.write_str(kind_name)
446447
}
447448
}
448449

‎crates/oxc_linter/src/rules/oxc/const_comparisons.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,13 @@ enum CmpOp {
386386

387387
impl std::fmt::Display for CmpOp {
388388
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
389-
match self {
390-
Self::Lt => write!(f, "<"),
391-
Self::Le => write!(f, "<="),
392-
Self::Ge => write!(f, ">="),
393-
Self::Gt => write!(f, ">"),
394-
}
389+
let op = match self {
390+
Self::Lt => "<",
391+
Self::Le => "<=",
392+
Self::Ge => ">=",
393+
Self::Gt => ">",
394+
};
395+
f.write_str(op)
395396
}
396397
}
397398

0 commit comments

Comments
 (0)
Please sign in to comment.