as_ptr_cast: move the unfixable case into a separate file (#15475)

allows creating `.fixed` for the main file

changelog: none
This commit is contained in:
Jason Newcomb 2025-08-13 17:11:14 +00:00 committed by GitHub
commit 355e4bacf5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 67 additions and 12 deletions

View file

@ -0,0 +1,38 @@
#![allow(unused)]
#![warn(clippy::as_ptr_cast_mut)]
#![allow(clippy::wrong_self_convention, clippy::unnecessary_cast)]
struct MutPtrWrapper(Vec<u8>);
impl MutPtrWrapper {
fn as_ptr(&mut self) -> *const u8 {
self.0.as_mut_ptr() as *const u8
}
}
struct Covariant<T>(*const T);
impl<T> Covariant<T> {
fn as_ptr(self) -> *const T {
self.0
}
}
fn main() {
let mut string = String::new();
let _ = string.as_mut_ptr();
//~^ as_ptr_cast_mut
let _ = string.as_ptr() as *const i8;
let _ = string.as_mut_ptr();
let _ = string.as_mut_ptr() as *mut u8;
let _ = string.as_mut_ptr() as *const u8;
let nn = std::ptr::NonNull::new(4 as *mut u8).unwrap();
let _ = nn.as_ptr() as *mut u8;
let mut wrap = MutPtrWrapper(Vec::new());
let _ = wrap.as_ptr() as *mut u8;
let mut local = 4;
let ref_with_write_perm = Covariant(std::ptr::addr_of_mut!(local) as *const _);
let _ = ref_with_write_perm.as_ptr() as *mut u8;
}

View file

@ -1,7 +1,6 @@
#![allow(unused)]
#![warn(clippy::as_ptr_cast_mut)]
#![allow(clippy::wrong_self_convention, clippy::unnecessary_cast)]
//@no-rustfix: incorrect suggestion
struct MutPtrWrapper(Vec<u8>);
impl MutPtrWrapper {
@ -22,9 +21,6 @@ fn main() {
let _ = string.as_ptr() as *mut u8;
//~^ as_ptr_cast_mut
let _: *mut i8 = string.as_ptr() as *mut _;
//~^ as_ptr_cast_mut
let _ = string.as_ptr() as *const i8;
let _ = string.as_mut_ptr();
let _ = string.as_mut_ptr() as *mut u8;

View file

@ -1,5 +1,5 @@
error: casting the result of `as_ptr` to *mut u8
--> tests/ui/as_ptr_cast_mut.rs:22:13
--> tests/ui/as_ptr_cast_mut.rs:21:13
|
LL | let _ = string.as_ptr() as *mut u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()`
@ -7,11 +7,5 @@ LL | let _ = string.as_ptr() as *mut u8;
= note: `-D clippy::as-ptr-cast-mut` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::as_ptr_cast_mut)]`
error: casting the result of `as_ptr` to *mut i8
--> tests/ui/as_ptr_cast_mut.rs:25:22
|
LL | let _: *mut i8 = string.as_ptr() as *mut _;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()`
error: aborting due to 2 previous errors
error: aborting due to 1 previous error

View file

@ -0,0 +1,16 @@
//@no-rustfix
#![allow(unused)]
#![warn(clippy::as_ptr_cast_mut)]
fn main() {
let mut string = String::new();
// the `*mut _` is actually necessary since it does two things at once:
// - changes the mutability (caught by the lint)
// - changes the type
//
// and so replacing this with `as_mut_ptr` removes the second thing,
// resulting in a type mismatch
let _: *mut i8 = string.as_ptr() as *mut _;
//~^ as_ptr_cast_mut
}

View file

@ -0,0 +1,11 @@
error: casting the result of `as_ptr` to *mut i8
--> tests/ui/as_ptr_cast_mut_unfixable.rs:14:22
|
LL | let _: *mut i8 = string.as_ptr() as *mut _;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()`
|
= note: `-D clippy::as-ptr-cast-mut` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::as_ptr_cast_mut)]`
error: aborting due to 1 previous error