new lint: manual_offset_from_unsigned#16688
new lint: manual_offset_from_unsigned#16688jaroslawroszyk wants to merge 1 commit intorust-lang:masterfrom
Conversation
|
r? @dswij rustbot has assigned @dswij. Use Why was this reviewer chosen?The reviewer was selected based on:
|
cb163b0 to
f6e6ab2
Compare
|
Lintcheck changes for 0e438f0
This comment will be updated if you push new changes |
f6e6ab2 to
03cb98a
Compare
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
03cb98a to
9d77d28
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
9d77d28 to
f2b9fdb
Compare
This comment has been minimized.
This comment has been minimized.
f2b9fdb to
5a5e54c
Compare
|
r? clippy |
|
@rustbot ready |
|
Failed to set assignee to
|
This comment has been minimized.
This comment has been minimized.
5a5e54c to
0e438f0
Compare
|
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. |
|
☔ The latest upstream changes (possibly #16025) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #16156
Description
This PR introduces a new lint manual_offset_from_unsigned that detects patterns like ptr1.offset_from(ptr2) as usize and suggests replacing them with ptr1.offset_from_unsigned(ptr2).
Why is this important?
The offset_from_unsigned method (stabilized in Rust 1.87.0) explicitly handles the safety precondition that the pointer must be greater than or equal to the argument. Using this method is more idiomatic, potentially allows for better compiler optimizations, and makes the code's intent clearer than a raw cast to usize.
Example
Can be replaced with:
Implementation Details
The implementation handles both raw pointers and NonNull types. It uses a helper function to "peel" blocks and unsafe scopes to find the inner method call even when it's wrapped in a block.
I've followed the contribution guidelines
I've added UI tests for the new lint
I've run
cargo dev update_lintschangelog:
[manual_offset_from_unsigned]: Added a lint to detect manual conversion fromoffset_fromtousize.