Skip to content

new lint: manual_offset_from_unsigned#16688

Open
jaroslawroszyk wants to merge 1 commit intorust-lang:masterfrom
jaroslawroszyk:manual_offset_from_unsigned
Open

new lint: manual_offset_from_unsigned#16688
jaroslawroszyk wants to merge 1 commit intorust-lang:masterfrom
jaroslawroszyk:manual_offset_from_unsigned

Conversation

@jaroslawroszyk
Copy link

@jaroslawroszyk jaroslawroszyk commented Mar 8, 2026

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

unsafe {
    let _ = ptr2.offset_from(ptr1) as usize;
}

Can be replaced with:

unsafe {
    let _ = ptr2.offset_from_unsigned(ptr1);
}

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.

changelog: [manual_offset_from_unsigned]: Added a lint to detect manual conversion from offset_from to usize.

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 8, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 8, 2026

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@jaroslawroszyk jaroslawroszyk force-pushed the manual_offset_from_unsigned branch from cb163b0 to f6e6ab2 Compare March 8, 2026 00:41
@github-actions
Copy link

github-actions bot commented Mar 8, 2026

Lintcheck changes for 03cb98a

Lint Added Removed Changed
clippy::manual_offset_from_unsigned 3 0 0

This comment will be updated if you push new changes

@jaroslawroszyk jaroslawroszyk force-pushed the manual_offset_from_unsigned branch from f6e6ab2 to 03cb98a Compare March 8, 2026 00:57
@jaroslawroszyk
Copy link
Author

@rustbot ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New lint: manual_offset_from_unsigned

3 participants