Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest .cast() instead of as for pointer casts #8017

Open
scottmcm opened this issue Nov 22, 2021 · 3 comments
Open

Suggest .cast() instead of as for pointer casts #8017

scottmcm opened this issue Nov 22, 2021 · 3 comments
Labels
A-lint Area: New lints

Comments

@scottmcm
Copy link
Member

What it does

Suggests using the more-scoped cast methods
https://doc.rust-lang.org/std/primitive.pointer.html#method.cast
https://doc.rust-lang.org/std/primitive.pointer.html#method.cast-1
for pointer casting instead of the more-general as.

Categories (optional)

  • Kind: style

(One could argue that this is more pedantic, as that's what https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless is. But I think that pointer casts are generally around unsafe code, so the pedantry is more generally appropriate.)

This helps avoid mistakes where the as cast is accidentally converting between *const _ and *mut _, for example. For pointers it's often tempting to slap on as _ to make something work, but that's a very broad conversion.

(This is not applicable for *const T <-> *mut T casts, which this lint should not warn about.)

Drawbacks

None.

Example

From https://users.rust-lang.org/t/converting-between-unsized-transparent-types-safely-in-place/67680/19?u=scottmcm

impl<T> From<OrderedVecSet<T>> for Rc<OrderedVecSetUnsized<T>>
where
    T: Ord,
{
    fn from(set: OrderedVecSet<T>) -> Self {
        let rc: Rc<[T]> = set.0.into();
        unsafe { Rc::from_raw(Rc::into_raw(rc) as _) }
    }
}

Could be written as:

impl<T> From<OrderedVecSet<T>> for Rc<OrderedVecSetUnsized<T>>
where
    T: Ord,
{
    fn from(set: OrderedVecSet<T>) -> Self {
        let rc: Rc<[T]> = set.0.into();
        unsafe { Rc::from_raw(Rc::into_raw(rc).cast()) }
    }
}
@scottmcm scottmcm added the A-lint Area: New lints label Nov 22, 2021
taiki-e added a commit to rust-lang/futures-rs that referenced this issue Jan 12, 2022
taiki-e added a commit to rust-lang/futures-rs that referenced this issue Jan 12, 2022
taiki-e added a commit to rust-lang/futures-rs that referenced this issue Jan 12, 2022
taiki-e added a commit to rust-lang/futures-rs that referenced this issue Feb 6, 2022
taiki-e added a commit to rust-lang/futures-rs that referenced this issue Feb 6, 2022
taiki-e added a commit to crossbeam-rs/crossbeam that referenced this issue Jul 22, 2022
taiki-e added a commit to crossbeam-rs/crossbeam that referenced this issue Jul 22, 2022
@taiki-e
Copy link
Member

taiki-e commented Jul 23, 2022

Does this seem implemented as clippy::ptr_as_ptr?

@SUPERCILEX
Copy link

Does ptr_as_ptr support the new cast_{mut,ref}?

@Centri3
Copy link
Member

Centri3 commented May 31, 2023

Does ptr_as_ptr support the new cast_{mut,ref}?

This is now covered by ptr_cast_constness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

4 participants