From 1d77d92a41d9e2e3a16d725761997817a24d6955 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 14 Aug 2025 20:52:59 +0000 Subject: [PATCH] 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. --- crates/oxc_linter/src/service/runtime.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index 6941a81278cd4..f5deb42815b1d 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -887,13 +887,13 @@ impl Runtime { return None; } - let mut records = SmallVec::<[Result>; 1]>::new(); - let mut module_content: Option = None; + let allocator_guard = self.allocator_pool.get(); if self.paths.contains(path) { - let allocator_guard = self.allocator_pool.get(); + let mut records = + SmallVec::<[Result>; 1]>::new(); - let mc = ModuleContent::try_new(allocator_guard, |allocator| { + let module_content = ModuleContent::try_new(allocator_guard, |allocator| { let Some(stt) = self.get_source_type_and_text(Path::new(path), ext, allocator) else { return Err(()); @@ -920,11 +920,10 @@ impl Runtime { Ok(ModuleContentDependent { source_text, section_contents }) }); - let mc = mc.ok()?; + let module_content = module_content.ok()?; - module_content = Some(mc); + Some(ProcessedModule { section_module_records: records, content: Some(module_content) }) } else { - let allocator_guard = self.allocator_pool.get(); let allocator = &*allocator_guard; let stt = self.get_source_type_and_text(Path::new(path), ext, allocator)?; @@ -937,7 +936,7 @@ impl Runtime { } }; - records = self.process_source( + let records = self.process_source( Path::new(path), ext, check_syntax_errors, @@ -946,9 +945,9 @@ impl Runtime { allocator, None, ); - } - Some(ProcessedModule { section_module_records: records, content: module_content }) + Some(ProcessedModule { section_module_records: records, content: None }) + } } #[expect(clippy::too_many_arguments)]