rust-lang / rust-lang/rust-clippy
Add `cast_known_wrap` for known bad casts in const contexts (`cast_possible_wrap`)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Casting between signed and unsigned constants is somewhat common, especially when working with C FFI (bindgen typically generates unsigned consts, C functions typically use signed ints).
This lint would check for as casts in const contexts and flag any where the RHS does not have the same value as the LHS.
Lint Name
cast_known_wrap
Category
pedantic
Advantage
Catch cases where a C #define doesn't fit into the expected value
Drawbacks
Noise when the cast is intended to just reinterpret the bit value
Example
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
// OK conversion
const A: u32 = 1;
const B: i32 = A as i32;
// Failed conversion
const X: u32 = u32::MAX;
const Y: i32 = X as i32;
// const Z: i32 = X.try_into().unwrap(); // not possible in const
fn main() {
println!("{A} {B}");
println!("{X} {Y}");
}
Could be written as:
(no alternative - just a warning)
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.
Research direction
The issue names the existing cast_possible_wrap lint as the related entry point; start by locating it and reviewing how Clippy handles casts and const contexts. The issue provides no file or test path, so completion means defining and validating cast_known_wrap for const casts whose value changes, while avoiding warnings for equal-value conversions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100