From c1774a7c6ccbdb5c920555829abae3fd8bfd1483 Mon Sep 17 00:00:00 2001 From: Riccardo Mancini Date: Mon, 11 Aug 2025 17:38:53 +0100 Subject: [PATCH 1/2] refactor(pcie): avoid string format on good path If a function is called in the `.ok_or`, this gets executed independently on whether the function succeeds or not, although the result is only used on failure. Replace it with a .ok_or_else to avoid a useless string allocation and memcpy. Signed-off-by: Riccardo Mancini --- src/vmm/src/vstate/vm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vmm/src/vstate/vm.rs b/src/vmm/src/vstate/vm.rs index aeb61f88c56..04ad9bc7907 100644 --- a/src/vmm/src/vstate/vm.rs +++ b/src/vmm/src/vstate/vm.rs @@ -170,7 +170,7 @@ impl InterruptSourceGroup for MsiVectorGroup { fn trigger(&self, index: InterruptIndex) -> vm_device::interrupt::Result<()> { self.notifier(index) - .ok_or(std::io::Error::other(format!( + .ok_or_else(|| std::io::Error::other(format!( "trigger: invalid interrupt index {index}" )))? .write(1) From 393bdf81ca5dfb55a9c7cf437ce18dbe4f26affe Mon Sep 17 00:00:00 2001 From: Riccardo Mancini Date: Mon, 11 Aug 2025 17:41:09 +0100 Subject: [PATCH 2/2] chore(clippy): enable warn for or_fun_call "or_fun_call" warns us when a function is called in side a "*_or(...)" function. In this case the value is computed (which may be expensive), but is only used if the Result/Option is an Error/None. In these cases, using the "*_or_else" variant is the best thing to do. Signed-off-by: Riccardo Mancini --- Cargo.toml | 1 + src/clippy-tracing/src/main.rs | 2 +- src/cpu-template-helper/src/template/verify/mod.rs | 2 +- src/vmm/src/vstate/vm.rs | 6 +++--- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 37a76cdd34f..a1c9ad79621 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ exit = "warn" tests_outside_test_module = "warn" assertions_on_result_states = "warn" error_impl_error = "warn" +or_fun_call = "warn" [profile.dev] panic = "abort" diff --git a/src/clippy-tracing/src/main.rs b/src/clippy-tracing/src/main.rs index c89fb6a5d37..721fca12b25 100644 --- a/src/clippy-tracing/src/main.rs +++ b/src/clippy-tracing/src/main.rs @@ -260,7 +260,7 @@ impl Error for ExecError {} fn exec() -> Result, ExecError> { let args = CommandLineArgs::parse(); - let path = args.path.unwrap_or(PathBuf::from(".")); + let path = args.path.unwrap_or_else(|| PathBuf::from(".")); for entry_res in WalkDir::new(path).follow_links(true) { let entry = entry_res.map_err(ExecError::Entry)?; let entry_path = entry.into_path(); diff --git a/src/cpu-template-helper/src/template/verify/mod.rs b/src/cpu-template-helper/src/template/verify/mod.rs index 1a83f6ba1b2..1f42e2f06cc 100644 --- a/src/cpu-template-helper/src/template/verify/mod.rs +++ b/src/cpu-template-helper/src/template/verify/mod.rs @@ -43,7 +43,7 @@ where for (key, template_value_filter) in template { let config_value_filter = config .get(&key) - .ok_or(VerifyError::KeyNotFound(key.to_string()))?; + .ok_or_else(|| VerifyError::KeyNotFound(key.to_string()))?; let template_value = template_value_filter.value & template_value_filter.filter; let config_value = config_value_filter.value & template_value_filter.filter; diff --git a/src/vmm/src/vstate/vm.rs b/src/vmm/src/vstate/vm.rs index 04ad9bc7907..8c4049f9e0c 100644 --- a/src/vmm/src/vstate/vm.rs +++ b/src/vmm/src/vstate/vm.rs @@ -170,9 +170,9 @@ impl InterruptSourceGroup for MsiVectorGroup { fn trigger(&self, index: InterruptIndex) -> vm_device::interrupt::Result<()> { self.notifier(index) - .ok_or_else(|| std::io::Error::other(format!( - "trigger: invalid interrupt index {index}" - )))? + .ok_or_else(|| { + std::io::Error::other(format!("trigger: invalid interrupt index {index}")) + })? .write(1) }