-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: enable clippy as_conversions lint across workspace #5577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: enable clippy as_conversions lint across workspace #5577
Conversation
Signed-off-by: Aryan Behmardi <arianbehmard@gmail.com>
|
Hi @arianbeh, Thank you for your contribution, this definitely looks like a good QoL change for us. The only thing I'd suggest as an improvement is to use I'll leave a review on your PR and point it our :) |
| impl From<FieldAccessType> for u8 { | ||
| fn from(val: FieldAccessType) -> u8 { | ||
| match val { | ||
| FieldAccessType::Any => 0, | ||
| FieldAccessType::Byte => 1, | ||
| FieldAccessType::Word => 2, | ||
| FieldAccessType::DWord => 3, | ||
| FieldAccessType::QWord => 4, | ||
| FieldAccessType::Buffer => 5, | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we add repr(u8) to the FieldAccessType enum, we can simplify this to:
| impl From<FieldAccessType> for u8 { | |
| fn from(val: FieldAccessType) -> u8 { | |
| match val { | |
| FieldAccessType::Any => 0, | |
| FieldAccessType::Byte => 1, | |
| FieldAccessType::Word => 2, | |
| FieldAccessType::DWord => 3, | |
| FieldAccessType::QWord => 4, | |
| FieldAccessType::Buffer => 5, | |
| } | |
| } | |
| } | |
| #[allow(clippy::as_conversions)] | |
| impl From<FieldAccessType> for u8 { | |
| fn from(val: FieldAccessType) -> u8 { | |
| val as u8 | |
| } | |
| } | |
| } | |
| impl From<OpRegionSpace> for u8 { | ||
| fn from(val: OpRegionSpace) -> u8 { | ||
| match val { | ||
| OpRegionSpace::SystemMemory => 0, | ||
| OpRegionSpace::SystemIo => 1, | ||
| OpRegionSpace::PConfig => 2, | ||
| OpRegionSpace::EmbeddedControl => 3, | ||
| OpRegionSpace::Smbus => 4, | ||
| OpRegionSpace::SystemCmos => 5, | ||
| OpRegionSpace::PciBarTarget => 6, | ||
| OpRegionSpace::Ipmi => 7, | ||
| OpRegionSpace::GeneralPurposeIo => 8, | ||
| OpRegionSpace::GenericSerialBus => 9, | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| impl From<OpRegionSpace> for u8 { | |
| fn from(val: OpRegionSpace) -> u8 { | |
| match val { | |
| OpRegionSpace::SystemMemory => 0, | |
| OpRegionSpace::SystemIo => 1, | |
| OpRegionSpace::PConfig => 2, | |
| OpRegionSpace::EmbeddedControl => 3, | |
| OpRegionSpace::Smbus => 4, | |
| OpRegionSpace::SystemCmos => 5, | |
| OpRegionSpace::PciBarTarget => 6, | |
| OpRegionSpace::Ipmi => 7, | |
| OpRegionSpace::GeneralPurposeIo => 8, | |
| OpRegionSpace::GenericSerialBus => 9, | |
| } | |
| } | |
| } | |
| #[allow(clippy::as_conversions)] | |
| impl From<OpRegionSpace> for u8 { | |
| fn from(val: OpRegionSpace) -> u8 { | |
| val as u8 | |
| } | |
| } | |
| impl Sdt for Dsdt { | ||
| fn len(&self) -> usize { | ||
| self.header.length.get() as usize | ||
| usize::try_from(self.header.length.get()).unwrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try_from(...).unwrap() syntax is a bit unnecessarily verbose for our use case, given that we only support 64-bit systems. The conversion from u32 -> usize should therefore never fail. However, we can leave that as is for now, and address it more broadly across the codebase in a later PR :)
| #[derive(Clone, Copy)] | ||
| pub enum FieldUpdateRule { | ||
| Preserve = 0, | ||
| WriteAsOnes = 1, | ||
| WriteAsZeroes = 2, | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add repr(u8) to explicitly tell the compiler that this enum should be represented by a u8.
| #[derive(Clone, Copy)] | |
| pub enum FieldUpdateRule { | |
| Preserve = 0, | |
| WriteAsOnes = 1, | |
| WriteAsZeroes = 2, | |
| } | |
| #[repr(u8)] | |
| #[derive(Clone, Copy)] | |
| pub enum FieldUpdateRule { | |
| Preserve = 0, | |
| WriteAsOnes = 1, | |
| WriteAsZeroes = 2, | |
| } | |
Changes
clippy::as_conversionslint at the workspace level by updatingCargo.toml.as_conversionsviolations across the workspace by:std::ptr::from_ref/std::ptr::from_mutwhere appropriate (e.g. in
cpu-template-helper).i64::from(),u64::try_from(), andusize::try_from().#[allow(clippy::as_conversions)]in low-level or constcontexts where
asis intentional and required (e.g., enum-to-byteencoding in ACPI AML, seccompiler const fns, and libc constant conversions).
truncation.
cpu-template-helperutils::timeacpi-tables(AML, DSDT, MADT, XSDT)seccompiler(bindings, types, lib)rebase-snapjailer(resource_limits)snapshot-editorfirecracker(main and examples)vmm(library, benches, and tests)Reason
This PR implements the final step of #3161, consolidating several
cast-related Clippy lints into
as_conversionsand fixing all violations.Enabling
as_conversionsensures:#[allow]where they are intentional and unavoidable.RUSTFLAGS="-Dwarnings".ascasts will be flagged consistently.All changes preserve existing behavior while making the code safer and more
explicit about type boundaries.
Verification
Locally ran:
RUSTFLAGS="-Dwarnings" cargo clippy --all --all-targetscargo fmtAll packages build clean with no Clippy warnings.
I was not able to run
tools/devtool checkbuildortools/devtool checkstylebecause
/dev/kvmis not available in my environment.Closes #3161.