diff --git a/arch/src/riscv64/fdt.rs b/arch/src/riscv64/fdt.rs index 1a7e2e5f4..580aaa7d3 100644 --- a/arch/src/riscv64/fdt.rs +++ b/arch/src/riscv64/fdt.rs @@ -448,10 +448,7 @@ fn print_node(node: fdt_parser::node::FdtNode<'_, '_>, n_spaces: usize) { // - At first, try to convert it to CStr and print, // - If failed, print it as u32 array. let value_result = match CStr::from_bytes_with_nul(value) { - Ok(value_cstr) => match value_cstr.to_str() { - Ok(value_str) => Some(value_str), - Err(_e) => None, - }, + Ok(value_cstr) => value_cstr.to_str().ok(), Err(_e) => None, }; diff --git a/block/src/lib.rs b/block/src/lib.rs index aed9c0ab3..4e10771c4 100644 --- a/block/src/lib.rs +++ b/block/src/lib.rs @@ -431,7 +431,7 @@ impl Request { // In case it's not properly aligned, an intermediate buffer is // created with the correct alignment, and a copy from/to the // origin buffer is performed, depending on the type of operation. - let iov_base = if (origin_ptr.as_ptr() as u64) % SECTOR_SIZE != 0 { + let iov_base = if !(origin_ptr.as_ptr() as u64).is_multiple_of(SECTOR_SIZE) { let layout = Layout::from_size_align(data_len, SECTOR_SIZE as usize).unwrap(); // SAFETY: layout has non-zero size let aligned_ptr = unsafe { alloc_zeroed(layout) }; diff --git a/block/src/qcow/mod.rs b/block/src/qcow/mod.rs index 7733ff0ce..4d84918e9 100644 --- a/block/src/qcow/mod.rs +++ b/block/src/qcow/mod.rs @@ -1705,12 +1705,12 @@ fn offset_is_cluster_boundary(offset: u64, cluster_bits: u32) -> Result<()> { // Ceiling of the division of `dividend`/`divisor`. fn div_round_up_u64(dividend: u64, divisor: u64) -> u64 { - dividend / divisor + u64::from(dividend % divisor != 0) + dividend / divisor + u64::from(!dividend.is_multiple_of(divisor)) } // Ceiling of the division of `dividend`/`divisor`. fn div_round_up_u32(dividend: u32, divisor: u32) -> u32 { - dividend / divisor + u32::from(dividend % divisor != 0) + dividend / divisor + u32::from(!dividend.is_multiple_of(divisor)) } fn convert_copy(reader: &mut R, writer: &mut W, offset: u64, size: u64) -> Result<()> diff --git a/block/src/qcow/raw_file.rs b/block/src/qcow/raw_file.rs index cb9637601..67bc99fca 100644 --- a/block/src/qcow/raw_file.rs +++ b/block/src/qcow/raw_file.rs @@ -89,9 +89,9 @@ impl RawFile { let align64: u64 = self.alignment.try_into().unwrap(); - (self.position % align64 == 0) - && ((buf.as_ptr() as usize) % self.alignment == 0) - && (buf.len() % self.alignment == 0) + self.position.is_multiple_of(align64) + && (buf.as_ptr() as usize).is_multiple_of(self.alignment) + && buf.len().is_multiple_of(self.alignment) } pub fn set_len(&self, size: u64) -> std::io::Result<()> { diff --git a/hypervisor/src/kvm/aarch64/gic/dist_regs.rs b/hypervisor/src/kvm/aarch64/gic/dist_regs.rs index 9135ad003..af2d6022c 100644 --- a/hypervisor/src/kvm/aarch64/gic/dist_regs.rs +++ b/hypervisor/src/kvm/aarch64/gic/dist_regs.rs @@ -156,7 +156,7 @@ fn compute_reg_len(gic: &DeviceFd, reg: &DistReg, base: u32) -> Result { // that the model has. It is also the type of register where // a register relates to multiple interrupts. end = base + (reg.bpi as u32 * (num_irq - LAYOUT_IRQ_BASE) / 8); - if reg.bpi as u32 * (num_irq - LAYOUT_IRQ_BASE) % 8 > 0 { + if !(reg.bpi as u32 * (num_irq - LAYOUT_IRQ_BASE)).is_multiple_of(8) { end += REG_SIZE as u32; } } diff --git a/virtio-devices/src/balloon.rs b/virtio-devices/src/balloon.rs index 890dfbd9f..2a34b6688 100644 --- a/virtio-devices/src/balloon.rs +++ b/virtio-devices/src/balloon.rs @@ -265,7 +265,7 @@ impl BalloonEpollHandler { error!("The head contains the request type is not right"); return Err(Error::UnexpectedWriteOnlyDescriptor); } - if desc.len() as usize % data_chunk_size != 0 { + if !(desc.len() as usize).is_multiple_of(data_chunk_size) { error!("the request size {} is not right", desc.len()); return Err(Error::InvalidRequest); } diff --git a/virtio-devices/src/mem.rs b/virtio-devices/src/mem.rs index 5739a12fa..8d5830ac3 100644 --- a/virtio-devices/src/mem.rs +++ b/virtio-devices/src/mem.rs @@ -193,35 +193,35 @@ unsafe impl ByteValued for VirtioMemConfig {} impl VirtioMemConfig { fn validate(&self) -> result::Result<(), Error> { - if self.addr % self.block_size != 0 { + if !self.addr.is_multiple_of(self.block_size) { return Err(Error::ValidateError(anyhow!( "addr 0x{:x} is not aligned on block_size 0x{:x}", self.addr, self.block_size ))); } - if self.region_size % self.block_size != 0 { + if !self.region_size.is_multiple_of(self.block_size) { return Err(Error::ValidateError(anyhow!( "region_size 0x{:x} is not aligned on block_size 0x{:x}", self.region_size, self.block_size ))); } - if self.usable_region_size % self.block_size != 0 { + if !self.usable_region_size.is_multiple_of(self.block_size) { return Err(Error::ValidateError(anyhow!( "usable_region_size 0x{:x} is not aligned on block_size 0x{:x}", self.usable_region_size, self.block_size ))); } - if self.plugged_size % self.block_size != 0 { + if !self.plugged_size.is_multiple_of(self.block_size) { return Err(Error::ValidateError(anyhow!( "plugged_size 0x{:x} is not aligned on block_size 0x{:x}", self.plugged_size, self.block_size ))); } - if self.requested_size % self.block_size != 0 { + if !self.requested_size.is_multiple_of(self.block_size) { return Err(Error::ValidateError(anyhow!( "requested_size 0x{:x} is not aligned on block_size 0x{:x}", self.requested_size, @@ -244,7 +244,7 @@ impl VirtioMemConfig { size, self.region_size ))); - } else if size % self.block_size != 0 { + } else if !size.is_multiple_of(self.block_size) { return Err(Error::ResizeError(anyhow!( "new size 0x{:x} is not aligned on block_size 0x{:x}", size, @@ -267,7 +267,7 @@ impl VirtioMemConfig { // Start address must be aligned on block_size, the size must be // greater than 0, and all blocks covered by the request must be // in the usable region. - if addr % self.block_size != 0 + if !addr.is_multiple_of(self.block_size) || size == 0 || (addr < self.addr || addr + size > self.addr + self.usable_region_size) { diff --git a/virtio-devices/src/net.rs b/virtio-devices/src/net.rs index 7d2a4a459..950cedb51 100644 --- a/virtio-devices/src/net.rs +++ b/virtio-devices/src/net.rs @@ -706,7 +706,7 @@ impl VirtioDevice for Net { let num_queues = queues.len(); let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into()); - if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && num_queues % 2 != 0 { + if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && !num_queues.is_multiple_of(2) { let ctrl_queue_index = num_queues - 1; let (_, mut ctrl_queue, ctrl_queue_evt) = queues.remove(ctrl_queue_index); diff --git a/virtio-devices/src/vhost_user/net.rs b/virtio-devices/src/vhost_user/net.rs index 930f55741..c52d5ca38 100644 --- a/virtio-devices/src/vhost_user/net.rs +++ b/virtio-devices/src/vhost_user/net.rs @@ -298,7 +298,7 @@ impl VirtioDevice for Net { let num_queues = queues.len(); let event_idx = self.common.feature_acked(VIRTIO_RING_F_EVENT_IDX.into()); - if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && num_queues % 2 != 0 { + if self.common.feature_acked(VIRTIO_NET_F_CTRL_VQ.into()) && !num_queues.is_multiple_of(2) { let ctrl_queue_index = num_queues - 1; let (_, mut ctrl_queue, ctrl_queue_evt) = queues.remove(ctrl_queue_index); diff --git a/vm-allocator/src/address.rs b/vm-allocator/src/address.rs index a6e11dc63..9a72afdf9 100644 --- a/vm-allocator/src/address.rs +++ b/vm-allocator/src/address.rs @@ -68,7 +68,7 @@ impl AddressAllocator { } fn align_address(&self, address: GuestAddress, alignment: GuestUsize) -> GuestAddress { - let align_adjust = if address.raw_value() % alignment != 0 { + let align_adjust = if !address.raw_value().is_multiple_of(alignment) { alignment - (address.raw_value() % alignment) } else { 0 diff --git a/vm-migration/src/protocol.rs b/vm-migration/src/protocol.rs index 274baf039..094a8c1a3 100644 --- a/vm-migration/src/protocol.rs +++ b/vm-migration/src/protocol.rs @@ -264,7 +264,7 @@ impl MemoryRangeTable { } pub fn read_from(fd: &mut dyn Read, length: u64) -> Result { - assert!(length as usize % std::mem::size_of::() == 0); + assert!((length as usize).is_multiple_of(size_of::())); let mut data: Vec = Vec::new(); data.resize_with( diff --git a/vmm/src/igvm/igvm_loader.rs b/vmm/src/igvm/igvm_loader.rs index 75eeb3358..805bb94b4 100644 --- a/vmm/src/igvm/igvm_loader.rs +++ b/vmm/src/igvm/igvm_loader.rs @@ -66,8 +66,8 @@ enum ParameterAreaState { #[cfg(feature = "sev_snp")] fn igvm_memmap_from_ram_range(ram_range: (u64, u64)) -> IGVM_VHS_MEMORY_MAP_ENTRY { - assert!(ram_range.0 % HV_PAGE_SIZE == 0); - assert!((ram_range.1 - ram_range.0) % HV_PAGE_SIZE == 0); + assert!(ram_range.0.is_multiple_of(HV_PAGE_SIZE)); + assert!((ram_range.1 - ram_range.0).is_multiple_of(HV_PAGE_SIZE)); IGVM_VHS_MEMORY_MAP_ENTRY { starting_gpa_page_number: ram_range.0 / HV_PAGE_SIZE, @@ -179,7 +179,7 @@ pub fn load_igvm( data_type, data, } => { - debug_assert!(data.len() as u64 % HV_PAGE_SIZE == 0); + debug_assert!((data.len() as u64).is_multiple_of(HV_PAGE_SIZE)); // TODO: only 4k or empty page data supported right now assert!(data.len() as u64 == HV_PAGE_SIZE || data.is_empty()); diff --git a/vmm/src/memory_manager.rs b/vmm/src/memory_manager.rs index a5ab29718..2aa8e5104 100644 --- a/vmm/src/memory_manager.rs +++ b/vmm/src/memory_manager.rs @@ -1700,7 +1700,7 @@ impl MemoryManager { } // "Inserted" DIMM must have a size that is a multiple of 128MiB - if size % (128 << 20) != 0 { + if !size.is_multiple_of(128 << 20) { return Err(Error::InvalidSize); }