Skip to content

clippy: fix ptr_as_ptr lint - #907

Open
xtqqczze wants to merge 1 commit into
microsoft:mainfrom
xtqqczze:lint/ptr_as_ptr
Open

clippy: fix ptr_as_ptr lint#907
xtqqczze wants to merge 1 commit into
microsoft:mainfrom
xtqqczze:lint/ptr_as_ptr

Conversation

@xtqqczze

Copy link
Copy Markdown
Contributor

@lhecker

lhecker commented Jul 31, 2026

Copy link
Copy Markdown
Member

I'm not super sure yet if I want to accept fixes for pedantic warnings (this one is quite larger after all). Is there a benefit to doing so?

@xtqqczze

xtqqczze commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

The benefit of this lint is that pointer::cast cannot accidentally change the pointer’s mutability, which forces you to be explicit when casting mutability.

I also think .cast() is a bit more readable from a style perspective since it follows the usual postfix method-call pattern, making chains of pointer operations easier to read. It also avoids some extra parentheses that are often needed with as casts.

Comment thread crates/edit/src/icu.rs
let ut = unsafe { &mut *ptr };
ut.p_funcs = &FUNCS;
ut.context = tb as *const TextBuffer as *mut _;
ut.context = ptr::from_ref(tb).cast_mut().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.

@lhecker e.g. this is more readable

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.

"from_ref cast mut cast" is not as readable to me as "as text buffer as whatever", personally. It'd be great to hear the opinion of others but I doubt anyone just randomly shows up haha. (I mean people that aren't too opinionated on Rust-correctness, otherwise that'd be unfair.)

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.

Being explicit when casting mutability makes the intent clearer and, in my opinion, improves readability.

In the case of .cast_mut(), this looks like a potentially suspicious operation, so I looked into this instance further. It seems the context field may be incorrectly typed, as the corresponding C definition uses const void *, while the Rust definition uses *mut c_void:

pub context: *mut c_void,

https://github.com/unicode-org/icu/blob/bbb41e0bc320e2d5515d337c0cd9e9703a1942ee/icu4c/source/common/unicode/utext.h#L1443

@xtqqczze xtqqczze Jul 31, 2026

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants