Skip to content

Commit

Permalink
Add clippy::cast_lossless warning
Browse files Browse the repository at this point in the history
Related issue - #3196

Signed-off-by: StemCll [email protected]
  • Loading branch information
StemCll committed Oct 31, 2022
1 parent dd1a77e commit 86300b8
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 32 deletions.
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/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
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 @@ -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));
}
}
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 @@ -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.
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 @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/io_uring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ 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| ((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
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/snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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
2 changes: 1 addition & 1 deletion src/vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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
16 changes: 8 additions & 8 deletions tests/integration_tests/build/test_clippy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

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.
@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)
)

0 comments on commit 86300b8

Please sign in to comment.