Skip to content

clippy::cast_possible_truncation is too broad #15197

Open
@demosdemon

Description

@demosdemon

Summary

cast_possible_truncation pedantically fires for any u64 -> usize conversion with no respect to the target platforms.

https://github.com/rust-lang/rust-clippy/blame/ad15b600227dd8fc95915d3b23d131f2dfb9ff7a/clippy_lints/src/casts/cast_possible_truncation.rs#L113C17-L113C94

Some projects may have no need to support 32-bit platforms while still wanting to use the cast_possible_truncation for the other cases of possible truncation. However, the u64 -> usize warning becomes increasingly noisy in codebases that deal with with filesystems and similar APIs so the entire lint ends up allowed.

Therefore, I feel it's reasonable to say cast_possible_truncation has overly broad scope. The " on targets with 32-bit wide pointers" suffix appended to the warning lends to it being overly broad by acknowledging that it may be firing unnecessarily.

I would like to keep cast_possible_truncation enabled in my codebase but have it be silent when casting u64 -> usize when my target is also 64-bit. Lints are target agnostic, so perhaps we can split cast_possible_truncation into a separate warning when u64 -> usize. And maybe also usize -> u32 for the inverse reason.

Reproducer

// target_pointer_width = "64" not necessary but included to illustrate the overbroadness
#[cfg(target_pointer_width = "64")]
#[deny(clippy::cast_possible_truncation)]
const fn thunk(v: u64) -> usize {
    v as usize
}

Version

rustc 1.90.0-nightly (f26e58023 2025-06-30)
binary: rustc
commit-hash: f26e58023071e71636a3c72ac3a2bf89b1f76706
commit-date: 2025-06-30
host: aarch64-apple-darwin
release: 1.90.0-nightly
LLVM version: 20.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions