Skip to content

crosspointer_transmute: fires on *const usize -> usize and suppresses the as cast suggestion #17871

Open

Description

@bardiharborow

Summary

crosspointer_transmute treats transmuting *const usize (or *mut usize, *const isize, ...) to usize as transmuting a pointer to its pointee. It is almost always just getting the pointer's address. The pointee type only happens to match the target type.

Because the lint fires, transmutes_expressible_as_ptr_casts is suppressed: the transmute lints in clippy_lints/src/transmute/mod.rs only run it when nothing else has linted. So the user gets a misleading warning in place of the useful p as usize suggestion. Change the pointee type to anything else (e.g. *const u64) and the cast suggestion appears as expected.

The reverse direction (usize -> *const usize) is not affected, because useless_transmute handles int-to-pointer first and returns early.

Lint Name

crosspointer_transmute

Reproducer

I tried this code:

#![warn(clippy::crosspointer_transmute)]

fn addr(p: *const usize) -> usize {
    unsafe { std::mem::transmute(p) }
}

fn addr2(p: *const u64) -> usize {
    unsafe { std::mem::transmute(p) }
}

I saw this happen:

warning: transmute from a type (`*const usize`) to the type that it points to (`usize`)
 --> src/lib.rs:4:14
  |
4 |     unsafe { std::mem::transmute(p) }
  |              ^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#crosspointer_transmute

warning: transmute from `*const u64` to `usize` which could be expressed as a pointer cast instead
 --> src/lib.rs:8:14
  |
8 |     unsafe { std::mem::transmute(p) }
  |              ^^^^^^^^^^^^^^^^^^^^^^ help: try: `p as usize`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#transmutes_expressible_as_ptr_casts

I expected to see this happen:

Both functions should get the same transmutes_expressible_as_ptr_casts warning with the p as usize suggestion. crosspointer_transmute should not fire when the pointee is an integer type, because a pointer-to-integer transmute is a well-known idiom for getting the address.

Possible fix: in crosspointer_transmute::check, skip the pointer -> pointee arm when the pointee is ty::Int(_) | ty::Uint(_). Or, at least, don't report it as linted, so the cast suggestion still fires. Note that the existing UI tests (tests/ui/transmute.rs, fn crosspointer) use a struct Usize(usize) wrapper, so they never exercise this case. A regression test with a bare *const usize should be added.

Version

rustc 1.101.0-nightly (21b707e3f 2026-09-30)
binary: rustc
commit-hash: 21b707e3f97e0b522ebd2f277a862339625ad83f
commit-date: 2026-09-30
host: aarch64-apple-darwin
release: 1.101.0-nightly
LLVM version: 23.1.1

Additional Labels

No response

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Oct 13, 2026
  2. bhirahatees-periyasamy commented on Oct 13, 2026

    @bhirahatees-periyasamy
    Contributor

    I was able to reproduce this issue. @rustbot claim

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

Metadata

Metadata

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions