diff --git a/block-sys/src/lib.rs b/block-sys/src/lib.rs index be40fae60..b279c8b58 100644 --- a/block-sys/src/lib.rs +++ b/block-sys/src/lib.rs @@ -18,6 +18,7 @@ #![warn(unreachable_pub)] #![deny(unsafe_op_in_unsafe_fn)] #![warn(clippy::cargo)] +#![warn(clippy::ptr_as_ptr)] // Update in Cargo.toml as well. #![doc(html_root_url = "https://docs.rs/block-sys/0.0.4")] diff --git a/block2/src/lib.rs b/block2/src/lib.rs index a13695239..54e963b89 100644 --- a/block2/src/lib.rs +++ b/block2/src/lib.rs @@ -81,6 +81,7 @@ #![warn(unreachable_pub)] #![deny(unsafe_op_in_unsafe_fn)] #![warn(clippy::cargo)] +#![warn(clippy::ptr_as_ptr)] // Update in Cargo.toml as well. #![doc(html_root_url = "https://docs.rs/block2/0.2.0-alpha.4")] diff --git a/objc-sys/src/lib.rs b/objc-sys/src/lib.rs index 8f65d8b2a..dfa90880d 100644 --- a/objc-sys/src/lib.rs +++ b/objc-sys/src/lib.rs @@ -19,6 +19,7 @@ #![warn(unreachable_pub)] #![deny(unsafe_op_in_unsafe_fn)] #![warn(clippy::cargo)] +#![warn(clippy::ptr_as_ptr)] #![allow(clippy::upper_case_acronyms)] #![allow(non_camel_case_types)] #![allow(non_upper_case_globals)] diff --git a/objc2-encode/src/lib.rs b/objc2-encode/src/lib.rs index 553602e44..ac4e170f8 100644 --- a/objc2-encode/src/lib.rs +++ b/objc2-encode/src/lib.rs @@ -91,6 +91,7 @@ #![warn(unreachable_pub)] #![deny(unsafe_op_in_unsafe_fn)] #![warn(clippy::cargo)] +#![warn(clippy::ptr_as_ptr)] // Update in Cargo.toml as well. #![doc(html_root_url = "https://docs.rs/objc2-encode/2.0.0-pre.0")] diff --git a/objc2-foundation/src/array.rs b/objc2-foundation/src/array.rs index e67ed1423..42aaa287a 100644 --- a/objc2-foundation/src/array.rs +++ b/objc2-foundation/src/array.rs @@ -310,7 +310,7 @@ impl NSMutableArray { // Bring back a reference to the closure. // Guaranteed to be unique, we gave `sortUsingFunction` unique is // ownership, and that method only runs one function at a time. - let closure: &mut F = unsafe { (context as *mut F).as_mut().unwrap_unchecked() }; + let closure: &mut F = unsafe { context.cast::().as_mut().unwrap_unchecked() }; NSComparisonResult::from((*closure)(obj1, obj2)) } @@ -322,7 +322,8 @@ impl NSMutableArray { // Grab a type-erased pointer to the closure (a pointer to stack). let mut closure = compare; - let context = &mut closure as *mut F as *mut c_void; + let context: *mut F = &mut closure; + let context: *mut c_void = context.cast(); unsafe { let _: () = msg_send![self, sortUsingFunction: f, context: context]; diff --git a/objc2-foundation/src/attributed_string.rs b/objc2-foundation/src/attributed_string.rs index 1dbb080bd..4f2f2097e 100644 --- a/objc2-foundation/src/attributed_string.rs +++ b/objc2-foundation/src/attributed_string.rs @@ -164,10 +164,7 @@ mod tests { assert!(s2.is_kind_of(NSAttributedString::class())); let s3 = s1.mutable_copy(); - assert_ne!( - Id::as_ptr(&s1), - Id::as_ptr(&s3) as *const NSAttributedString - ); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3).cast()); assert!(s3.is_kind_of(NSMutableAttributedString::class())); } } diff --git a/objc2-foundation/src/lib.rs b/objc2-foundation/src/lib.rs index 6c76763a0..a343e43a5 100644 --- a/objc2-foundation/src/lib.rs +++ b/objc2-foundation/src/lib.rs @@ -33,6 +33,7 @@ #![deny(unsafe_op_in_unsafe_fn)] #![allow(clippy::missing_safety_doc)] // TODO: Remove this #![warn(clippy::cargo)] +#![warn(clippy::ptr_as_ptr)] // Update in Cargo.toml as well. #![doc(html_root_url = "https://docs.rs/objc2-foundation/0.2.0-alpha.5")] diff --git a/objc2-foundation/src/mutable_attributed_string.rs b/objc2-foundation/src/mutable_attributed_string.rs index 70e1f6b7b..fb06dd5dc 100644 --- a/objc2-foundation/src/mutable_attributed_string.rs +++ b/objc2-foundation/src/mutable_attributed_string.rs @@ -97,10 +97,7 @@ mod tests { fn test_copy() { let s1 = NSMutableAttributedString::from_nsstring(&NSString::from_str("abc")); let s2 = s1.copy(); - assert_ne!( - Id::as_ptr(&s1) as *const NSAttributedString, - Id::as_ptr(&s2) - ); + assert_ne!(Id::as_ptr(&s1).cast(), Id::as_ptr(&s2)); assert!(s2.is_kind_of(NSAttributedString::class())); let s3 = s1.mutable_copy(); diff --git a/objc2-foundation/src/mutable_string.rs b/objc2-foundation/src/mutable_string.rs index 8b070743a..28576d8ec 100644 --- a/objc2-foundation/src/mutable_string.rs +++ b/objc2-foundation/src/mutable_string.rs @@ -208,7 +208,7 @@ mod tests { fn test_copy() { let s1 = NSMutableString::from_str("abc"); let s2 = s1.copy(); - assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s2) as *const NSMutableString); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s2).cast()); assert!(s2.is_kind_of(NSString::class())); let s3 = s1.mutable_copy(); diff --git a/objc2-foundation/src/string.rs b/objc2-foundation/src/string.rs index 3b88e4042..64f3cab24 100644 --- a/objc2-foundation/src/string.rs +++ b/objc2-foundation/src/string.rs @@ -359,7 +359,7 @@ mod tests { assert!(s2.is_kind_of(NSString::class())); let s3 = s1.mutable_copy(); - assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *const NSString); + assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3).cast()); assert!(s3.is_kind_of(NSMutableString::class())); } diff --git a/objc2/src/lib.rs b/objc2/src/lib.rs index 201cafe47..236308d69 100644 --- a/objc2/src/lib.rs +++ b/objc2/src/lib.rs @@ -131,6 +131,7 @@ #![warn(unreachable_pub)] #![deny(unsafe_op_in_unsafe_fn)] #![warn(clippy::cargo)] +#![warn(clippy::ptr_as_ptr)] // Update in Cargo.toml as well. #![doc(html_root_url = "https://docs.rs/objc2/0.3.0-beta.0")] diff --git a/objc2/src/message/mod.rs b/objc2/src/message/mod.rs index 68b5edbb2..ac730fbec 100644 --- a/objc2/src/message/mod.rs +++ b/objc2/src/message/mod.rs @@ -222,7 +222,7 @@ pub unsafe trait MessageReceiver: private::Sealed + Sized { unsafe impl MessageReceiver for *const T { #[inline] fn __as_raw_receiver(self) -> *mut Object { - self as *mut T as *mut Object + (self as *mut T).cast() } } @@ -244,7 +244,7 @@ unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a T { #[inline] fn __as_raw_receiver(self) -> *mut Object { let ptr: *const T = self; - ptr as *mut T as *mut Object + (ptr as *mut T).cast() } } @@ -259,7 +259,7 @@ unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut T { unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Id { #[inline] fn __as_raw_receiver(self) -> *mut Object { - Id::as_ptr(self) as *mut T as *mut Object + (Id::as_ptr(self) as *mut T).cast() } } @@ -280,7 +280,7 @@ unsafe impl MessageReceiver for ManuallyDrop< unsafe impl MessageReceiver for *const Class { #[inline] fn __as_raw_receiver(self) -> *mut Object { - self as *mut Class as *mut Object + (self as *mut Class).cast() } } @@ -288,7 +288,7 @@ unsafe impl<'a> MessageReceiver for &'a Class { #[inline] fn __as_raw_receiver(self) -> *mut Object { let ptr: *const Class = self; - ptr as *mut Class as *mut Object + (ptr as *mut Class).cast() } } diff --git a/objc2/src/rc/id.rs b/objc2/src/rc/id.rs index eb7738392..9fb1719d1 100644 --- a/objc2/src/rc/id.rs +++ b/objc2/src/rc/id.rs @@ -263,12 +263,11 @@ impl Id { #[doc(alias = "objc_retain")] #[inline] pub unsafe fn retain(ptr: *mut T) -> Option> { - let ptr = ptr as *mut ffi::objc_object; // SAFETY: The caller upholds that the pointer is valid - let res = unsafe { ffi::objc_retain(ptr) }; + let res: *mut T = unsafe { ffi::objc_retain(ptr.cast()) }.cast(); debug_assert_eq!(res, ptr, "objc_retain did not return the same pointer"); // SAFETY: We just retained the object, so it has +1 retain count - unsafe { Self::new(res as *mut T) } + unsafe { Self::new(res) } } /// Retains a previously autoreleased object pointer. @@ -358,11 +357,9 @@ impl Id { }; } - let ptr = ptr as *mut ffi::objc_object; - // SAFETY: Same as `retain`, `objc_retainAutoreleasedReturnValue` is // just an optimization. - let res = unsafe { ffi::objc_retainAutoreleasedReturnValue(ptr) }; + let res: *mut T = unsafe { ffi::objc_retainAutoreleasedReturnValue(ptr.cast()) }.cast(); // Ideally, we'd be able to specify that the above call should never // be tail-call optimized (become a `jmp` instruction instead of a @@ -390,7 +387,7 @@ impl Id { res, ptr, "objc_retainAutoreleasedReturnValue did not return the same pointer" ); - unsafe { Self::new(res as *mut T) } + unsafe { Self::new(res) } } #[inline] @@ -401,14 +398,14 @@ impl Id { // retained objects it is hard to imagine a case where the inner type // has a method with the same name. - let ptr = ManuallyDrop::new(self).ptr.as_ptr() as *mut ffi::objc_object; + let ptr = ManuallyDrop::new(self).ptr.as_ptr(); // SAFETY: The `ptr` is guaranteed to be valid and have at least one // retain count. // And because of the ManuallyDrop, we don't call the Drop // implementation, so the object won't also be released there. - let res = unsafe { ffi::objc_autorelease(ptr) }; + let res: *mut T = unsafe { ffi::objc_autorelease(ptr.cast()) }.cast(); debug_assert_eq!(res, ptr, "objc_autorelease did not return the same pointer"); - res as *mut T + res } // TODO: objc_autoreleaseReturnValue @@ -531,7 +528,7 @@ impl Drop for Id { // SAFETY: The `ptr` is guaranteed to be valid and have at least one // retain count - unsafe { ffi::objc_release(self.ptr.as_ptr() as *mut _) }; + unsafe { ffi::objc_release(self.ptr.as_ptr().cast()) }; } } diff --git a/objc2/src/rc/weak_id.rs b/objc2/src/rc/weak_id.rs index 5730c5b2c..05a5444ae 100644 --- a/objc2/src/rc/weak_id.rs +++ b/objc2/src/rc/weak_id.rs @@ -52,7 +52,7 @@ impl WeakId { unsafe fn new_inner(obj: *const T) -> Self { let inner = Box::new(UnsafeCell::new(ptr::null_mut())); // SAFETY: `ptr` will never move, and the caller verifies `obj` - let _ = unsafe { ffi::objc_initWeak(inner.get(), obj as *mut T as *mut ffi::objc_object) }; + let _ = unsafe { ffi::objc_initWeak(inner.get(), (obj as *mut T).cast()) }; Self { inner, item: PhantomData, @@ -70,7 +70,7 @@ impl WeakId { #[inline] pub fn load(&self) -> Option> { let ptr = self.inner.get(); - let obj = unsafe { ffi::objc_loadWeakRetained(ptr) } as *mut T; + let obj = unsafe { ffi::objc_loadWeakRetained(ptr) }.cast(); unsafe { Id::new(obj) } }