From ea4693a09123234951ae1516f112c5cfce5032ca Mon Sep 17 00:00:00 2001 From: Jinank Jain Date: Wed, 2 Apr 2025 07:44:40 +0000 Subject: [PATCH] misc: Fix clippy error from beta compiler Rust has a new way of constructing other error and clippy complains if we are still using the older way to construct error message. Thus, migrate to the new approach suggested by the clippy. Warning from beta compiler: error: this can be `std::io::Error::other(_)` --> block/src/vhdx/mod.rs:142:17 | | / std::io::Error::new( | | std::io::ErrorKind::Other, | | format!("Failed to update VHDx header: {e}"), | | ) | |_________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#io_other_error help: use `std::io::Error::other` std::io::Error::other( format!("Failed to update VHDx header: {e}"), Signed-off-by: Jinank Jain --- block/src/qcow/mod.rs | 3 +- block/src/vhdx/mod.rs | 27 +++++-------- net_util/src/mac.rs | 26 ++++++------ pci/src/vfio.rs | 39 +++++++----------- pci/src/vfio_user.rs | 25 ++++-------- test_infra/src/lib.rs | 3 +- vhost_user_block/src/lib.rs | 2 +- vhost_user_net/src/lib.rs | 2 +- virtio-devices/src/block.rs | 9 ++--- virtio-devices/src/iommu.rs | 14 +++---- virtio-devices/src/mem.rs | 21 ++++------ virtio-devices/src/vdpa.rs | 27 +++++-------- virtio-devices/src/vhost_user/mod.rs | 14 +++---- vm-device/src/bus.rs | 2 +- vmm/src/device_manager.rs | 60 +++++++++------------------- vmm/src/interrupt.rs | 48 +++++++--------------- 16 files changed, 114 insertions(+), 208 deletions(-) diff --git a/block/src/qcow/mod.rs b/block/src/qcow/mod.rs index 4a6cb6c15..266f54fcc 100644 --- a/block/src/qcow/mod.rs +++ b/block/src/qcow/mod.rs @@ -1625,8 +1625,7 @@ impl FileSync for QcowFile { impl FileSetLen for QcowFile { fn set_len(&self, _len: u64) -> std::io::Result<()> { - Err(std::io::Error::new( - std::io::ErrorKind::Other, + Err(std::io::Error::other( "set_len() not supported for QcowFile", )) } diff --git a/block/src/vhdx/mod.rs b/block/src/vhdx/mod.rs index 953dcc76e..c8dbd4055 100644 --- a/block/src/vhdx/mod.rs +++ b/block/src/vhdx/mod.rs @@ -111,12 +111,9 @@ impl Read for Vhdx { sector_count, ) .map_err(|e| { - std::io::Error::new( - std::io::ErrorKind::Other, - format!( - "Failed reading {sector_count} sectors from VHDx at index {sector_index}: {e}" - ), - ) + std::io::Error::other(format!( + "Failed reading {sector_count} sectors from VHDx at index {sector_index}: {e}" + )) })?; self.current_offset = self.current_offset.checked_add(result as u64).unwrap(); @@ -138,12 +135,9 @@ impl Write for Vhdx { if self.first_write { self.first_write = false; - self.vhdx_header.update(&mut self.file).map_err(|e| { - std::io::Error::new( - std::io::ErrorKind::Other, - format!("Failed to update VHDx header: {e}"), - ) - })?; + self.vhdx_header + .update(&mut self.file) + .map_err(|e| std::io::Error::other(format!("Failed to update VHDx header: {e}")))?; } let result = vhdx_io::write( @@ -156,12 +150,9 @@ impl Write for Vhdx { sector_count, ) .map_err(|e| { - std::io::Error::new( - std::io::ErrorKind::Other, - format!( - "Failed writing {sector_count} sectors on VHDx at index {sector_index}: {e}" - ), - ) + std::io::Error::other(format!( + "Failed writing {sector_count} sectors on VHDx at index {sector_index}: {e}" + )) })?; self.current_offset = self.current_offset.checked_add(result as u64).unwrap(); diff --git a/net_util/src/mac.rs b/net_util/src/mac.rs index a1ed6a764..4bd1ac38f 100644 --- a/net_util/src/mac.rs +++ b/net_util/src/mac.rs @@ -25,10 +25,10 @@ impl MacAddr { { let v: Vec<&str> = s.as_ref().split(':').collect(); let mut bytes = [0u8; MAC_ADDR_LEN]; - let common_err = Err(io::Error::new( - io::ErrorKind::Other, - format!("parsing of {} into a MAC address failed", s.as_ref()), - )); + let common_err = Err(io::Error::other(format!( + "parsing of {} into a MAC address failed", + s.as_ref() + ))); if v.len() != MAC_ADDR_LEN { return common_err; @@ -39,10 +39,11 @@ impl MacAddr { return common_err; } bytes[i] = u8::from_str_radix(v[i], 16).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("parsing of {} into a MAC address failed: {}", s.as_ref(), e), - ) + io::Error::other(format!( + "parsing of {} into a MAC address failed: {}", + s.as_ref(), + e + )) })?; } @@ -64,10 +65,11 @@ impl MacAddr { #[inline] pub fn from_bytes(src: &[u8]) -> Result { if src.len() != MAC_ADDR_LEN { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("invalid length of slice: {} vs {}", src.len(), MAC_ADDR_LEN), - )); + return Err(io::Error::other(format!( + "invalid length of slice: {} vs {}", + src.len(), + MAC_ADDR_LEN + ))); } Ok(MacAddr::from_bytes_unchecked(src)) } diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index 41481ad5d..bad337f1e 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -310,10 +310,9 @@ impl MmioRegionRange for Vec { } } - Err(io::Error::new( - io::ErrorKind::Other, - format!("unable to find user address: 0x{guest_addr:x}"), - )) + Err(io::Error::other(format!( + "unable to find user address: 0x{guest_addr:x}" + ))) } } @@ -1872,7 +1871,7 @@ impl PciDevice for VfioPciDevice { self.vm .remove_user_memory_region(old_mem_region) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(io::Error::other)?; // Update the user memory region with the correct start address. if new_base > old_base { @@ -1893,7 +1892,7 @@ impl PciDevice for VfioPciDevice { self.vm .create_user_memory_region(new_mem_region) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(io::Error::other)?; } } } @@ -1965,8 +1964,7 @@ impl ExternalDmaMapping for VfioDmaMapping t as u64, Err(e) => { - return Err(io::Error::new( - io::ErrorKind::Other, + return Err(io::Error::other( format!("unable to retrieve user address for gpa 0x{gpa:x} from guest memory region: {e}") )); } @@ -1974,34 +1972,27 @@ impl ExternalDmaMapping for VfioDmaMapping std::result::Result<(), io::Error> { self.container.vfio_dma_unmap(iova, size).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!( - "failed to unmap memory for VFIO container, \ + io::Error::other(format!( + "failed to unmap memory for VFIO container, \ iova 0x{iova:x}, size 0x{size:x}: {e:?}" - ), - ) + )) }) } } diff --git a/pci/src/vfio_user.rs b/pci/src/vfio_user.rs index f2c723b10..cde2aec08 100644 --- a/pci/src/vfio_user.rs +++ b/pci/src/vfio_user.rs @@ -476,7 +476,7 @@ impl PciDevice for VfioUserPciDevice { self.vm .remove_user_memory_region(old_region) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; + .map_err(std::io::Error::other)?; // Update the user memory region with the correct start address. if new_base > old_base { @@ -497,7 +497,7 @@ impl PciDevice for VfioUserPciDevice { self.vm .create_user_memory_region(new_region) - .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; + .map_err(std::io::Error::other)?; } info!("Moved bar 0x{:x} -> 0x{:x}", old_base, new_base); } @@ -582,17 +582,11 @@ impl ExternalDmaMapping for VfioUserDmaMappi .lock() .unwrap() .dma_map(offset, iova, size, file_offset.file().as_raw_fd()) - .map_err(|e| { - std::io::Error::new( - std::io::ErrorKind::Other, - format!("Error mapping region: {e}"), - ) - }) + .map_err(|e| std::io::Error::other(format!("Error mapping region: {e}"))) } else { - Err(std::io::Error::new( - std::io::ErrorKind::Other, - format!("Region not found for 0x{gpa:x}"), - )) + Err(std::io::Error::other(format!( + "Region not found for 0x{gpa:x}" + ))) } } @@ -601,11 +595,6 @@ impl ExternalDmaMapping for VfioUserDmaMappi .lock() .unwrap() .dma_unmap(iova, size) - .map_err(|e| { - std::io::Error::new( - std::io::ErrorKind::Other, - format!("Error unmapping region: {e}"), - ) - }) + .map_err(|e| std::io::Error::other(format!("Error unmapping region: {e}"))) } } diff --git a/test_infra/src/lib.rs b/test_infra/src/lib.rs index 35cf35b49..57605b143 100644 --- a/test_infra/src/lib.rs +++ b/test_infra/src/lib.rs @@ -1328,8 +1328,7 @@ impl<'a> GuestCommand<'a> { if pipesize >= PIPE_SIZE && pipesize1 >= PIPE_SIZE { Ok(child) } else { - Err(std::io::Error::new( - std::io::ErrorKind::Other, + Err(std::io::Error::other( format!( "resizing pipe w/ 'fnctl' failed: stdout pipesize {pipesize}, stderr pipesize {pipesize1}" ), diff --git a/vhost_user_block/src/lib.rs b/vhost_user_block/src/lib.rs index abfb58964..d17e24082 100644 --- a/vhost_user_block/src/lib.rs +++ b/vhost_user_block/src/lib.rs @@ -83,7 +83,7 @@ impl error::Error for Error {} impl convert::From for io::Error { fn from(e: Error) -> Self { - io::Error::new(io::ErrorKind::Other, e) + io::Error::other(e) } } diff --git a/vhost_user_net/src/lib.rs b/vhost_user_net/src/lib.rs index 4339b95f9..4f252dcd7 100644 --- a/vhost_user_net/src/lib.rs +++ b/vhost_user_net/src/lib.rs @@ -69,7 +69,7 @@ impl std::error::Error for Error {} impl std::convert::From for std::io::Error { fn from(e: Error) -> Self { - std::io::Error::new(io::ErrorKind::Other, e) + std::io::Error::other(e) } } diff --git a/virtio-devices/src/block.rs b/virtio-devices/src/block.rs index d914d2710..f84cd40cc 100644 --- a/virtio-devices/src/block.rs +++ b/virtio-devices/src/block.rs @@ -608,12 +608,9 @@ impl Block { true, ) } else { - let disk_size = disk_image.size().map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Failed getting disk size: {e}"), - ) - })?; + let disk_size = disk_image + .size() + .map_err(|e| io::Error::other(format!("Failed getting disk size: {e}")))?; if disk_size % SECTOR_SIZE != 0 { warn!( "Disk size {} is not a multiple of sector size {}; \ diff --git a/virtio-devices/src/iommu.rs b/virtio-devices/src/iommu.rs index 0b8309a92..165daeb5e 100644 --- a/virtio-devices/src/iommu.rs +++ b/virtio-devices/src/iommu.rs @@ -822,10 +822,9 @@ impl DmaRemapping for IommuMapping { return Ok(addr); } - Err(io::Error::new( - io::ErrorKind::Other, - format!("failed to translate GVA addr 0x{addr:x}"), - )) + Err(io::Error::other(format!( + "failed to translate GVA addr 0x{addr:x}" + ))) } fn translate_gpa(&self, id: u32, addr: u64) -> std::result::Result { @@ -850,10 +849,9 @@ impl DmaRemapping for IommuMapping { return Ok(addr); } - Err(io::Error::new( - io::ErrorKind::Other, - format!("failed to translate GPA addr 0x{addr:x}"), - )) + Err(io::Error::other(format!( + "failed to translate GPA addr 0x{addr:x}" + ))) } } diff --git a/virtio-devices/src/mem.rs b/virtio-devices/src/mem.rs index 034015a85..c28043d78 100644 --- a/virtio-devices/src/mem.rs +++ b/virtio-devices/src/mem.rs @@ -723,10 +723,9 @@ impl Mem { let region_len = region.len(); if region_len != region_len / VIRTIO_MEM_ALIGN_SIZE * VIRTIO_MEM_ALIGN_SIZE { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("Virtio-mem size is not aligned with {VIRTIO_MEM_ALIGN_SIZE}"), - )); + return Err(io::Error::other(format!( + "Virtio-mem size is not aligned with {VIRTIO_MEM_ALIGN_SIZE}" + ))); } let (avail_features, acked_features, config, paused) = if let Some(state) = state { @@ -753,12 +752,9 @@ impl Mem { if initial_size != 0 { config.resize(initial_size).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!( - "Failed to resize virtio-mem configuration to {initial_size}: {e:?}" - ), - ) + io::Error::other(format!( + "Failed to resize virtio-mem configuration to {initial_size}: {e:?}" + )) })?; } @@ -770,10 +766,7 @@ impl Mem { // Make sure the virtio-mem configuration complies with the // specification. config.validate().map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Invalid virtio-mem configuration: {e:?}"), - ) + io::Error::other(format!("Invalid virtio-mem configuration: {e:?}")) })?; (avail_features, 0, config, false) diff --git a/virtio-devices/src/vdpa.rs b/virtio-devices/src/vdpa.rs index 4f3ac1e15..a95fcc967 100644 --- a/virtio-devices/src/vdpa.rs +++ b/virtio-devices/src/vdpa.rs @@ -541,13 +541,10 @@ impl ExternalDmaMapping for VdpaDmaMapping ExternalDmaMapping for VdpaDmaMapping ExternalDmaMapping for VdpaDmaMapping VhostUserEpollHandler { true, ) .map_err(|e| { - EpollHelperError::IoError(std::io::Error::new( - std::io::ErrorKind::Other, - format!("failed connecting vhost-user backend {e:?}"), - )) + EpollHelperError::IoError(std::io::Error::other(format!( + "failed connecting vhost-user backend {e:?}" + ))) })?; // Initialize the backend @@ -236,10 +235,9 @@ impl VhostUserEpollHandler { self.inflight.as_mut(), ) .map_err(|e| { - EpollHelperError::IoError(std::io::Error::new( - std::io::ErrorKind::Other, - format!("failed reconnecting vhost-user backend: {e:?}"), - )) + EpollHelperError::IoError(std::io::Error::other(format!( + "failed reconnecting vhost-user backend: {e:?}" + ))) })?; helper.add_event_custom( diff --git a/vm-device/src/bus.rs b/vm-device/src/bus.rs index 3817d443f..ff7c1f8c5 100644 --- a/vm-device/src/bus.rs +++ b/vm-device/src/bus.rs @@ -73,7 +73,7 @@ impl error::Error for Error {} impl convert::From for io::Error { fn from(e: Error) -> Self { - io::Error::new(io::ErrorKind::Other, e) + io::Error::other(e) } } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 519ab7b1c..ac4fb541b 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -573,14 +573,12 @@ impl DeviceRelocation for AddressManager { .lock() .unwrap() .allocate_io_addresses(Some(GuestAddress(new_base)), len as GuestUsize, None) - .ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "failed allocating new IO range") - })?; + .ok_or_else(|| io::Error::other("failed allocating new IO range"))?; // Update PIO bus self.io_bus .update_range(old_base, len, new_base, len) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(io::Error::other)?; } PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => { let allocators = if region_type == PciBarRegionType::Memory32BitRegion { @@ -604,12 +602,7 @@ impl DeviceRelocation for AddressManager { .lock() .unwrap() .allocate(Some(GuestAddress(new_base)), len as GuestUsize, Some(len)) - .ok_or_else(|| { - io::Error::new( - io::ErrorKind::Other, - "failed allocating new MMIO range", - ) - })?; + .ok_or_else(|| io::Error::other("failed allocating new MMIO range"))?; break; } @@ -618,7 +611,7 @@ impl DeviceRelocation for AddressManager { // Update MMIO bus self.mmio_bus .update_range(old_base, len, new_base, len) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(io::Error::other)?; } } @@ -637,18 +630,14 @@ impl DeviceRelocation for AddressManager { } if !resource_updated { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "Couldn't find a resource with base 0x{old_base:x} for device {id}" - ), - )); + return Err(io::Error::other(format!( + "Couldn't find a resource with base 0x{old_base:x} for device {id}" + ))); } } else { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("Couldn't find device {id} from device tree"), - )); + return Err(io::Error::other(format!( + "Couldn't find device {id} from device tree" + ))); } } @@ -659,10 +648,7 @@ impl DeviceRelocation for AddressManager { for (event, addr) in virtio_pci_dev.ioeventfds(old_base) { let io_addr = IoEventAddress::Mmio(addr); self.vm.unregister_ioevent(event, &io_addr).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("failed to unregister ioevent: {e:?}"), - ) + io::Error::other(format!("failed to unregister ioevent: {e:?}")) })?; } for (event, addr) in virtio_pci_dev.ioeventfds(new_base) { @@ -670,10 +656,7 @@ impl DeviceRelocation for AddressManager { self.vm .register_ioevent(event, &io_addr, None) .map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("failed to register ioevent: {e:?}"), - ) + io::Error::other(format!("failed to register ioevent: {e:?}")) })?; } } else { @@ -691,10 +674,7 @@ impl DeviceRelocation for AddressManager { ); self.vm.remove_user_memory_region(mem_region).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("failed to remove user memory region: {e:?}"), - ) + io::Error::other(format!("failed to remove user memory region: {e:?}")) })?; // Create new mapping by inserting new region to KVM. @@ -708,19 +688,15 @@ impl DeviceRelocation for AddressManager { ); self.vm.create_user_memory_region(mem_region).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("failed to create user memory regions: {e:?}"), - ) + io::Error::other(format!("failed to create user memory regions: {e:?}")) })?; // Update shared memory regions to reflect the new mapping. shm_regions.addr = GuestAddress(new_base); virtio_dev.set_shm_regions(shm_regions).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("failed to update shared memory regions: {e:?}"), - ) + io::Error::other(format!( + "failed to update shared memory regions: {e:?}" + )) })?; } } @@ -804,7 +780,7 @@ impl AccessPlatform for SevSnpPageAccessProxy { fn translate_gva(&self, base: u64, size: u64) -> std::result::Result { self.vm .gain_page_access(base, size as u32) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(io::Error::other)?; Ok(base) } } diff --git a/vmm/src/interrupt.rs b/vmm/src/interrupt.rs index 1655029c8..8bafd800a 100644 --- a/vmm/src/interrupt.rs +++ b/vmm/src/interrupt.rs @@ -31,7 +31,7 @@ impl InterruptRoute { let irq_fd = EventFd::new(libc::EFD_NONBLOCK)?; let gsi = allocator .allocate_gsi() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Failed allocating new GSI"))?; + .ok_or_else(|| io::Error::other("Failed allocating new GSI"))?; Ok(InterruptRoute { gsi, @@ -42,12 +42,8 @@ impl InterruptRoute { pub fn enable(&self, vm: &Arc) -> Result<()> { if !self.registered.load(Ordering::Acquire) { - vm.register_irqfd(&self.irq_fd, self.gsi).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Failed registering irq_fd: {e}"), - ) - })?; + vm.register_irqfd(&self.irq_fd, self.gsi) + .map_err(|e| io::Error::other(format!("Failed registering irq_fd: {e}")))?; // Update internals to track the irq_fd as "registered". self.registered.store(true, Ordering::Release); @@ -58,12 +54,8 @@ impl InterruptRoute { pub fn disable(&self, vm: &Arc) -> Result<()> { if self.registered.load(Ordering::Acquire) { - vm.unregister_irqfd(&self.irq_fd, self.gsi).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Failed unregistering irq_fd: {e}"), - ) - })?; + vm.unregister_irqfd(&self.irq_fd, self.gsi) + .map_err(|e| io::Error::other(format!("Failed unregistering irq_fd: {e}")))?; // Update internals to track the irq_fd as "unregistered". self.registered.store(false, Ordering::Release); @@ -107,12 +99,9 @@ impl MsiInterruptGroup { entry_vec.push(entry.route); } - self.vm.set_gsi_routing(&entry_vec).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Failed setting GSI routing: {e}"), - ) - }) + self.vm + .set_gsi_routing(&entry_vec) + .map_err(|e| io::Error::other(format!("Failed setting GSI routing: {e}"))) } } @@ -152,10 +141,9 @@ impl InterruptSourceGroup for MsiInterruptGroup { return route.trigger(); } - Err(io::Error::new( - io::ErrorKind::Other, - format!("trigger: Invalid interrupt index {index}"), - )) + Err(io::Error::other(format!( + "trigger: Invalid interrupt index {index}" + ))) } fn notifier(&self, index: InterruptIndex) -> Option { @@ -203,10 +191,9 @@ impl InterruptSourceGroup for MsiInterruptGroup { return Ok(()); } - Err(io::Error::new( - io::ErrorKind::Other, - format!("update: Invalid interrupt index {index}"), - )) + Err(io::Error::other(format!( + "update: Invalid interrupt index {index}" + ))) } fn set_gsi(&self) -> Result<()> { @@ -232,12 +219,7 @@ impl InterruptSourceGroup for LegacyUserspaceInterruptGroup { .lock() .unwrap() .service_irq(self.irq as usize) - .map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("failed to inject IRQ #{}: {:?}", self.irq, e), - ) - }) + .map_err(|e| io::Error::other(format!("failed to inject IRQ #{}: {:?}", self.irq, e))) } fn update(