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

arianbeh and others added 3 commits December 27, 2025 14:19
Co-authored-by: James Curtis <jamescurtis2003@live.co.uk>
Co-authored-by: James Curtis <jamescurtis2003@live.co.uk>
Co-authored-by: James Curtis <jamescurtis2003@live.co.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants