Skip to content

ptr_cast_constness: check for implicit mut to const pointer coercion - #15747

Open
zihan0822 wants to merge 1 commit into
rust-lang:masterfrom
zihan0822:check-implicit-ptr-cast-constness
Open

ptr_cast_constness: check for implicit mut to const pointer coercion#15747
zihan0822 wants to merge 1 commit into
rust-lang:masterfrom
zihan0822:check-implicit-ptr-cast-constness

Conversation

@zihan0822

@zihan0822 zihan0822 commented Sep 23, 2025

Copy link
Copy Markdown
Contributor

Fixes #13667

changelog: [ptr_cast_constness]: check for implicit mut to const pointer coercion

changelog: [`ptr_cast_constness`]: check for implicit mut to const
pointer coercion

Signed-off-by: Zihan <zihanli0822@gmail.com>
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 23, 2025
@rustbot

rustbot commented Sep 23, 2025

Copy link
Copy Markdown
Collaborator

r? @y21

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

@github-actions

Copy link
Copy Markdown

Lintcheck changes for cec69cd

Lint Added Removed Changed
clippy::ptr_cast_constness 18 0 1

This comment will be updated if you push new changes

@y21 y21 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.

In my opinion, if we start linting this pattern, it should be a new lint.

ptr_cast_constness is specifically about as casts. The motivation is that it's easy to unintentionally also change the pointee type alongside with constness with normal as casts, so like accidentally going from *mut i32 to *const i64.

This is not true for implicit coercions like what is proposed. That can only ever go from *mut T to *const T without changing the pointee and seems entirely harmless to me. Looking at the lintcheck results, I'm also not too convinced that it adds much value.

It should then likely also be a restriction lint given that there's IMO not really any advantage, just forces the user to be more explicit.
I'll also open a thread on zulip soon to gather some other opinions.

View changes since this review

@rustbot

rustbot commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17369) 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

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.

Suggest cast_const() for *mut to *const coercions (similar to ptr_cast_constness)

3 participants