Skip to content

Commit 2c7cccd

Browse files
committed
Add clippy::cast_lossless warning
Signed-off-by: StemCll [email protected]
1 parent b2d36f7 commit 2c7cccd

File tree

30 files changed

+46
-37
lines changed

30 files changed

+46
-37
lines changed

src/api_server/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![deny(missing_docs)]
55
#![warn(clippy::ptr_as_ptr)]
66
#![warn(clippy::undocumented_unsafe_blocks)]
7-
7+
#![warn(clippy::cast_lossless)]
88
//! Implements the interface for intercepting API requests, forwarding them to the VMM
99
//! and responding to the user.
1010
//! It is constructed on top of an HTTP Server that uses Unix Domain Sockets and `EPOLL` to

src/arch/src/aarch64/fdt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ fn create_cpu_nodes(fdt: &mut FdtWriter, vcpu_mpidr: &[u64]) -> Result<()> {
146146
fdt.property_u32(cache.type_.of_cache_size(), size as u32)?;
147147
}
148148
if let Some(line_size) = cache.line_size {
149-
fdt.property_u32(cache.type_.of_cache_line_size(), line_size as u32)?;
149+
fdt.property_u32(cache.type_.of_cache_line_size(), u32::from(line_size))?;
150150
}
151151
if let Some(number_of_sets) = cache.number_of_sets {
152-
fdt.property_u32(cache.type_.of_cache_sets(), number_of_sets as u32)?;
152+
fdt.property_u32(cache.type_.of_cache_sets(), u32::from(number_of_sets))?;
153153
}
154154
}
155155

@@ -188,15 +188,15 @@ fn create_cpu_nodes(fdt: &mut FdtWriter, vcpu_mpidr: &[u64]) -> Result<()> {
188188
))?);
189189
fdt.property_u32("phandle", cache_phandle)?;
190190
fdt.property_string("compatible", "cache")?;
191-
fdt.property_u32("cache-level", cache.level as u32)?;
191+
fdt.property_u32("cache-level", u32::from(cache.level))?;
192192
if let Some(size) = cache.size_ {
193193
fdt.property_u32(cache.type_.of_cache_size(), size as u32)?;
194194
}
195195
if let Some(line_size) = cache.line_size {
196-
fdt.property_u32(cache.type_.of_cache_line_size(), line_size as u32)?;
196+
fdt.property_u32(cache.type_.of_cache_line_size(), u32::from(line_size))?;
197197
}
198198
if let Some(number_of_sets) = cache.number_of_sets {
199-
fdt.property_u32(cache.type_.of_cache_sets(), number_of_sets as u32)?;
199+
fdt.property_u32(cache.type_.of_cache_sets(), u32::from(number_of_sets))?;
200200
}
201201
if let Some(cache_type) = cache.type_.of_cache_type() {
202202
fdt.property_null(cache_type)?;

src/arch/src/aarch64/regs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn setup_boot_regs(
239239
///
240240
/// * `regid` - The index of the register we are checking.
241241
pub fn is_system_register(regid: u64) -> bool {
242-
if (regid & KVM_REG_ARM_COPROC_MASK as u64) == KVM_REG_ARM_CORE as u64 {
242+
if (regid & u64::from(KVM_REG_ARM_COPROC_MASK)) == u64::from(KVM_REG_ARM_CORE) {
243243
return false;
244244
}
245245

@@ -508,7 +508,7 @@ mod tests {
508508
assert!(!is_system_register(regid));
509509
let regid = KVM_REG_ARM64 as u64
510510
| KVM_REG_SIZE_U64 as u64
511-
| kvm_bindings::KVM_REG_ARM64_SYSREG as u64;
511+
| u64::from(kvm_bindings::KVM_REG_ARM64_SYSREG);
512512
assert!(is_system_register(regid));
513513
}
514514

src/arch/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![deny(missing_docs)]
55
#![warn(clippy::ptr_as_ptr)]
66
#![warn(clippy::undocumented_unsafe_blocks)]
7-
7+
#![warn(clippy::cast_lossless)]
88
//! Implements platform specific functionality.
99
//! Supported platforms: x86_64 and aarch64.
1010
use std::{fmt, result};

src/cpuid/src/bit_helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl BitHelper for u32 {
148148
assert!(pos <= MAX_U32_BIT_INDEX, "Invalid pos");
149149

150150
*self &= !(1 << pos);
151-
*self |= (val as u32) << pos;
151+
*self |= (u32::from(val)) << pos;
152152
self
153153
}
154154

src/cpuid/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![deny(missing_docs)]
99
#![warn(clippy::ptr_as_ptr)]
1010
#![warn(clippy::undocumented_unsafe_blocks)]
11-
11+
#![warn(clippy::cast_lossless)]
1212
//! Utility for configuring the CPUID (CPU identification) for the guest microVM.
1313
1414
#![cfg(target_arch = "x86_64")]

src/cpuid/src/transformer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl VmSpec {
3636
cpu_vendor_id,
3737
cpu_index,
3838
cpu_count,
39-
cpu_bits: (cpu_count > 1 && smt) as u8,
39+
cpu_bits: u8::from(cpu_count > 1 && smt),
4040
brand_string: BrandString::from_vendor_id(&cpu_vendor_id),
4141
})
4242
}

src/devices/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#![warn(clippy::ptr_as_ptr)]
99
#![warn(clippy::undocumented_unsafe_blocks)]
10-
10+
#![warn(clippy::cast_lossless)]
1111
//! Emulates virtual and hardware devices.
1212
use std::io;
1313

src/devices/src/virtio/balloon/device.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl Balloon {
303303
// Remove the page ranges.
304304
for (page_frame_number, range_len) in page_ranges {
305305
let guest_addr =
306-
GuestAddress((page_frame_number as u64) << VIRTIO_BALLOON_PFN_SHIFT);
306+
GuestAddress(u64::from(page_frame_number) << VIRTIO_BALLOON_PFN_SHIFT);
307307

308308
if let Err(err) = remove_range(
309309
mem,
@@ -365,7 +365,7 @@ impl Balloon {
365365
// so we ignore the rest of it.
366366
let addr = head
367367
.addr
368-
.checked_add(index as u64)
368+
.checked_add(u64::from(index))
369369
.ok_or(BalloonError::MalformedDescriptor)?;
370370
let stat = mem
371371
.read_obj::<BalloonStat>(addr)
@@ -445,8 +445,8 @@ impl Balloon {
445445

446446
pub fn update_timer_state(&mut self) {
447447
let timer_state = TimerState::Periodic {
448-
current: Duration::from_secs(self.stats_polling_interval_s as u64),
449-
interval: Duration::from_secs(self.stats_polling_interval_s as u64),
448+
current: Duration::from_secs(u64::from(self.stats_polling_interval_s)),
449+
interval: Duration::from_secs(u64::from(self.stats_polling_interval_s)),
450450
};
451451
self.stats_timer
452452
.set_state(timer_state, SetTimeFlags::Default);
@@ -687,7 +687,7 @@ pub(crate) mod tests {
687687

688688
let features: u64 = (1u64 << VIRTIO_F_VERSION_1)
689689
| ((if *deflate_on_oom { 1 } else { 0 }) << VIRTIO_BALLOON_F_DEFLATE_ON_OOM)
690-
| ((*stats_interval as u64) << VIRTIO_BALLOON_F_STATS_VQ);
690+
| ((u64::from(*stats_interval)) << VIRTIO_BALLOON_F_STATS_VQ);
691691

692692
assert_eq!(balloon.avail_features_by_page(0), features as u32);
693693
assert_eq!(balloon.avail_features_by_page(1), (features >> 32) as u32);

src/devices/src/virtio/balloon/persist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl Persist<'_> for Balloon {
145145

146146
// Restart timer if needed.
147147
let timer_state = TimerState::Periodic {
148-
current: Duration::from_secs(state.stats_polling_interval_s as u64),
149-
interval: Duration::from_secs(state.stats_polling_interval_s as u64),
148+
current: Duration::from_secs(u64::from(state.stats_polling_interval_s)),
149+
interval: Duration::from_secs(u64::from(state.stats_polling_interval_s)),
150150
};
151151
balloon
152152
.stats_timer

src/devices/src/virtio/block/io/async_io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<T> AsyncFileEngine<T> {
6767

6868
let completion_evt = EventFd::new(libc::EFD_NONBLOCK).map_err(Error::EventFd)?;
6969
let ring = IoUring::new(
70-
IO_URING_NUM_ENTRIES as u32,
70+
u32::from(IO_URING_NUM_ENTRIES),
7171
vec![&file],
7272
vec![
7373
// Make sure we only allow operations on pre-registered fds.

src/devices/src/virtio/block/io/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ pub mod tests {
240240

241241
fn check_dirty_mem(mem: &GuestMemoryMmap, addr: GuestAddress, len: u32) {
242242
let bitmap = mem.find_region(addr).unwrap().bitmap().as_ref().unwrap();
243-
for offset in addr.0..addr.0 + len as u64 {
243+
for offset in addr.0..addr.0 + u64::from(len) {
244244
assert!(bitmap.dirty_at(offset as usize));
245245
}
246246
}
247247

248248
fn check_clean_mem(mem: &GuestMemoryMmap, addr: GuestAddress, len: u32) {
249249
let bitmap = mem.find_region(addr).unwrap().bitmap().as_ref().unwrap();
250-
for offset in addr.0..addr.0 + len as u64 {
250+
for offset in addr.0..addr.0 + u64::from(len) {
251251
assert!(!bitmap.dirty_at(offset as usize));
252252
}
253253
}

src/devices/src/virtio/mmio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl BusDevice for MmioTransport {
235235
features
236236
}
237237
0x34 => self.with_queue(0, |q| u32::from(q.get_max_size())),
238-
0x44 => self.with_queue(0, |q| q.ready as u32),
238+
0x44 => self.with_queue(0, |q| u32::from(q.ready)),
239239
0x60 => self.interrupt_status.load(Ordering::SeqCst) as u32,
240240
0x70 => self.device_status,
241241
0xfc => self.config_generation,
@@ -513,7 +513,7 @@ pub(crate) mod tests {
513513
assert_eq!(read_le_u32(&buf[..]), 16);
514514

515515
d.read(0x44, &mut buf[..]);
516-
assert_eq!(read_le_u32(&buf[..]), false as u32);
516+
assert_eq!(read_le_u32(&buf[..]), u32::from(false));
517517

518518
d.interrupt_status.store(111, Ordering::SeqCst);
519519
d.read(0x60, &mut buf[..]);

src/devices/src/virtio/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a> VirtQueue<'a> {
325325

326326
pub fn check_used_elem(&self, used_index: u16, expected_id: u16, expected_len: u32) {
327327
let used_elem = self.used.ring[used_index as usize].get();
328-
assert_eq!(used_elem.id, expected_id as u32);
328+
assert_eq!(used_elem.id, u32::from(expected_id));
329329
assert_eq!(used_elem.len, expected_len);
330330
}
331331
}

src/dumbo/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![deny(missing_docs)]
55
#![warn(clippy::ptr_as_ptr)]
66
#![warn(clippy::undocumented_unsafe_blocks)]
7-
7+
#![warn(clippy::cast_lossless)]
88
//! Provides helper logic for parsing and writing protocol data units, and minimalist
99
//! implementations of a TCP listener, a TCP connection, and an HTTP/1.1 server.
1010
pub mod pdu;

src/firecracker/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![warn(clippy::ptr_as_ptr)]
55
#![warn(clippy::undocumented_unsafe_blocks)]
6+
#![warn(clippy::cast_lossless)]
67

78
mod api_server_adapter;
89
mod metrics;

src/io_uring/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![deny(missing_docs)]
55
#![warn(clippy::ptr_as_ptr)]
66
#![warn(clippy::undocumented_unsafe_blocks)]
7-
7+
#![warn(clippy::cast_lossless)]
88
//! High-level interface over Linux io_uring.
99
//!
1010
//! Aims to provide an easy-to-use interface, while making some Firecracker-specific simplifying
@@ -368,7 +368,8 @@ impl IoUring {
368368
let supported_opcodes: HashSet<u8> = probes
369369
.as_slice()
370370
.iter()
371-
.filter(|op| ((op.flags as u32) & bindings::IO_URING_OP_SUPPORTED) != 0)
371+
//.filter(|op| ((op.flags as u32) & bindings::IO_URING_OP_SUPPORTED) != 0)
372+
.filter(|op| ((u32::from(op.flags)) & bindings::IO_URING_OP_SUPPORTED) != 0)
372373
.map(|op| op.op)
373374
.collect();
374375

src/jailer/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![warn(clippy::ptr_as_ptr)]
55
#![warn(clippy::undocumented_unsafe_blocks)]
6+
#![warn(clippy::cast_lossless)]
67

78
mod cgroup;
89
mod chroot;

src/logger/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![deny(missing_docs)]
55
#![warn(clippy::ptr_as_ptr)]
66
#![warn(clippy::undocumented_unsafe_blocks)]
7-
7+
#![warn(clippy::cast_lossless)]
88
//! Crate that implements Firecracker specific functionality as far as logging and metrics
99
//! collecting.
1010

src/mmds/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![warn(clippy::ptr_as_ptr)]
55
#![warn(clippy::undocumented_unsafe_blocks)]
6+
#![warn(clippy::cast_lossless)]
67

78
pub mod data_store;
89
pub mod ns;

src/mmds/src/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl TokenAuthority {
277277
// to current time (also in milliseconds). This addition is safe
278278
// because ttl is verified beforehand and can never be more than
279279
// 6h (21_600_000 ms).
280-
now_as_milliseconds.add(ttl_as_seconds as u64 * MILLISECONDS_PER_SECOND)
280+
now_as_milliseconds.add(u64::from(ttl_as_seconds) * MILLISECONDS_PER_SECOND)
281281
}
282282
}
283283

src/rate_limiter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![deny(missing_docs)]
55
#![warn(clippy::ptr_as_ptr)]
66
#![warn(clippy::undocumented_unsafe_blocks)]
7-
7+
#![warn(clippy::cast_lossless)]
88
//! # Rate Limiter
99
//!
1010
//! Provides a rate limiter written in Rust useful for IO operations that need to

src/rebase-snap/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![warn(clippy::ptr_as_ptr)]
55
#![warn(clippy::undocumented_unsafe_blocks)]
6+
#![warn(clippy::cast_lossless)]
67

78
use std::fs::{File, OpenOptions};
89
use std::io::{Seek, SeekFrom};

src/seccompiler/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![deny(missing_docs)]
44
#![warn(clippy::ptr_as_ptr)]
55
#![warn(clippy::undocumented_unsafe_blocks)]
6+
#![warn(clippy::cast_lossless)]
67

78
//! The library crate that defines common helper functions that are generally used in
89
//! conjunction with seccompiler-bin.

src/snapshot/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![deny(missing_docs)]
44
#![warn(clippy::ptr_as_ptr)]
55
#![warn(clippy::undocumented_unsafe_blocks)]
6+
#![warn(clippy::cast_lossless)]
67

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

9798
fn build_magic_id(format_version: u16) -> u64 {
98-
BASE_MAGIC_ID | format_version as u64
99+
BASE_MAGIC_ID | u64::from(format_version)
99100
}
100101

101102
impl Snapshot {

src/utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#![warn(clippy::ptr_as_ptr)]
55
#![warn(clippy::undocumented_unsafe_blocks)]
6+
#![warn(clippy::cast_lossless)]
67

78
// We use `utils` as a wrapper over `vmm_sys_util` to control the latter
89
// dependency easier (i.e. update only in one place `vmm_sys_util` version).

src/vmm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ impl Vmm {
694694
) -> std::result::Result<(), BalloonError> {
695695
// The balloon cannot have a target size greater than the size of
696696
// the guest memory.
697-
if amount_mib as u64 > mem_size_mib(self.guest_memory()) {
697+
if u64::from(amount_mib) > mem_size_mib(self.guest_memory()) {
698698
return Err(BalloonError::TooManyPagesRequested);
699699
}
700700

src/vmm/src/vmm_config/vsock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub(crate) mod tests {
159159
vsock_config.guest_cid = new_cid;
160160
store.insert(vsock_config).unwrap();
161161
let vsock = store.get().unwrap();
162-
assert_eq!(vsock.lock().unwrap().cid(), new_cid as u64);
162+
assert_eq!(vsock.lock().unwrap().cid(), u64::from(new_cid));
163163
}
164164

165165
#[test]

src/vmm/src/vstate/vcpu/x86_64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ impl KvmVcpu {
438438
// We accept values within a tolerance of 250 parts
439439
// per million beacuse it is common for TSC frequency
440440
// to differ due to calibration at boot time.
441-
let diff = (self.get_tsc_khz()? as i64 - state_tsc_freq as i64).abs();
442-
Ok(diff > (state_tsc_freq as f64 * TSC_KHZ_TOL).round() as i64)
441+
let diff = (i64::from(self.get_tsc_khz()?) - i64::from(state_tsc_freq)).abs();
442+
Ok(diff > (f64::from(state_tsc_freq) * TSC_KHZ_TOL).round() as i64)
443443
}
444444

445445
// Scale the TSC frequency of this vCPU to the one provided as a parameter.

tools/bindgen.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function fc-bindgen {
3333
non_snake_case,
3434
clippy::ptr_as_ptr,
3535
clippy::undocumented_unsafe_blocks
36+
clippy::cast_lossless
3637
)]
3738
3839
EOF

0 commit comments

Comments
 (0)