-
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 fba9c01
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 |
Implement a new lint to detect casts between pointer-sized integers (`usize`, `isize`) and fixed-size integers that exhibit architecture-dependent behavior.
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
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.
If you add
#![deny(clippy::cast_lossless, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_sign_loss)]at the top of your test file, you'll see that most of the lines that your PR lints will already be linted.
I wonder if it would be more useful to extend existing lints with an additional configuration option to warn when pointer sized ints could cause issues even though they don't on the platform on which they are compiled.
Maybe the user could even specify the range of sizes it is interested in compiling for. For example, target_usize_supported_bounds = [32, 64] would let the user indicate that they are not interested in compiling for 16 bit platforms, and the lint should report an error if the current platform is outside this range.
What do you think?
| /// u64::try_from(x).expect("usize should fit in u64") | ||
| /// } | ||
| /// ``` | ||
| #[clippy::version = "1.89.0"] |
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.
| #[clippy::version = "1.89.0"] | |
| #[clippy::version = "1.95.0"] |
| @@ -0,0 +1,89 @@ | |||
| //@no-rustfix | |||
| #![warn(clippy::cast_ptr_sized_int)] | |||
| #![allow(clippy::unnecessary_cast)] | |||
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.
Why is this needed?
| @@ -0,0 +1,229 @@ | |||
| error: casting `usize` to `u8`: the behavior depends on the target pointer width | |||
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.
The message is not great here: casting usize to u8 will always truncate.
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