Skip to content

Commit 86300b8

Browse files
committed
Add clippy::cast_lossless warning
Related issue - #3196 Signed-off-by: StemCll [email protected]
1 parent dd1a77e commit 86300b8

File tree

16 files changed

+33
-32
lines changed

16 files changed

+33
-32
lines changed

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/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/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
@@ -239,14 +239,14 @@ pub mod tests {
239239

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

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

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/net/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ pub mod test {
424424
desc.next.set(next_index);
425425
}
426426

427-
addr += len as u64;
427+
addr += u64::from(len);
428428
// Add small random gaps between descriptor addresses in order to make sure we
429429
// don't blindly read contiguous memory.
430-
addr += utils::rand::xor_psuedo_rng_u32() as u64 % 10;
430+
addr += u64::from(utils::rand::xor_psuedo_rng_u32()) % 10;
431431
}
432432

433433
// Mark the chain as available.

src/devices/src/virtio/test_utils.rs

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

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

src/io_uring/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ impl IoUring {
365365
let supported_opcodes: HashSet<u8> = probes
366366
.as_slice()
367367
.iter()
368-
.filter(|op| ((op.flags as u32) & bindings::IO_URING_OP_SUPPORTED) != 0)
368+
//.filter(|op| ((op.flags as u32) & bindings::IO_URING_OP_SUPPORTED) != 0)
369+
.filter(|op| ((u32::from(op.flags)) & bindings::IO_URING_OP_SUPPORTED) != 0)
369370
.map(|op| op.op)
370371
.collect();
371372

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/snapshot/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn get_format_version(magic_id: u64) -> Result<u16, Error> {
9494
}
9595

9696
fn build_magic_id(format_version: u16) -> u64 {
97-
BASE_MAGIC_ID | format_version as u64
97+
BASE_MAGIC_ID | u64::from(format_version)
9898
}
9999

100100
impl Snapshot {

src/vmm/src/lib.rs

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

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.

tests/integration_tests/build/test_clippy.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99

1010
SUCCESS_CODE = 0
1111
MACHINE = platform.machine()
12-
TARGETS = ["{}-unknown-linux-gnu".format(MACHINE),
13-
"{}-unknown-linux-musl".format(MACHINE)]
12+
TARGETS = [
13+
"{}-unknown-linux-gnu".format(MACHINE),
14+
"{}-unknown-linux-musl".format(MACHINE),
15+
]
1416

1517

16-
@pytest.mark.parametrize(
17-
"target",
18-
TARGETS
19-
)
18+
@pytest.mark.parametrize("target", TARGETS)
2019
def test_rust_clippy(target):
2120
"""
2221
Test that clippy does not generate any errors/warnings.
2322
2423
@type: build
2524
"""
2625
utils.run_cmd(
27-
'cargo clippy --target {} --all --profile test'
28-
' -- -D warnings'.format(target))
26+
"cargo clippy --target {} --all --profile test"
27+
" -- -D warnings -D clippy::cast_lossless".format(target)
28+
)

0 commit comments

Comments
 (0)