-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: add cast_ptr_sized_int lint
#16262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
rustbot has assigned @samueltardieu. Use |
This comment has been minimized.
This comment has been minimized.
|
Lintcheck changes for fa57af9
This comment will be updated if you push new changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't those cases already covered by the cast_lossless, cast_possible_truncation, cast_possible_wrap, and cast_sign_loss lints?
Moreover, the suggestion transforms integer types into options, which is not a directly applicable fix.
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
This is exactly the gap the lint addresses: If you compile on 64-bit, usize as u64 looks safe. But the same code behaves differently on 32-bit. No existing lint catches this architecture-dependent behavior. The suggestion is not directly applicable: should i remove the suggestion (just warn without providing a fix)? |
|
The lint still triggers inconsistently. For example:
And indeed, no fix should be provided if it would be incorrect, but the help message may suggest using |
Introduce a new lint to detect casts between pointer-sized integers (`usize`, `isize`) and fixed-size integer types. - Identifies architecture-dependent casts that may lead to truncation or unexpected behavior when moving between 32-bit and 64-bit platforms. - Encourages the use of `TryFrom` or `TryInto` to make potential conversion failures explicit and handled. - Includes optimization logic to ignore always-safe casts, such as widening conversions from small fixed-size integers to pointer-sized types.
Introduce a new restriction lint to identify casts between pointer-sized integer types (
usize,isize) and fixed-size integer types.Encourage the use of
TryFromorTryIntoover directascasts to make potential platform-specific truncation or zero-extension explicit and handled.Also Provide machine-applicable suggestions to replace risky casts with fallible conversions while excluding constant contexts where such traits are not yet stable.
Closes: #9231
changelog: add cast_ptr_sized_int lint