ref_as_ptr
suggests using ptr::from_ref(r)
where it should instead suggest ptr::from_mut(r)
for r: &mut T
.
#12882
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
Summary
ptr::from_ref
creates a pointer that isn't safe to cast to*mut T
butptr::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 recommendfrom_mut
when it sees an expressionr as *const T
wherer: &mut T
. See rust-lang/rust#56604 (comment), #12791, rust-lang/rust#125897.Reproducer
I tried this code:
I expected to see this happen:
Clippy should recommended
ptr::from_mut(x)
orptr::from_mut(x).cast_const()
.Instead, this happened:
Clippy recommended
ptr::from_ref(x)
.Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: