rust-lang / rust-lang/rust-clippy

`cast_ptr_alignment` doesn't see alignment check that follows right after

Open
#17,636 4 comments 0 reactions 1 assignee View on GitHub

@saberoueslati is already working on this.

Since Aug 29, 2026.

C-bug E-hard I-false-positive L-pedantic
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.