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

ref_as_ptr suggests using ptr::from_ref(r) where it should instead suggest ptr::from_mut(r) for r: &mut T. #12882

Open
briansmith opened this issue Jun 3, 2024 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way

Comments

@briansmith
Copy link

Summary

ptr::from_ref creates a pointer that isn't safe to cast to *mut T but ptr::from_mut(x).cast_const() creates a pointer that is safe to cast to *mut T. Unless Clippy is sure that the resultant pointer is never going to be cast to *mut T, it should recommend from_mut when it sees an expression r as *const T where r: &mut T. See rust-lang/rust#56604 (comment), #12791, rust-lang/rust#125897.

Reproducer

I tried this code:

#![deny(clippy::ref_as_ptr)]

fn bar<T>(_x: *const T) {}

fn foo<T>(x: &mut T) {
   bar(x as *const T);
}

I expected to see this happen:

Clippy should recommended ptr::from_mut(x) or ptr::from_mut(x).cast_const().

Instead, this happened:

Clippy recommended ptr::from_ref(x).

Version

Nightly channel
Build using the Nightly version: 1.80.0-nightly
(2024-06-02 032af18af578f4283a29)

Additional Labels

No response

@briansmith briansmith added the C-bug Category: Clippy is not doing the correct thing label Jun 3, 2024
@J-ZhengLi J-ZhengLi added the I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way label Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way
Projects
None yet
Development

No branches or pull requests

2 participants