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 <jinankjain@microsoft.com>
This commit is contained in:
Jinank Jain 2025-04-02 07:44:40 +00:00
parent 3698b8e74c
commit ea4693a091
16 changed files with 114 additions and 208 deletions

View file

@ -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",
))
}

View file

@ -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();

View file

@ -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<MacAddr, io::Error> {
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))
}

View file

@ -310,10 +310,9 @@ impl MmioRegionRange for Vec<MmioRegion> {
}
}
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<M: GuestAddressSpace + Sync + Send> ExternalDmaMapping for VfioDmaMapping<M
match mem.get_host_address(guest_addr) {
Ok(t) => 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<M: GuestAddressSpace + Sync + Send> ExternalDmaMapping for VfioDmaMapping<M
} else if self.mmio_regions.lock().unwrap().check_range(gpa, size) {
self.mmio_regions.lock().unwrap().find_user_address(gpa)?
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("failed to locate guest address 0x{gpa:x} in guest memory"),
));
return Err(io::Error::other(format!(
"failed to locate guest address 0x{gpa:x} in guest memory"
)));
};
self.container
.vfio_dma_map(iova, size, user_addr)
.map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!(
"failed to map memory for VFIO container, \
io::Error::other(format!(
"failed to map memory for VFIO container, \
iova 0x{iova:x}, gpa 0x{gpa:x}, size 0x{size:x}: {e:?}"
),
)
))
})
}
fn unmap(&self, iova: u64, size: u64) -> 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:?}"
),
)
))
})
}
}

View file

@ -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<M: GuestAddressSpace + Sync + Send> 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<M: GuestAddressSpace + Sync + Send> 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}")))
}
}

View file

@ -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}"
),

View file

@ -83,7 +83,7 @@ impl error::Error for Error {}
impl convert::From<Error> for io::Error {
fn from(e: Error) -> Self {
io::Error::new(io::ErrorKind::Other, e)
io::Error::other(e)
}
}

View file

@ -69,7 +69,7 @@ impl std::error::Error for Error {}
impl std::convert::From<Error> for std::io::Error {
fn from(e: Error) -> Self {
std::io::Error::new(io::ErrorKind::Other, e)
std::io::Error::other(e)
}
}

View file

@ -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 {}; \

View file

@ -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<u64, std::io::Error> {
@ -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}"
)))
}
}

View file

@ -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)

View file

@ -541,13 +541,10 @@ impl<M: GuestAddressSpace + Sync + Send> ExternalDmaMapping for VdpaDmaMapping<M
let user_addr = if mem.check_range(guest_addr, size as usize) {
mem.get_host_address(guest_addr).unwrap() as *const u8
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"failed to convert guest address 0x{gpa:x} into \
return Err(io::Error::other(format!(
"failed to convert guest address 0x{gpa:x} into \
host user virtual address"
),
));
)));
};
debug!(
@ -559,13 +556,10 @@ impl<M: GuestAddressSpace + Sync + Send> ExternalDmaMapping for VdpaDmaMapping<M
.unwrap()
.dma_map(iova, size, user_addr, false)
.map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!(
"failed to map memory for vDPA device, \
io::Error::other(format!(
"failed to map memory for vDPA device, \
iova 0x{iova:x}, gpa 0x{gpa:x}, size 0x{size:x}: {e:?}"
),
)
))
})
}
@ -576,13 +570,10 @@ impl<M: GuestAddressSpace + Sync + Send> ExternalDmaMapping for VdpaDmaMapping<M
.unwrap()
.dma_unmap(iova, size)
.map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!(
"failed to unmap memory for vDPA device, \
io::Error::other(format!(
"failed to unmap memory for vDPA device, \
iova 0x{iova:x}, size 0x{size:x}: {e:?}"
),
)
))
})
}
}

View file

@ -215,10 +215,9 @@ impl<S: VhostUserFrontendReqHandler> VhostUserEpollHandler<S> {
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<S: VhostUserFrontendReqHandler> VhostUserEpollHandler<S> {
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(

View file

@ -73,7 +73,7 @@ impl error::Error for Error {}
impl convert::From<Error> for io::Error {
fn from(e: Error) -> Self {
io::Error::new(io::ErrorKind::Other, e)
io::Error::other(e)
}
}

View file

@ -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<u64, std::io::Error> {
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)
}
}

View file

@ -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<dyn hypervisor::Vm>) -> 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<dyn hypervisor::Vm>) -> 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<EventFd> {
@ -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(