Skip to content

Commit 1d77d92

Browse files
committed
refactor(linter): avoid unnecessary var initialization (#13072)
Further refactor of `process_path`, continuing from #13070. Avoid unnecessary pre-initialization of `records` and `module_content`, when mostly they're overwritten later. Instead, each branch of the `if` / `else` hold their own vars, and construct a `ProcessedModule`. There may be some tiny perf benefit of skipping the initializations, but mainly motivation is to make it more explicit what each branch returns, which personally I find makes the logic easier to follow. Also de-duplicate creation of `allocator_guard`, which was previously repeated in both branches.
1 parent 1c15288 commit 1d77d92

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

crates/oxc_linter/src/service/runtime.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -887,13 +887,13 @@ impl Runtime {
887887
return None;
888888
}
889889

890-
let mut records = SmallVec::<[Result<ResolvedModuleRecord, Vec<OxcDiagnostic>>; 1]>::new();
891-
let mut module_content: Option<ModuleContent> = None;
890+
let allocator_guard = self.allocator_pool.get();
892891

893892
if self.paths.contains(path) {
894-
let allocator_guard = self.allocator_pool.get();
893+
let mut records =
894+
SmallVec::<[Result<ResolvedModuleRecord, Vec<OxcDiagnostic>>; 1]>::new();
895895

896-
let mc = ModuleContent::try_new(allocator_guard, |allocator| {
896+
let module_content = ModuleContent::try_new(allocator_guard, |allocator| {
897897
let Some(stt) = self.get_source_type_and_text(Path::new(path), ext, allocator)
898898
else {
899899
return Err(());
@@ -920,11 +920,10 @@ impl Runtime {
920920

921921
Ok(ModuleContentDependent { source_text, section_contents })
922922
});
923-
let mc = mc.ok()?;
923+
let module_content = module_content.ok()?;
924924

925-
module_content = Some(mc);
925+
Some(ProcessedModule { section_module_records: records, content: Some(module_content) })
926926
} else {
927-
let allocator_guard = self.allocator_pool.get();
928927
let allocator = &*allocator_guard;
929928

930929
let stt = self.get_source_type_and_text(Path::new(path), ext, allocator)?;
@@ -937,7 +936,7 @@ impl Runtime {
937936
}
938937
};
939938

940-
records = self.process_source(
939+
let records = self.process_source(
941940
Path::new(path),
942941
ext,
943942
check_syntax_errors,
@@ -946,9 +945,9 @@ impl Runtime {
946945
allocator,
947946
None,
948947
);
949-
}
950948

951-
Some(ProcessedModule { section_module_records: records, content: module_content })
949+
Some(ProcessedModule { section_module_records: records, content: None })
950+
}
952951
}
953952

954953
#[expect(clippy::too_many_arguments)]

0 commit comments

Comments
 (0)