Skip to content

Conversation

@arianbeh
Copy link

Changes

  • Enabled the clippy::as_conversions lint at the workspace level by updating
    Cargo.toml.
  • Resolved all resulting as_conversions violations across the workspace by:
    • Replacing unsafe pointer casts with std::ptr::from_ref / std::ptr::from_mut
      where appropriate (e.g. in cpu-template-helper).
    • Replacing silent integer casts with explicit conversions such as
      i64::from(), u64::try_from(), and usize::try_from().
    • Using localized #[allow(clippy::as_conversions)] in low-level or const
      contexts where as is intentional and required (e.g., enum-to-byte
      encoding in ACPI AML, seccompiler const fns, and libc constant conversions).
    • Cleaning up comparisons and length/size conversions to avoid silent
      truncation.
  • Updated the necessary code paths in:
    • cpu-template-helper
    • utils::time
    • acpi-tables (AML, DSDT, MADT, XSDT)
    • seccompiler (bindings, types, lib)
    • rebase-snap
    • jailer (resource_limits)
    • snapshot-editor
    • firecracker (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_conversions and fixing all violations.

Enabling as_conversions ensures:

  • Integer and pointer casts are either:
    • Performed via explicit, safe conversion APIs, or
    • Clearly scoped behind #[allow] where they are intentional and unavoidable.
  • Silent truncation, wraparound, and lossy conversions are avoided.
  • The workspace stays clean under RUSTFLAGS="-Dwarnings".
  • Future code introducing unsafe as casts 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-targets
  • cargo fmt

All packages build clean with no Clippy warnings.

I was not able to run tools/devtool checkbuild or tools/devtool checkstyle
because /dev/kvm is not available in my environment.

Closes #3161.

Signed-off-by: Aryan Behmardi <arianbehmard@gmail.com>
@JamesC1305 JamesC1305 self-assigned this Dec 17, 2025
@JamesC1305
Copy link
Contributor

JamesC1305 commented Dec 19, 2025

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 repr(u8) on enums you're defining From for. We can then use#[allow(clippy::as_conversions)] to allow a single as cast inside the from function. This doesn't prevent us using as entirely, but it makes our use case explicit, which should prevent any unwanted consequences whilst preventing unnecessary code bloat.

I'll leave a review on your PR and point it our :)

Comment on lines +749 to +761
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,
}
}
}

Copy link
Contributor

@JamesC1305 JamesC1305 Dec 19, 2025

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:

Suggested change
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
}
}
}

Comment on lines +852 to +868
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,
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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()
Copy link
Contributor

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 :)

Comment on lines 762 to 768
#[derive(Clone, Copy)]
pub enum FieldUpdateRule {
Preserve = 0,
WriteAsOnes = 1,
WriteAsZeroes = 2,
}

Copy link
Contributor

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.

Suggested change
#[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,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use clippy::as_conversions

2 participants