File tree Expand file tree Collapse file tree 5 files changed +67
-12
lines changed Expand file tree Collapse file tree 5 files changed +67
-12
lines changed Original file line number Diff line number Diff line change 1+ #![allow(unused)]
2+ #![warn(clippy::as_ptr_cast_mut)]
3+ #![allow(clippy::wrong_self_convention, clippy::unnecessary_cast)]
4+
5+ struct MutPtrWrapper(Vec<u8>);
6+ impl MutPtrWrapper {
7+ fn as_ptr(&mut self) -> *const u8 {
8+ self.0.as_mut_ptr() as *const u8
9+ }
10+ }
11+
12+ struct Covariant<T>(*const T);
13+ impl<T> Covariant<T> {
14+ fn as_ptr(self) -> *const T {
15+ self.0
16+ }
17+ }
18+
19+ fn main() {
20+ let mut string = String::new();
21+ let _ = string.as_mut_ptr();
22+ //~^ as_ptr_cast_mut
23+
24+ let _ = string.as_ptr() as *const i8;
25+ let _ = string.as_mut_ptr();
26+ let _ = string.as_mut_ptr() as *mut u8;
27+ let _ = string.as_mut_ptr() as *const u8;
28+
29+ let nn = std::ptr::NonNull::new(4 as *mut u8).unwrap();
30+ let _ = nn.as_ptr() as *mut u8;
31+
32+ let mut wrap = MutPtrWrapper(Vec::new());
33+ let _ = wrap.as_ptr() as *mut u8;
34+
35+ let mut local = 4;
36+ let ref_with_write_perm = Covariant(std::ptr::addr_of_mut!(local) as *const _);
37+ let _ = ref_with_write_perm.as_ptr() as *mut u8;
38+ }
Original file line number Diff line number Diff line change 11#![ allow( unused) ]
22#![ warn( clippy:: as_ptr_cast_mut) ]
33#![ allow( clippy:: wrong_self_convention, clippy:: unnecessary_cast) ]
4- //@no-rustfix: incorrect suggestion
54
65struct MutPtrWrapper ( Vec < u8 > ) ;
76impl MutPtrWrapper {
@@ -22,9 +21,6 @@ fn main() {
2221 let _ = string. as_ptr ( ) as * mut u8 ;
2322 //~^ as_ptr_cast_mut
2423
25- let _: * mut i8 = string. as_ptr ( ) as * mut _ ;
26- //~^ as_ptr_cast_mut
27-
2824 let _ = string. as_ptr ( ) as * const i8 ;
2925 let _ = string. as_mut_ptr ( ) ;
3026 let _ = string. as_mut_ptr ( ) as * mut u8 ;
Original file line number Diff line number Diff line change 11error: casting the result of `as_ptr` to *mut u8
2- --> tests/ui/as_ptr_cast_mut.rs:22 :13
2+ --> tests/ui/as_ptr_cast_mut.rs:21 :13
33 |
44LL | let _ = string.as_ptr() as *mut u8;
55 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()`
66 |
77 = note: `-D clippy::as-ptr-cast-mut` implied by `-D warnings`
88 = help: to override `-D warnings` add `#[allow(clippy::as_ptr_cast_mut)]`
99
10- error: casting the result of `as_ptr` to *mut i8
11- --> tests/ui/as_ptr_cast_mut.rs:25:22
12- |
13- LL | let _: *mut i8 = string.as_ptr() as *mut _;
14- | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()`
15-
16- error: aborting due to 2 previous errors
10+ error: aborting due to 1 previous error
1711
Original file line number Diff line number Diff line change 1+ //@no-rustfix
2+ #![ allow( unused) ]
3+ #![ warn( clippy:: as_ptr_cast_mut) ]
4+
5+ fn main ( ) {
6+ let mut string = String :: new ( ) ;
7+
8+ // the `*mut _` is actually necessary since it does two things at once:
9+ // - changes the mutability (caught by the lint)
10+ // - changes the type
11+ //
12+ // and so replacing this with `as_mut_ptr` removes the second thing,
13+ // resulting in a type mismatch
14+ let _: * mut i8 = string. as_ptr ( ) as * mut _ ;
15+ //~^ as_ptr_cast_mut
16+ }
Original file line number Diff line number Diff line change 1+ error: casting the result of `as_ptr` to *mut i8
2+ --> tests/ui/as_ptr_cast_mut_unfixable.rs:14:22
3+ |
4+ LL | let _: *mut i8 = string.as_ptr() as *mut _;
5+ | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()`
6+ |
7+ = note: `-D clippy::as-ptr-cast-mut` implied by `-D warnings`
8+ = help: to override `-D warnings` add `#[allow(clippy::as_ptr_cast_mut)]`
9+
10+ error: aborting due to 1 previous error
11+
You can’t perform that action at this time.
0 commit comments