rust-lang / rust-lang/rust-clippy
Have a lint against usize-to-u64 casts (or, against *all* integer casts)
@big14way is already working on this.
Since Jul 17, 2026.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
I would like clippy to lint against all integer casts. So I have set:
#![warn(
clippy::cast_possible_wrap, // unsigned -> signed
clippy::cast_sign_loss, // signed -> unsigned
clippy::cast_lossless,
clippy::cast_possible_truncation,
)]
However, I just by accident noticed that this does not lint against usize-to-u64 casts. I guess cast_possible_truncation says "this cannot truncate because we don't have more than 64bit pointer size", and "cast_lossless" says "ah this might be lossy on platforms with pointers larger than 64bit", and then neither of them does anything.
I would be happy to either have one of these lints also trigger on usize-to-u64 casts, or to have a new lint against all integer casts.
Lint Name
cast_integer
Category
pedantic
Advantage
Integer casts are subtle and should be done via From/TryFrom, never via as, so I want to rule out all of them in my codebase.
Drawbacks
No response
Example
pub fn foo(x: usize) -> u64 { x as u64 }
Could be written as:
pub fn foo(x: usize) -> u64 { u64::try_from(x).unwrap() }
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.