From b22fd3fcd77db92184f2669ede074cdf81328f74 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 Signed-off-by: StemCll lydjotj6f@mozmail.com --- src/api_server/src/lib.rs | 1 + src/arch/src/aarch64/fdt.rs | 10 +++++----- src/arch/src/aarch64/regs.rs | 4 ++-- src/arch/src/lib.rs | 1 + src/cpuid/src/bit_helper.rs | 2 +- src/cpuid/src/lib.rs | 1 + src/cpuid/src/transformer/mod.rs | 2 +- src/devices/src/lib.rs | 1 + 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/dumbo/src/lib.rs | 1 + src/firecracker/src/main.rs | 1 + src/io_uring/src/lib.rs | 4 +++- src/jailer/src/main.rs | 1 + src/logger/src/lib.rs | 1 + src/mmds/src/lib.rs | 1 + src/mmds/src/token.rs | 2 +- src/rate_limiter/src/lib.rs | 1 + src/rebase-snap/src/main.rs | 1 + src/seccompiler/src/lib.rs | 1 + src/snapshot/src/lib.rs | 3 ++- src/utils/src/lib.rs | 1 + src/vmm/src/lib.rs | 2 +- src/vmm/src/vmm_config/vsock.rs | 2 +- src/vmm/src/vstate/vcpu/x86_64.rs | 4 ++-- tools/bindgen.sh | 3 ++- 31 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/api_server/src/lib.rs b/src/api_server/src/lib.rs index 8dc929ed09fb..87c9e5868ebb 100644 --- a/src/api_server/src/lib.rs +++ b/src/api_server/src/lib.rs @@ -3,6 +3,7 @@ #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! Implements the interface for intercepting API requests, forwarding them to the VMM //! and responding to the user. //! It is constructed on top of an HTTP Server that uses Unix Domain Sockets and `EPOLL` to diff --git a/src/arch/src/aarch64/fdt.rs b/src/arch/src/aarch64/fdt.rs index 6291f1918c63..e24a69bc6539 100644 --- a/src/arch/src/aarch64/fdt.rs +++ b/src/arch/src/aarch64/fdt.rs @@ -146,10 +146,10 @@ fn create_cpu_nodes(fdt: &mut FdtWriter, vcpu_mpidr: &[u64]) -> Result<()> { fdt.property_u32(cache.type_.of_cache_size(), size as u32)?; } if let Some(line_size) = cache.line_size { - fdt.property_u32(cache.type_.of_cache_line_size(), line_size as u32)?; + fdt.property_u32(cache.type_.of_cache_line_size(), u32::from(line_size))?; } if let Some(number_of_sets) = cache.number_of_sets { - fdt.property_u32(cache.type_.of_cache_sets(), number_of_sets as u32)?; + fdt.property_u32(cache.type_.of_cache_sets(), u32::from(number_of_sets))?; } } @@ -188,15 +188,15 @@ fn create_cpu_nodes(fdt: &mut FdtWriter, vcpu_mpidr: &[u64]) -> Result<()> { ))?); fdt.property_u32("phandle", cache_phandle)?; fdt.property_string("compatible", "cache")?; - fdt.property_u32("cache-level", cache.level as u32)?; + fdt.property_u32("cache-level", u32::from(cache.level))?; if let Some(size) = cache.size_ { fdt.property_u32(cache.type_.of_cache_size(), size as u32)?; } if let Some(line_size) = cache.line_size { - fdt.property_u32(cache.type_.of_cache_line_size(), line_size as u32)?; + fdt.property_u32(cache.type_.of_cache_line_size(), u32::from(line_size))?; } if let Some(number_of_sets) = cache.number_of_sets { - fdt.property_u32(cache.type_.of_cache_sets(), number_of_sets as u32)?; + fdt.property_u32(cache.type_.of_cache_sets(), u32::from(number_of_sets))?; } if let Some(cache_type) = cache.type_.of_cache_type() { fdt.property_null(cache_type)?; diff --git a/src/arch/src/aarch64/regs.rs b/src/arch/src/aarch64/regs.rs index 6afe32af747c..9aa06be0d278 100644 --- a/src/arch/src/aarch64/regs.rs +++ b/src/arch/src/aarch64/regs.rs @@ -237,7 +237,7 @@ pub fn setup_boot_regs( /// /// * `regid` - The index of the register we are checking. pub fn is_system_register(regid: u64) -> bool { - if (regid & KVM_REG_ARM_COPROC_MASK as u64) == KVM_REG_ARM_CORE as u64 { + if (regid & u64::from(KVM_REG_ARM_COPROC_MASK)) == u64::from(KVM_REG_ARM_CORE) { return false; } @@ -505,7 +505,7 @@ mod tests { assert!(!is_system_register(regid)); let regid = KVM_REG_ARM64 as u64 | KVM_REG_SIZE_U64 as u64 - | kvm_bindings::KVM_REG_ARM64_SYSREG as u64; + | u64::from(kvm_bindings::KVM_REG_ARM64_SYSREG); assert!(is_system_register(regid)); } diff --git a/src/arch/src/lib.rs b/src/arch/src/lib.rs index 0fe2c6c371ea..828ccd6997c6 100644 --- a/src/arch/src/lib.rs +++ b/src/arch/src/lib.rs @@ -3,6 +3,7 @@ #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! Implements platform specific functionality. //! Supported platforms: x86_64 and aarch64. use std::{fmt, result}; 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/lib.rs b/src/cpuid/src/lib.rs index b7af7d404513..78b978de5a91 100644 --- a/src/cpuid/src/lib.rs +++ b/src/cpuid/src/lib.rs @@ -7,6 +7,7 @@ #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! Utility for configuring the CPUID (CPU identification) for the guest microVM. #![cfg(target_arch = "x86_64")] 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/lib.rs b/src/devices/src/lib.rs index 72db8b292a03..408f4261dd20 100644 --- a/src/devices/src/lib.rs +++ b/src/devices/src/lib.rs @@ -6,6 +6,7 @@ // found in the THIRD-PARTY file. #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! Emulates virtual and hardware devices. use std::io; 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/dumbo/src/lib.rs b/src/dumbo/src/lib.rs index e737990abd8e..8796efa48d12 100644 --- a/src/dumbo/src/lib.rs +++ b/src/dumbo/src/lib.rs @@ -3,6 +3,7 @@ #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! Provides helper logic for parsing and writing protocol data units, and minimalist //! implementations of a TCP listener, a TCP connection, and an HTTP/1.1 server. pub mod pdu; diff --git a/src/firecracker/src/main.rs b/src/firecracker/src/main.rs index 8d06d52a6b3a..dcce45417f21 100644 --- a/src/firecracker/src/main.rs +++ b/src/firecracker/src/main.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] mod api_server_adapter; mod metrics; diff --git a/src/io_uring/src/lib.rs b/src/io_uring/src/lib.rs index 75b53abd052f..bbc73829353c 100644 --- a/src/io_uring/src/lib.rs +++ b/src/io_uring/src/lib.rs @@ -3,6 +3,7 @@ #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! High-level interface over Linux io_uring. //! //! Aims to provide an easy-to-use interface, while making some Firecracker-specific simplifying @@ -365,7 +366,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/jailer/src/main.rs b/src/jailer/src/main.rs index dfeb6bf631e2..eacb34cbe451 100644 --- a/src/jailer/src/main.rs +++ b/src/jailer/src/main.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] mod cgroup; mod chroot; diff --git a/src/logger/src/lib.rs b/src/logger/src/lib.rs index 038ad7ed8952..2044a86dac93 100644 --- a/src/logger/src/lib.rs +++ b/src/logger/src/lib.rs @@ -3,6 +3,7 @@ #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! Crate that implements Firecracker specific functionality as far as logging and metrics //! collecting. diff --git a/src/mmds/src/lib.rs b/src/mmds/src/lib.rs index 48efaeedaa84..c97ee4e87c43 100644 --- a/src/mmds/src/lib.rs +++ b/src/mmds/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] pub mod data_store; pub mod ns; 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/rate_limiter/src/lib.rs b/src/rate_limiter/src/lib.rs index d0930b900982..a7d6cc0ee93a 100644 --- a/src/rate_limiter/src/lib.rs +++ b/src/rate_limiter/src/lib.rs @@ -3,6 +3,7 @@ #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! # Rate Limiter //! //! Provides a rate limiter written in Rust useful for IO operations that need to diff --git a/src/rebase-snap/src/main.rs b/src/rebase-snap/src/main.rs index 61163a555ab3..0d96b4b2a476 100644 --- a/src/rebase-snap/src/main.rs +++ b/src/rebase-snap/src/main.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] use std::fs::{File, OpenOptions}; use std::io::{Seek, SeekFrom}; diff --git a/src/seccompiler/src/lib.rs b/src/seccompiler/src/lib.rs index 40f4a6da33dc..581f52b15181 100644 --- a/src/seccompiler/src/lib.rs +++ b/src/seccompiler/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! The library crate that defines common helper functions that are generally used in //! conjunction with seccompiler-bin. diff --git a/src/snapshot/src/lib.rs b/src/snapshot/src/lib.rs index 6d089728684e..d58600ffd53c 100644 --- a/src/snapshot/src/lib.rs +++ b/src/snapshot/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #![deny(missing_docs)] #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] //! Provides version tolerant serialization and deserialization facilities and //! implements a persistent storage format for Firecracker state snapshots. @@ -94,7 +95,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/utils/src/lib.rs b/src/utils/src/lib.rs index ccdadd512544..01f54c5080e2 100644 --- a/src/utils/src/lib.rs +++ b/src/utils/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #![warn(clippy::ptr_as_ptr)] +#![warn(clippy::cast_lossless)] // We use `utils` as a wrapper over `vmm_sys_util` to control the latter // dependency easier (i.e. update only in one place `vmm_sys_util` version). 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/tools/bindgen.sh b/tools/bindgen.sh index 37c45103723b..1a5f4e979992 100755 --- a/tools/bindgen.sh +++ b/tools/bindgen.sh @@ -31,7 +31,8 @@ function fc-bindgen { non_upper_case_globals, dead_code, non_snake_case, - clippy::ptr_as_ptr + clippy::ptr_as_ptr, + clippy::cast_lossless )] EOF