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
Copy Markdown
Contributor

@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
Copy Markdown
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
Copy Markdown

github-actions Bot commented Mar 8, 2026

Lintcheck changes for 0e438f0

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
Copy Markdown
Contributor Author

@rustbot ready

@rustbot

This comment has been minimized.

@jaroslawroszyk jaroslawroszyk force-pushed the manual_offset_from_unsigned branch from 03cb98a to 9d77d28 Compare March 23, 2026 21:25
@rustbot

This comment has been minimized.

@jaroslawroszyk
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot

This comment has been minimized.

@jaroslawroszyk jaroslawroszyk force-pushed the manual_offset_from_unsigned branch from 9d77d28 to f2b9fdb Compare March 28, 2026 20:43
@rustbot

This comment has been minimized.

@jaroslawroszyk jaroslawroszyk force-pushed the manual_offset_from_unsigned branch from f2b9fdb to 5a5e54c Compare March 28, 2026 20:45
@dswij
Copy link
Copy Markdown
Member

dswij commented Apr 6, 2026

r? clippy

@rustbot rustbot assigned flip1995 and unassigned dswij Apr 6, 2026
@jaroslawroszyk
Copy link
Copy Markdown
Contributor Author

jaroslawroszyk commented Apr 6, 2026

@rustbot ready

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 6, 2026

Failed to set assignee to Clippy: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot assigned Centri3 and unassigned flip1995 Apr 6, 2026
@rustbot

This comment has been minimized.

@jaroslawroszyk jaroslawroszyk force-pushed the manual_offset_from_unsigned branch from 5a5e54c to 0e438f0 Compare April 15, 2026 18:35
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 15, 2026

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
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 24, 2026

☔ The latest upstream changes (possibly #16025) made this pull request unmergeable. Please resolve the merge conflicts.

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

5 participants