From 86300b8ed6411698656d7eb0db3c57d12cbdd98f Mon Sep 17 00:00:00 2001 From: StemCll Date: Thu, 27 Oct 2022 21:49:16 +0200 Subject: [PATCH] Add clippy::cast_lossless warning Related issue - #3196 Signed-off-by: StemCll lydjotj6f@mozmail.com --- src/cpuid/src/bit_helper.rs | 2 +- src/cpuid/src/transformer/mod.rs | 2 +- src/devices/src/virtio/balloon/device.rs | 10 +++++----- src/devices/src/virtio/balloon/persist.rs | 4 ++-- src/devices/src/virtio/block/io/async_io.rs | 2 +- src/devices/src/virtio/block/io/mod.rs | 4 ++-- src/devices/src/virtio/mmio.rs | 4 ++-- src/devices/src/virtio/net/test_utils.rs | 4 ++-- src/devices/src/virtio/test_utils.rs | 2 +- src/io_uring/src/lib.rs | 3 ++- src/mmds/src/token.rs | 2 +- src/snapshot/src/lib.rs | 2 +- src/vmm/src/lib.rs | 2 +- src/vmm/src/vmm_config/vsock.rs | 2 +- src/vmm/src/vstate/vcpu/x86_64.rs | 4 ++-- tests/integration_tests/build/test_clippy.py | 16 ++++++++-------- 16 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/cpuid/src/bit_helper.rs b/src/cpuid/src/bit_helper.rs index 0b8b8d43930f..36385454329d 100644 --- a/src/cpuid/src/bit_helper.rs +++ b/src/cpuid/src/bit_helper.rs @@ -148,7 +148,7 @@ impl BitHelper for u32 { assert!(pos <= MAX_U32_BIT_INDEX, "Invalid pos"); *self &= !(1 << pos); - *self |= (val as u32) << pos; + *self |= (u32::from(val)) << pos; self } diff --git a/src/cpuid/src/transformer/mod.rs b/src/cpuid/src/transformer/mod.rs index 66f964f86747..aea30d14719c 100644 --- a/src/cpuid/src/transformer/mod.rs +++ b/src/cpuid/src/transformer/mod.rs @@ -36,7 +36,7 @@ impl VmSpec { cpu_vendor_id, cpu_index, cpu_count, - cpu_bits: (cpu_count > 1 && smt) as u8, + cpu_bits: u8::from(cpu_count > 1 && smt), brand_string: BrandString::from_vendor_id(&cpu_vendor_id), }) } diff --git a/src/devices/src/virtio/balloon/device.rs b/src/devices/src/virtio/balloon/device.rs index 0c29869b9932..ee7d31398928 100644 --- a/src/devices/src/virtio/balloon/device.rs +++ b/src/devices/src/virtio/balloon/device.rs @@ -303,7 +303,7 @@ impl Balloon { // Remove the page ranges. for (page_frame_number, range_len) in page_ranges { let guest_addr = - GuestAddress((page_frame_number as u64) << VIRTIO_BALLOON_PFN_SHIFT); + GuestAddress(u64::from(page_frame_number) << VIRTIO_BALLOON_PFN_SHIFT); if let Err(err) = remove_range( mem, @@ -365,7 +365,7 @@ impl Balloon { // so we ignore the rest of it. let addr = head .addr - .checked_add(index as u64) + .checked_add(u64::from(index)) .ok_or(BalloonError::MalformedDescriptor)?; let stat = mem .read_obj::(addr) @@ -445,8 +445,8 @@ impl Balloon { pub fn update_timer_state(&mut self) { let timer_state = TimerState::Periodic { - current: Duration::from_secs(self.stats_polling_interval_s as u64), - interval: Duration::from_secs(self.stats_polling_interval_s as u64), + current: Duration::from_secs(u64::from(self.stats_polling_interval_s)), + interval: Duration::from_secs(u64::from(self.stats_polling_interval_s)), }; self.stats_timer .set_state(timer_state, SetTimeFlags::Default); @@ -687,7 +687,7 @@ pub(crate) mod tests { let features: u64 = (1u64 << VIRTIO_F_VERSION_1) | ((if *deflate_on_oom { 1 } else { 0 }) << VIRTIO_BALLOON_F_DEFLATE_ON_OOM) - | ((*stats_interval as u64) << VIRTIO_BALLOON_F_STATS_VQ); + | ((u64::from(*stats_interval)) << VIRTIO_BALLOON_F_STATS_VQ); assert_eq!(balloon.avail_features_by_page(0), features as u32); assert_eq!(balloon.avail_features_by_page(1), (features >> 32) as u32); diff --git a/src/devices/src/virtio/balloon/persist.rs b/src/devices/src/virtio/balloon/persist.rs index 8a9d4179bdbf..09eee7c90160 100644 --- a/src/devices/src/virtio/balloon/persist.rs +++ b/src/devices/src/virtio/balloon/persist.rs @@ -145,8 +145,8 @@ impl Persist<'_> for Balloon { // Restart timer if needed. let timer_state = TimerState::Periodic { - current: Duration::from_secs(state.stats_polling_interval_s as u64), - interval: Duration::from_secs(state.stats_polling_interval_s as u64), + current: Duration::from_secs(u64::from(state.stats_polling_interval_s)), + interval: Duration::from_secs(u64::from(state.stats_polling_interval_s)), }; balloon .stats_timer diff --git a/src/devices/src/virtio/block/io/async_io.rs b/src/devices/src/virtio/block/io/async_io.rs index 13b22f2c2524..fb8c0c397ce7 100644 --- a/src/devices/src/virtio/block/io/async_io.rs +++ b/src/devices/src/virtio/block/io/async_io.rs @@ -67,7 +67,7 @@ impl AsyncFileEngine { let completion_evt = EventFd::new(libc::EFD_NONBLOCK).map_err(Error::EventFd)?; let ring = IoUring::new( - IO_URING_NUM_ENTRIES as u32, + u32::from(IO_URING_NUM_ENTRIES), vec![&file], vec![ // Make sure we only allow operations on pre-registered fds. diff --git a/src/devices/src/virtio/block/io/mod.rs b/src/devices/src/virtio/block/io/mod.rs index 0483e03bc945..b33f8c958d47 100644 --- a/src/devices/src/virtio/block/io/mod.rs +++ b/src/devices/src/virtio/block/io/mod.rs @@ -239,14 +239,14 @@ pub mod tests { fn check_dirty_mem(mem: &GuestMemoryMmap, addr: GuestAddress, len: u32) { let bitmap = mem.find_region(addr).unwrap().bitmap().as_ref().unwrap(); - for offset in addr.0..addr.0 + len as u64 { + for offset in addr.0..addr.0 + u64::from(len) { assert!(bitmap.dirty_at(offset as usize)); } } fn check_clean_mem(mem: &GuestMemoryMmap, addr: GuestAddress, len: u32) { let bitmap = mem.find_region(addr).unwrap().bitmap().as_ref().unwrap(); - for offset in addr.0..addr.0 + len as u64 { + for offset in addr.0..addr.0 + u64::from(len) { assert!(!bitmap.dirty_at(offset as usize)); } } diff --git a/src/devices/src/virtio/mmio.rs b/src/devices/src/virtio/mmio.rs index 4215d21ebb94..f4eef2b7e020 100644 --- a/src/devices/src/virtio/mmio.rs +++ b/src/devices/src/virtio/mmio.rs @@ -235,7 +235,7 @@ impl BusDevice for MmioTransport { features } 0x34 => self.with_queue(0, |q| u32::from(q.get_max_size())), - 0x44 => self.with_queue(0, |q| q.ready as u32), + 0x44 => self.with_queue(0, |q| u32::from(q.ready)), 0x60 => self.interrupt_status.load(Ordering::SeqCst) as u32, 0x70 => self.device_status, 0xfc => self.config_generation, @@ -513,7 +513,7 @@ pub(crate) mod tests { assert_eq!(read_le_u32(&buf[..]), 16); d.read(0x44, &mut buf[..]); - assert_eq!(read_le_u32(&buf[..]), false as u32); + assert_eq!(read_le_u32(&buf[..]), u32::from(false)); d.interrupt_status.store(111, Ordering::SeqCst); d.read(0x60, &mut buf[..]); diff --git a/src/devices/src/virtio/net/test_utils.rs b/src/devices/src/virtio/net/test_utils.rs index 99468ba7a5e0..52417b1282dc 100644 --- a/src/devices/src/virtio/net/test_utils.rs +++ b/src/devices/src/virtio/net/test_utils.rs @@ -424,10 +424,10 @@ pub mod test { desc.next.set(next_index); } - addr += len as u64; + addr += u64::from(len); // Add small random gaps between descriptor addresses in order to make sure we // don't blindly read contiguous memory. - addr += utils::rand::xor_psuedo_rng_u32() as u64 % 10; + addr += u64::from(utils::rand::xor_psuedo_rng_u32()) % 10; } // Mark the chain as available. diff --git a/src/devices/src/virtio/test_utils.rs b/src/devices/src/virtio/test_utils.rs index 40b61fe8ff8d..ff67be95699a 100644 --- a/src/devices/src/virtio/test_utils.rs +++ b/src/devices/src/virtio/test_utils.rs @@ -324,7 +324,7 @@ impl<'a> VirtQueue<'a> { pub fn check_used_elem(&self, used_index: u16, expected_id: u16, expected_len: u32) { let used_elem = self.used.ring[used_index as usize].get(); - assert_eq!(used_elem.id, expected_id as u32); + assert_eq!(used_elem.id, u32::from(expected_id)); assert_eq!(used_elem.len, expected_len); } } diff --git a/src/io_uring/src/lib.rs b/src/io_uring/src/lib.rs index 75b53abd052f..02338d0cd128 100644 --- a/src/io_uring/src/lib.rs +++ b/src/io_uring/src/lib.rs @@ -365,7 +365,8 @@ impl IoUring { let supported_opcodes: HashSet = probes .as_slice() .iter() - .filter(|op| ((op.flags as u32) & bindings::IO_URING_OP_SUPPORTED) != 0) + //.filter(|op| ((op.flags as u32) & bindings::IO_URING_OP_SUPPORTED) != 0) + .filter(|op| ((u32::from(op.flags)) & bindings::IO_URING_OP_SUPPORTED) != 0) .map(|op| op.op) .collect(); diff --git a/src/mmds/src/token.rs b/src/mmds/src/token.rs index 0c9548dd1398..272318ccc85e 100644 --- a/src/mmds/src/token.rs +++ b/src/mmds/src/token.rs @@ -277,7 +277,7 @@ impl TokenAuthority { // to current time (also in milliseconds). This addition is safe // because ttl is verified beforehand and can never be more than // 6h (21_600_000 ms). - now_as_milliseconds.add(ttl_as_seconds as u64 * MILLISECONDS_PER_SECOND) + now_as_milliseconds.add(u64::from(ttl_as_seconds) * MILLISECONDS_PER_SECOND) } } diff --git a/src/snapshot/src/lib.rs b/src/snapshot/src/lib.rs index 6d089728684e..4d191df3a6b4 100644 --- a/src/snapshot/src/lib.rs +++ b/src/snapshot/src/lib.rs @@ -94,7 +94,7 @@ fn get_format_version(magic_id: u64) -> Result { } fn build_magic_id(format_version: u16) -> u64 { - BASE_MAGIC_ID | format_version as u64 + BASE_MAGIC_ID | u64::from(format_version) } impl Snapshot { diff --git a/src/vmm/src/lib.rs b/src/vmm/src/lib.rs index e79d808eb79e..f87b4b834707 100644 --- a/src/vmm/src/lib.rs +++ b/src/vmm/src/lib.rs @@ -693,7 +693,7 @@ impl Vmm { ) -> std::result::Result<(), BalloonError> { // The balloon cannot have a target size greater than the size of // the guest memory. - if amount_mib as u64 > mem_size_mib(self.guest_memory()) { + if u64::from(amount_mib) > mem_size_mib(self.guest_memory()) { return Err(BalloonError::TooManyPagesRequested); } diff --git a/src/vmm/src/vmm_config/vsock.rs b/src/vmm/src/vmm_config/vsock.rs index bc3209781748..7f99553ac0fd 100644 --- a/src/vmm/src/vmm_config/vsock.rs +++ b/src/vmm/src/vmm_config/vsock.rs @@ -159,7 +159,7 @@ pub(crate) mod tests { vsock_config.guest_cid = new_cid; store.insert(vsock_config).unwrap(); let vsock = store.get().unwrap(); - assert_eq!(vsock.lock().unwrap().cid(), new_cid as u64); + assert_eq!(vsock.lock().unwrap().cid(), u64::from(new_cid)); } #[test] diff --git a/src/vmm/src/vstate/vcpu/x86_64.rs b/src/vmm/src/vstate/vcpu/x86_64.rs index a6b8f25af82f..a409349be0b2 100644 --- a/src/vmm/src/vstate/vcpu/x86_64.rs +++ b/src/vmm/src/vstate/vcpu/x86_64.rs @@ -438,8 +438,8 @@ impl KvmVcpu { // We accept values within a tolerance of 250 parts // per million beacuse it is common for TSC frequency // to differ due to calibration at boot time. - let diff = (self.get_tsc_khz()? as i64 - state_tsc_freq as i64).abs(); - Ok(diff > (state_tsc_freq as f64 * TSC_KHZ_TOL).round() as i64) + let diff = (i64::from(self.get_tsc_khz()?) - i64::from(state_tsc_freq)).abs(); + Ok(diff > (f64::from(state_tsc_freq) * TSC_KHZ_TOL).round() as i64) } // Scale the TSC frequency of this vCPU to the one provided as a parameter. diff --git a/tests/integration_tests/build/test_clippy.py b/tests/integration_tests/build/test_clippy.py index 3c8698055823..279c1c5800d6 100644 --- a/tests/integration_tests/build/test_clippy.py +++ b/tests/integration_tests/build/test_clippy.py @@ -9,14 +9,13 @@ SUCCESS_CODE = 0 MACHINE = platform.machine() -TARGETS = ["{}-unknown-linux-gnu".format(MACHINE), - "{}-unknown-linux-musl".format(MACHINE)] +TARGETS = [ + "{}-unknown-linux-gnu".format(MACHINE), + "{}-unknown-linux-musl".format(MACHINE), +] -@pytest.mark.parametrize( - "target", - TARGETS -) +@pytest.mark.parametrize("target", TARGETS) def test_rust_clippy(target): """ Test that clippy does not generate any errors/warnings. @@ -24,5 +23,6 @@ def test_rust_clippy(target): @type: build """ utils.run_cmd( - 'cargo clippy --target {} --all --profile test' - ' -- -D warnings'.format(target)) + "cargo clippy --target {} --all --profile test" + " -- -D warnings -D clippy::cast_lossless".format(target) + )