misc: clippy: add needless_pass_by_value (partially)

This helps to uncover expensive and needless clones in the code base.
For example, I prevented extensive clones in the snapshot path where
(nested) BTreeMap's have been cloned over and over again. Further,
the lint helps devs to much better reason about the ownership of
parameters.

All of these changes have been done manually with the necessary
caution. A few structs that are cheap to clone are now `copy` so that
this lint won't trigger for them.

I didn't enable the lint so far as it is a massive rabbit hole and
needs much more fixes. Nevertheless, it is very useful.

Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster
2025-11-25 14:29:36 +01:00
committed by Rob Bradford
parent 0a07c96d17
commit 6a86c157af
29 changed files with 162 additions and 155 deletions

View File

@@ -800,8 +800,8 @@ impl Block {
self.disk_path.display()
);
let fd = self.disk_image.fd();
fcntl::try_acquire_lock(fd, lock_type, granularity).map_err(|error| {
let current_lock = get_lock_state(fd, granularity);
fcntl::try_acquire_lock(&fd, lock_type, granularity).map_err(|error| {
let current_lock = get_lock_state(&fd, granularity);
// Don't propagate the error to the outside, as it is not useful at all. Instead,
// we try to log additional help to the user.
if let Ok(current_lock) = current_lock {
@@ -830,7 +830,8 @@ impl Block {
// It is very unlikely that this fails;
// Should we remove the Result to simplify the error propagation on
// higher levels?
fcntl::clear_lock(self.disk_image.fd(), granularity).map_err(|error| Error::LockDiskImage {
let fd = self.disk_image.fd();
fcntl::clear_lock(&fd, granularity).map_err(|error| Error::LockDiskImage {
path: self.disk_path.clone(),
error,
lock_type: LockType::Unlock,

View File

@@ -389,7 +389,7 @@ impl VirtioPciDevice {
use_64bit_bar: bool,
dma_handler: Option<Arc<dyn ExternalDmaMapping>>,
pending_activations: Arc<Mutex<Vec<VirtioPciDeviceActivator>>>,
snapshot: Option<Snapshot>,
snapshot: Option<&Snapshot>,
) -> Result<Self> {
let mut locked_device = device.lock().unwrap();
let mut queue_evts = Vec::new();
@@ -423,8 +423,8 @@ impl VirtioPciDevice {
))
})?;
let msix_state = vm_migration::state_from_id(snapshot.as_ref(), pci::MSIX_CONFIG_ID)
.map_err(|e| {
let msix_state =
vm_migration::state_from_id(snapshot, pci::MSIX_CONFIG_ID).map_err(|e| {
VirtioPciDeviceError::CreateVirtioPciDevice(anyhow!(
"Failed to get MsixConfigState from Snapshot: {e}"
))
@@ -462,13 +462,11 @@ impl VirtioPciDevice {
};
let pci_configuration_state =
vm_migration::state_from_id(snapshot.as_ref(), pci::PCI_CONFIGURATION_ID).map_err(
|e| {
VirtioPciDeviceError::CreateVirtioPciDevice(anyhow!(
"Failed to get PciConfigurationState from Snapshot: {e}"
))
},
)?;
vm_migration::state_from_id(snapshot, pci::PCI_CONFIGURATION_ID).map_err(|e| {
VirtioPciDeviceError::CreateVirtioPciDevice(anyhow!(
"Failed to get PciConfigurationState from Snapshot: {e}"
))
})?;
let configuration = PciConfiguration::new(
VIRTIO_PCI_VENDOR_ID,
@@ -485,13 +483,11 @@ impl VirtioPciDevice {
);
let common_config_state =
vm_migration::state_from_id(snapshot.as_ref(), VIRTIO_PCI_COMMON_CONFIG_ID).map_err(
|e| {
VirtioPciDeviceError::CreateVirtioPciDevice(anyhow!(
"Failed to get VirtioPciCommonConfigState from Snapshot: {e}"
))
},
)?;
vm_migration::state_from_id(snapshot, VIRTIO_PCI_COMMON_CONFIG_ID).map_err(|e| {
VirtioPciDeviceError::CreateVirtioPciDevice(anyhow!(
"Failed to get VirtioPciCommonConfigState from Snapshot: {e}"
))
})?;
let common_config = if let Some(common_config_state) = common_config_state {
VirtioPciCommonConfig::new(common_config_state, access_platform)

View File

@@ -219,7 +219,7 @@ impl Vdpa {
&mut self,
mem: &GuestMemoryMmap,
virtio_interrupt: &dyn VirtioInterrupt,
queues: Vec<(usize, Queue, EventFd)>,
queues: &[(usize, Queue, EventFd)],
) -> Result<()> {
assert!(self.vhost.is_some());
self.vhost
@@ -434,7 +434,7 @@ impl VirtioDevice for Vdpa {
virtio_interrupt: Arc<dyn VirtioInterrupt>,
queues: Vec<(usize, Queue, EventFd)>,
) -> ActivateResult {
self.activate_vdpa(&mem.memory(), virtio_interrupt.as_ref(), queues)
self.activate_vdpa(&mem.memory(), virtio_interrupt.as_ref(), &queues)
.map_err(ActivateError::ActivateVdpa)?;
// Store the virtio interrupt handler as we need to return it on reset