Skip to content

Extend unnecessary_cast lint to detect unnecessary ptr::cast() calls#16680

Open
lapla-cogito wants to merge 1 commit intorust-lang:masterfrom
lapla-cogito:unnecesarycast_method
Open

Extend unnecessary_cast lint to detect unnecessary ptr::cast() calls#16680
lapla-cogito wants to merge 1 commit intorust-lang:masterfrom
lapla-cogito:unnecesarycast_method

Conversation

@lapla-cogito
Copy link
Copy Markdown
Contributor

@lapla-cogito lapla-cogito commented Mar 7, 2026

close #15624

Casts to type aliases, cfg-dependent types, and explicit wildcards (ptr.cast::<_>()) are intentionally excluded to stay consistent with the existing as-cast behavior.

changelog: [unnecessary_cast]: also lint unnecessary ptr::cast() calls

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 7, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 7, 2026

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

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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 7, 2026

Lintcheck changes for 854aa39

Lint Added Removed Changed
clippy::unnecessary_cast 503 0 1

This comment will be updated if you push new changes

@lapla-cogito lapla-cogito force-pushed the unnecesarycast_method branch 2 times, most recently from 1ea877b to 32235e9 Compare March 7, 2026 13:20
@rustbot

This comment has been minimized.

@lapla-cogito lapla-cogito force-pushed the unnecesarycast_method branch from 32235e9 to 632e3e5 Compare March 7, 2026 13:26
@rustbot

This comment has been minimized.

//@[edition2018] check-pass
#![warn(clippy::manual_c_str_literals)]
#![allow(clippy::no_effect)]
#![allow(clippy::no_effect, clippy::unnecessary_cast)]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Initially, I considered using #[expect(clippy::unnecessary_cast)] for let _: *const _ = b"foo\0".as_ptr().cast::<i8>();, but this appears to only occur in edition 2021 (where edition 2018 triggers an unfulfilled lint). Therefore, I'm using #[allow] instead.

@lapla-cogito lapla-cogito force-pushed the unnecesarycast_method branch from 632e3e5 to bc9f5a3 Compare March 8, 2026 09:37
&& let ty::RawPtr(to_pointee_ty, to_mutbl) = result_ty.kind()
&& from_mutbl == to_mutbl
&& from_pointee_ty == to_pointee_ty
&& !from_pointee_ty.has_erased_regions()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added this based on lintcheck results from the first version. The first tokio-util case intentionally modified the lifetimes, which I think is a FP.

@lapla-cogito lapla-cogito force-pushed the unnecesarycast_method branch from bc9f5a3 to 430ccc7 Compare March 8, 2026 09:42
@rustbot

This comment has been minimized.

@lapla-cogito lapla-cogito force-pushed the unnecesarycast_method branch from 430ccc7 to 54334d8 Compare March 31, 2026 02:59
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@lapla-cogito lapla-cogito force-pushed the unnecesarycast_method branch from 54334d8 to 854aa39 Compare April 8, 2026 08:14
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 8, 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.

@lapla-cogito
Copy link
Copy Markdown
Contributor Author

friendly ping @samueltardieu

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

How would that work with types from std::ffi, for example when casting a std::ffi::c_long to i64? On 32 bit systems the cast may not be superfluous.

View changes since this review

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

Labels

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.

unncessary_cast is not triggered by ptr::cast

3 participants