Skip to content

Commit

Permalink
Add clippy::cast_lossless warning
Browse files Browse the repository at this point in the history
Signed-off-by: StemCll [email protected]
  • Loading branch information
StemCll committed Nov 20, 2022
1 parent 1644b3c commit 1c077d1
Show file tree
Hide file tree
Showing 31 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/api_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![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
Expand Down
10 changes: 5 additions & 5 deletions src/arch/src/aarch64/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
}
}

Expand Down Expand Up @@ -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)?;
Expand Down
4 changes: 2 additions & 2 deletions src/arch/src/aarch64/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,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;
}

Expand Down Expand Up @@ -508,7 +508,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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/arch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![warn(clippy::cast_lossless)]
//! Implements platform specific functionality.
//! Supported platforms: x86_64 and aarch64.
use std::{fmt, result};
Expand Down
2 changes: 1 addition & 1 deletion src/cpuid/src/bit_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/cpuid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![warn(clippy::cast_lossless)]
//! Utility for configuring the CPUID (CPU identification) for the guest microVM.
#![cfg(target_arch = "x86_64")]
Expand Down
2 changes: 1 addition & 1 deletion src/cpuid/src/transformer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/devices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![warn(clippy::cast_lossless)]
//! Emulates virtual and hardware devices.
use std::io;

Expand Down
10 changes: 5 additions & 5 deletions src/devices/src/virtio/balloon/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<BalloonStat>(addr)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/devices/src/virtio/balloon/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/devices/src/virtio/block/io/async_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<T> AsyncFileEngine<T> {

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.
Expand Down
4 changes: 2 additions & 2 deletions src/devices/src/virtio/block/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,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));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/devices/src/virtio/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[..]);
Expand Down
4 changes: 2 additions & 2 deletions src/devices/src/virtio/net/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,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_pseudo_rng_u32() as u64 % 10;
addr += u64::from(utils::rand::xor_pseudo_rng_u32()) % 10;
}

// Mark the chain as available.
Expand Down
2 changes: 1 addition & 1 deletion src/devices/src/virtio/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,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);
}
}
2 changes: 1 addition & 1 deletion src/dumbo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![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;
Expand Down
1 change: 1 addition & 0 deletions src/firecracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![warn(clippy::cast_lossless)]

mod api_server_adapter;
mod metrics;
Expand Down
4 changes: 2 additions & 2 deletions src/io_uring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![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
Expand Down Expand Up @@ -368,7 +368,7 @@ impl IoUring {
let supported_opcodes: HashSet<u8> = probes
.as_slice()
.iter()
.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();

Expand Down
1 change: 1 addition & 0 deletions src/jailer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![warn(clippy::cast_lossless)]

mod cgroup;
mod chroot;
Expand Down
2 changes: 1 addition & 1 deletion src/logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![warn(clippy::cast_lossless)]
//! Crate that implements Firecracker specific functionality as far as logging and metrics
//! collecting.
Expand Down
1 change: 1 addition & 0 deletions src/mmds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![warn(clippy::cast_lossless)]

pub mod data_store;
pub mod ns;
Expand Down
2 changes: 1 addition & 1 deletion src/mmds/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rate_limiter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]

#![warn(clippy::cast_lossless)]
//! # Rate Limiter
//!
//! Provides a rate limiter written in Rust useful for IO operations that need to
Expand Down
1 change: 1 addition & 0 deletions src/rebase-snap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![warn(clippy::cast_lossless)]

use std::fs::{File, OpenOptions};
use std::io::{Seek, SeekFrom};
Expand Down
1 change: 1 addition & 0 deletions src/seccompiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![warn(clippy::cast_lossless)]

//! The library crate that defines common helper functions that are generally used in
//! conjunction with seccompiler-bin.
Expand Down
3 changes: 2 additions & 1 deletion src/snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![deny(missing_docs)]
#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![warn(clippy::cast_lossless)]

//! Provides version tolerant serialization and deserialization facilities and
//! implements a persistent storage format for Firecracker state snapshots.
Expand Down Expand Up @@ -95,7 +96,7 @@ fn get_format_version(magic_id: u64) -> Result<u16, Error> {
}

fn build_magic_id(format_version: u16) -> u64 {
BASE_MAGIC_ID | format_version as u64
BASE_MAGIC_ID | u64::from(format_version)
}

impl Snapshot {
Expand Down
1 change: 1 addition & 0 deletions src/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![warn(clippy::ptr_as_ptr)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![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).
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/vmm_config/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/vmm/src/vstate/vcpu/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tools/bindgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function fc-bindgen {
non_snake_case,
clippy::ptr_as_ptr,
clippy::undocumented_unsafe_blocks
clippy::cast_lossless
)]
EOF
Expand Down

0 comments on commit 1c077d1

Please sign in to comment.