Skip to content

feat: add cast_ptr_sized_int lint - #16262

Open
ab22593k wants to merge 1 commit into
rust-lang:masterfrom
ab22593k:master
Open

feat: add cast_ptr_sized_int lint#16262
ab22593k wants to merge 1 commit into
rust-lang:masterfrom
ab22593k:master

Conversation

@ab22593k

@ab22593k ab22593k commented Dec 18, 2025

Copy link
Copy Markdown

Introduce a new restriction lint to identify casts between pointer-sized integer types (usize, isize) and fixed-size integer types.

Encourage the use of TryFrom or TryInto over direct as casts 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

@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 Dec 18, 2025
@rustbot

rustbot commented Dec 18, 2025

Copy link
Copy Markdown
Collaborator

r? @samueltardieu

rustbot has assigned @samueltardieu.
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

@rustbot

This comment has been minimized.

@github-actions

github-actions Bot commented Dec 18, 2025

Copy link
Copy Markdown

Lintcheck changes for 3a6e6b3

Lint Added Removed Changed
clippy::cast_ptr_sized_int 1382 0 0

This comment will be updated if you push new changes

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

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.

View changes since this review

@samueltardieu

Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 19, 2025
@rustbot

rustbot commented Dec 19, 2025

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Dec 19, 2025
@ab22593k

Copy link
Copy Markdown
Author

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)?

@samueltardieu

Copy link
Copy Markdown
Member

The lint still triggers inconsistently. For example:

  • usize can also be a 16-bit integer on some tier 3 platforms such as MSP430
  • A cast from u8 or u16 to usize will always succeed and should not be warned about
  • A cast from usize to u64 or u128 will always succeed and should not be warned about

And indeed, no fix should be provided if it would be incorrect, but the help message may suggest using try_from() and let the user act on it.

@ab22593k
ab22593k force-pushed the master branch 2 times, most recently from fa57af9 to fba9c01 Compare December 26, 2025 08:14
@rustbot

This comment has been minimized.

@ab22593k

Copy link
Copy Markdown
Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Dec 26, 2025

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

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?

View changes since this review

Comment thread clippy_lints/src/casts/mod.rs Outdated
Comment thread tests/ui/cast_ptr_sized_int.rs Outdated
@@ -0,0 +1,89 @@
//@no-rustfix
#![warn(clippy::cast_ptr_sized_int)]
#![allow(clippy::unnecessary_cast)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry; just keep #![warn(clippy::cast_ptr_sized_int)]

Comment thread tests/ui/cast_ptr_sized_int.stderr Outdated
@@ -0,0 +1,229 @@
error: casting `usize` to `u8`: the behavior depends on the target pointer width

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jan 18, 2026
Implement a new lint to detect casts between pointer-sized integers (`usize`, `isize`)
and fixed-size integers that exhibit architecture-dependent behavior.
@rustbot

rustbot commented Feb 1, 2026

Copy link
Copy Markdown
Collaborator

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.

@ab22593k

ab22593k commented Feb 1, 2026

Copy link
Copy Markdown
Author

I think the extend cast_possible_truncation with platform bounds configuration is more is compelling.

clippy.toml Configuration:
target_pointer_widths = [32, 64] # Platforms the user targets
Default: [16, 32, 64] # Warns for all platforms

With proposed Error message with target_pointer_widths = [32, 64] like:
casting u64 to usize may truncate the value on 32-bit platforms

Benefits: No duplication. User control. Leverages existing code

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Feb 1, 2026
@ab22593k

ab22593k commented Feb 1, 2026

Copy link
Copy Markdown
Author

@rustbot ready

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still cases where it will conflict with those three pedantic lints: cast_possible_truncation and cast_possible_wrap, and even cast_sign_loss (e.g., when casting a i64 to usize).

We'll have to find a way to trigger either one or the other lint. If this new lint was a restriction lint, it would make sense to try it first and make it return a boolean if it effectively lints, and execute the other three only when this one didn't post a warning.

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Feb 21, 2026
@rustbot

rustbot commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #15979) 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-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Have a lint against usize-to-u64 casts (or, against *all* integer casts)

3 participants