Skip to content

Commit

Permalink
chore: appease clippy
Browse files Browse the repository at this point in the history
A combination of new lints (e.g. about avoiding `std::u\d{1,2}` modules)
and pre-existing lints now applying in more contexts (such as inside
macros).

Also fix a rustc warning about `.cargo/config` being deprecated in favor
of `.cargo/config.toml`.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
  • Loading branch information
roypat committed Jul 16, 2024
1 parent dbf2c1e commit e664d54
Show file tree
Hide file tree
Showing 25 changed files with 59 additions and 73 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/acpi-tables/src/aml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ pub type Usize = usize;

impl Aml for Usize {
fn append_aml_bytes(&self, bytes: &mut Vec<u8>) {
if *self <= u8::max_value().into() {
if *self <= u8::MAX.into() {
TryInto::<u8>::try_into(*self)
.unwrap()
.append_aml_bytes(bytes)
} else if *self <= u16::max_value().into() {
} else if *self <= u16::MAX.into() {
TryInto::<u16>::try_into(*self)
.unwrap()
.append_aml_bytes(bytes)
} else if *self <= u32::max_value() as usize {
} else if *self <= u32::MAX as usize {
TryInto::<u32>::try_into(*self)
.unwrap()
.append_aml_bytes(bytes)
Expand Down
2 changes: 1 addition & 1 deletion src/cpu-template-helper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ mod tests {

#[test]
fn test_template_strip_command() {
let files = vec![generate_sample_template(), generate_sample_template()];
let files = [generate_sample_template(), generate_sample_template()];

let mut args = vec!["cpu-template-helper", "template", "strip", "-p"];
let paths = files
Expand Down
62 changes: 22 additions & 40 deletions src/seccompiler/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ impl SeccompRule {
offset: &mut u8,
) {
// Tries to detect whether prepending the current condition will produce an unjumpable
// offset (since BPF jumps are a maximum of 255 instructions, which is std::u8::MAX).
// offset (since BPF jumps are a maximum of 255 instructions, which is u8::MAX).
if offset.checked_add(CONDITION_MAX_LEN + 1).is_none() {
// If that is the case, three additional helper jumps are prepended and the offset
// is reset to 1.
Expand Down Expand Up @@ -1010,15 +1010,15 @@ mod tests {
let rules = vec![allow_syscall_if(
libc::SYS_ioctl,
vec![SeccompRule::new(
vec![Cond::new(2, SeccompCmpArgLen::Qword, Eq, std::u64::MAX).unwrap()],
vec![Cond::new(2, SeccompCmpArgLen::Qword, Eq, u64::MAX).unwrap()],
SeccompAction::Allow,
)],
)];
// check syscalls that are supposed to work
validate_seccomp_filter(
rules.clone(),
|| unsafe {
libc::ioctl(0, 0, std::u64::MAX);
libc::ioctl(0, 0, u64::MAX);
},
false,
);
Expand Down Expand Up @@ -1064,16 +1064,16 @@ mod tests {
let rules = vec![allow_syscall_if(
libc::SYS_ioctl,
vec![SeccompRule::new(
vec![Cond::new(2, SeccompCmpArgLen::Qword, Ge, u64::from(std::u32::MAX)).unwrap()],
vec![Cond::new(2, SeccompCmpArgLen::Qword, Ge, u64::from(u32::MAX)).unwrap()],
SeccompAction::Allow,
)],
)];
// check syscalls that are supposed to work
validate_seccomp_filter(
rules.clone(),
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX));
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 1);
libc::ioctl(0, 0, u64::from(u32::MAX));
libc::ioctl(0, 0, u64::from(u32::MAX) + 1);
},
false,
);
Expand Down Expand Up @@ -1118,29 +1118,23 @@ mod tests {
let rules = vec![allow_syscall_if(
libc::SYS_ioctl,
vec![SeccompRule::new(
vec![Cond::new(
2,
SeccompCmpArgLen::Qword,
Gt,
u64::from(std::u32::MAX) + 10,
)
.unwrap()],
vec![Cond::new(2, SeccompCmpArgLen::Qword, Gt, u64::from(u32::MAX) + 10).unwrap()],
SeccompAction::Allow,
)],
)];
// check syscalls that are supposed to work
validate_seccomp_filter(
rules.clone(),
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 11);
libc::ioctl(0, 0, u64::from(u32::MAX) + 11);
},
false,
);
// check syscalls that are not supposed to work
validate_seccomp_filter(
rules,
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 10);
libc::ioctl(0, 0, u64::from(u32::MAX) + 10);
},
true,
);
Expand Down Expand Up @@ -1178,30 +1172,24 @@ mod tests {
let rules = vec![allow_syscall_if(
libc::SYS_ioctl,
vec![SeccompRule::new(
vec![Cond::new(
2,
SeccompCmpArgLen::Qword,
Le,
u64::from(std::u32::MAX) + 10,
)
.unwrap()],
vec![Cond::new(2, SeccompCmpArgLen::Qword, Le, u64::from(u32::MAX) + 10).unwrap()],
SeccompAction::Allow,
)],
)];
// check syscalls that are supposed to work
validate_seccomp_filter(
rules.clone(),
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 10);
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 9);
libc::ioctl(0, 0, u64::from(u32::MAX) + 10);
libc::ioctl(0, 0, u64::from(u32::MAX) + 9);
},
false,
);
// check syscalls that are not supposed to work
validate_seccomp_filter(
rules,
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 11);
libc::ioctl(0, 0, u64::from(u32::MAX) + 11);
},
true,
);
Expand Down Expand Up @@ -1238,29 +1226,23 @@ mod tests {
let rules = vec![allow_syscall_if(
libc::SYS_ioctl,
vec![SeccompRule::new(
vec![Cond::new(
2,
SeccompCmpArgLen::Qword,
Lt,
u64::from(std::u32::MAX) + 10,
)
.unwrap()],
vec![Cond::new(2, SeccompCmpArgLen::Qword, Lt, u64::from(u32::MAX) + 10).unwrap()],
SeccompAction::Allow,
)],
)];
// check syscalls that are supposed to work
validate_seccomp_filter(
rules.clone(),
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 9);
libc::ioctl(0, 0, u64::from(u32::MAX) + 9);
},
false,
);
// check syscalls that are not supposed to work
validate_seccomp_filter(
rules,
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX) + 10);
libc::ioctl(0, 0, u64::from(u32::MAX) + 10);
},
true,
);
Expand Down Expand Up @@ -1307,8 +1289,8 @@ mod tests {
vec![Cond::new(
2,
SeccompCmpArgLen::Qword,
MaskedEq(u64::from(std::u32::MAX)),
std::u64::MAX,
MaskedEq(u64::from(u32::MAX)),
u64::MAX,
)
.unwrap()],
SeccompAction::Allow,
Expand All @@ -1318,8 +1300,8 @@ mod tests {
validate_seccomp_filter(
rules.clone(),
|| unsafe {
libc::ioctl(0, 0, u64::from(std::u32::MAX));
libc::ioctl(0, 0, std::u64::MAX);
libc::ioctl(0, 0, u64::from(u32::MAX));
libc::ioctl(0, 0, u64::MAX);
},
false,
);
Expand Down Expand Up @@ -1364,7 +1346,7 @@ mod tests {
let rules = vec![allow_syscall_if(
libc::SYS_ioctl,
vec![SeccompRule::new(
vec![Cond::new(2, SeccompCmpArgLen::Qword, Ne, std::u64::MAX).unwrap()],
vec![Cond::new(2, SeccompCmpArgLen::Qword, Ne, u64::MAX).unwrap()],
SeccompAction::Allow,
)],
)];
Expand All @@ -1380,7 +1362,7 @@ mod tests {
validate_seccomp_filter(
rules,
|| unsafe {
libc::ioctl(0, 0, std::u64::MAX);
libc::ioctl(0, 0, u64::MAX);
},
true,
);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/src/byte_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ macro_rules! generate_read_fn {
pub fn $fn_name(input: &[$byte_type]) -> $data_type {
assert!($type_size == std::mem::size_of::<$data_type>());
let mut array = [0u8; $type_size];
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_wrap)]
for (byte, read) in array.iter_mut().zip(input.iter().cloned()) {
*byte = read as u8;
}
Expand All @@ -17,6 +19,8 @@ macro_rules! generate_read_fn {
macro_rules! generate_write_fn {
($fn_name: ident, $data_type: ty, $byte_type: ty, $endian_type: ident) => {
pub fn $fn_name(buf: &mut [$byte_type], n: $data_type) {
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_wrap)]
for (byte, read) in buf
.iter_mut()
.zip(<$data_type>::$endian_type(n).iter().cloned())
Expand Down Expand Up @@ -81,6 +85,8 @@ mod tests {
];

let type_size = std::mem::size_of::<$data_type>();
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
for (test_val, v_arr) in &test_cases {
let v = *test_val as $data_type;
let cmp_iter: Box<dyn Iterator<Item = _>> = if $is_be {
Expand Down
4 changes: 2 additions & 2 deletions src/vmm/src/arch/aarch64/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ macro_rules! arm64_core_reg_id {
// id = KVM_REG_ARM64 | KVM_REG_SIZE_U64/KVM_REG_SIZE_U32/KVM_REG_SIZE_U128 |
// KVM_REG_ARM_CORE | offset
KVM_REG_ARM64 as u64
| KVM_REG_ARM_CORE as u64
| u64::from(KVM_REG_ARM_CORE)
| $size
| (($offset / std::mem::size_of::<u32>()) as u64)
| u64::from($offset / std::mem::size_of::<u32>())
};
}
pub(crate) use arm64_core_reg_id;
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/arch/x86_64/mptable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const MPC_SIGNATURE: [c_char; 4] = char_array!(c_char; 'P', 'C', 'M', 'P');
const MPC_SPEC: i8 = 4;
const MPC_OEM: [c_char; 8] = char_array!(c_char; 'F', 'C', ' ', ' ', ' ', ' ', ' ', ' ');
const MPC_PRODUCT_ID: [c_char; 12] = ['0' as c_char; 12];
const BUS_TYPE_ISA: [u8; 6] = char_array!(u8; 'I', 'S', 'A', ' ', ' ', ' ');
const BUS_TYPE_ISA: [u8; 6] = [b'I', b'S', b'A', b' ', b' ', b' '];
const IO_APIC_DEFAULT_PHYS_BASE: u32 = 0xfec0_0000; // source: linux/arch/x86/include/asm/apicdef.h
const APIC_DEFAULT_PHYS_BASE: u32 = 0xfee0_0000; // source: linux/arch/x86/include/asm/apicdef.h
const APIC_VERSION: u8 = 0x14;
Expand Down
4 changes: 2 additions & 2 deletions src/vmm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,12 @@ fn load_initrd_from_config(
/// * `image` - The initrd image.
///
/// Returns the result of initrd loading
fn load_initrd<F: Debug>(
fn load_initrd<F>(
vm_memory: &GuestMemoryMmap,
image: &mut F,
) -> Result<InitrdConfig, StartMicrovmError>
where
F: ReadVolatile + Seek,
F: ReadVolatile + Seek + Debug,
{
use self::StartMicrovmError::{InitrdLoad, InitrdRead};

Expand Down
10 changes: 5 additions & 5 deletions src/vmm/src/cpu_config/x86_64/cpuid/intel/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ mod tests {
clippy::as_conversions
)]

use std::ffi::CStr;

use super::*;
#[test]
fn default_brand_string_test() {
Expand Down Expand Up @@ -365,11 +367,9 @@ mod tests {
result,
Err(DefaultBrandStringError::Overflow),
"{:?}",
result.as_ref().map(|s| unsafe {
std::ffi::CStr::from_ptr((s as *const u8).cast())
.to_str()
.unwrap()
}),
result
.as_ref()
.map(|s| CStr::from_bytes_until_nul(s).unwrap()),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/device_manager/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ mod tests {
// know will results in `Ok`
let mut clone = MMIODeviceManager::new();
// We only care about the device hashmap.
clone.id_to_dev_info = self.id_to_dev_info.clone();
clone.id_to_dev_info.clone_from(&self.id_to_dev_info);
clone
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/legacy/rtc_pl031.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use vm_superio::rtc_pl031::RtcEvents;
use crate::logger::{warn, IncMetric, SharedIncMetric};

/// Metrics specific to the RTC device.
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Default)]
pub struct RTCDeviceMetrics {
/// Errors triggered while using the RTC device.
pub error_count: SharedIncMetric,
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/legacy/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const IER_RDA_BIT: u8 = 0b0000_0001;
pub const IER_RDA_OFFSET: u8 = 1;

/// Metrics specific to the UART device.
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Default)]
pub struct SerialDeviceMetrics {
/// Errors triggered while using the UART device.
pub error_count: SharedIncMetric,
Expand Down
2 changes: 0 additions & 2 deletions src/vmm/src/devices/virtio/balloon/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,6 @@ impl VirtioDevice for Balloon {

#[cfg(test)]
pub(crate) mod tests {
use std::u32;

use super::super::BALLOON_CONFIG_SPACE_SIZE;
use super::*;
use crate::check_metric_after_block;
Expand Down
2 changes: 0 additions & 2 deletions src/vmm/src/devices/virtio/balloon/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#![doc(hidden)]

use std::u32;

use crate::devices::virtio::test_utils::VirtQueue;
#[cfg(test)]
use crate::devices::virtio::{balloon::Balloon, balloon::BALLOON_NUM_QUEUES};
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/balloon/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ mod tests {
fn random_pfn_u32_max() -> impl Strategy<Value = Vec<u32>> {
// Create a randomly sized vec (max MAX_PAGE_COMPACT_BUFFER elements) filled with random u32
// elements.
prop::collection::vec(0..std::u32::MAX, 0..MAX_PAGE_COMPACT_BUFFER)
prop::collection::vec(0..u32::MAX, 0..MAX_PAGE_COMPACT_BUFFER)
}

#[allow(clippy::let_with_type_underscore)]
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/block/virtio/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ mod tests {
use std::fs::metadata;
use std::io::Read;
use std::os::unix::ffi::OsStrExt;
use std::thread;
use std::time::Duration;
use std::{thread, u32};

use utils::skip_if_io_uring_unsupported;
use utils::tempfile::TempFile;
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/net/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ pub mod tests {
}

for i in 0..10 {
net.ack_features_by_page(i, std::u32::MAX);
net.ack_features_by_page(i, u32::MAX);
}

assert_eq!(net.acked_features, features);
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/net/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ pub mod tests {
drop(METRICS.write().unwrap());

NetMetricsPerDevice::alloc(String::from(devn));
assert!(METRICS.read().unwrap().metrics.get(devn).is_some());
METRICS.read().unwrap().metrics.get(devn).unwrap();

METRICS
.read()
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/rng/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ mod tests {
}

for i in 0..10 {
entropy_dev.ack_features_by_page(i, std::u32::MAX);
entropy_dev.ack_features_by_page(i, u32::MAX);
}

assert_eq!(entropy_dev.acked_features, features);
Expand Down
Loading

0 comments on commit e664d54

Please sign in to comment.