Skip to content

cast_ptr_alignment doesn't see alignment check that follows right after #17636

Description

@nazar-pc

Summary

The lint is all about alignment, but doesn't take manual alignment checks into consideration

Lint Name

cast_ptr_alignment

Reproducer

I tried this code:

let bytes_ptr = bytes.as_ptr().cast::<u128>();
if !bytes_ptr.is_aligned()
    || bytes.len()
        < size_of::<TransactionHeader>() + size_of::<SerializedTransactionLengths>()
{
    return None;
}

I saw this happen:

warning: casting from `*const u8` to a more-strictly-aligned pointer (`*const u128`) (1 < 16 bytes)
   --> crates/shared/ab-core-primitives/src/transaction.rs:136:25
    |
136 |         let bytes_ptr = bytes.as_ptr().cast::<u128>();
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#cast_ptr_alignment

I tried this code:

if !bytes.as_ptr().cast::<u128>().is_aligned()
    || bytes.len()
        < size_of::<TransactionHeader>() + size_of::<SerializedTransactionLengths>()
{
    return None;
}

I saw this happen:

   --> crates/shared/ab-core-primitives/src/transaction.rs:136:13
    |
136 |         if !bytes.as_ptr().cast::<u128>().is_aligned()
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#cast_ptr_alignment
    = note: `-W clippy::cast-ptr-alignment` implied by `-W clippy::pedantic`
    = help: to override `-W clippy::pedantic` add `#[allow(clippy::cast_ptr_alignment)]`

I expected to see this happen: No warning since the code explicitly checks pointer alignment right after casting

Version

rustc 1.100.0-nightly (e7769602a 2026-08-24)
binary: rustc
commit-hash: e7769602aca3770e8d8ea55716becb22e839a579
commit-date: 2026-08-24
host: x86_64-unknown-linux-gnu
release: 1.100.0-nightly
LLVM version: 23.1.0

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't haveL-pedanticLint: Belongs in the pedantic lint group

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions