Skip to content

Commit

Permalink
Add #![warn(clippy::ptr_as_ptr)] lint
Browse files Browse the repository at this point in the history
Follow-up from b8f9b88
  • Loading branch information
madsmtm committed Jun 14, 2022
1 parent 689aafd commit 7948b63
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 30 deletions.
1 change: 1 addition & 0 deletions block-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

Expand Down
1 change: 1 addition & 0 deletions block2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

Expand Down
1 change: 1 addition & 0 deletions objc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions objc2-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

Expand Down
5 changes: 3 additions & 2 deletions objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
// 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::<F>().as_mut().unwrap_unchecked() };

NSComparisonResult::from((*closure)(obj1, obj2))
}
Expand All @@ -322,7 +322,8 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {

// 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];
Expand Down
5 changes: 1 addition & 4 deletions objc2-foundation/src/attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
1 change: 1 addition & 0 deletions objc2-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

Expand Down
5 changes: 1 addition & 4 deletions objc2-foundation/src/mutable_attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion objc2-foundation/src/mutable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion objc2-foundation/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
1 change: 1 addition & 0 deletions objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

Expand Down
10 changes: 5 additions & 5 deletions objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub unsafe trait MessageReceiver: private::Sealed + Sized {
unsafe impl<T: Message + ?Sized> MessageReceiver for *const T {
#[inline]
fn __as_raw_receiver(self) -> *mut Object {
self as *mut T as *mut Object
(self as *mut T).cast()
}
}

Expand All @@ -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()
}
}

Expand All @@ -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<T, O> {
#[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()
}
}

Expand All @@ -280,15 +280,15 @@ unsafe impl<T: Message + ?Sized, O: Ownership> 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()
}
}

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()
}
}

Expand Down
19 changes: 8 additions & 11 deletions objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,11 @@ impl<T: Message, O: Ownership> Id<T, O> {
#[doc(alias = "objc_retain")]
#[inline]
pub unsafe fn retain(ptr: *mut T) -> Option<Id<T, O>> {
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.
Expand Down Expand Up @@ -358,11 +357,9 @@ impl<T: Message, O: Ownership> Id<T, O> {
};
}

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
Expand Down Expand Up @@ -390,7 +387,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
res, ptr,
"objc_retainAutoreleasedReturnValue did not return the same pointer"
);
unsafe { Self::new(res as *mut T) }
unsafe { Self::new(res) }
}

#[inline]
Expand All @@ -401,14 +398,14 @@ impl<T: Message, O: Ownership> Id<T, O> {
// 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
Expand Down Expand Up @@ -531,7 +528,7 @@ impl<T: ?Sized, O: Ownership> Drop for Id<T, O> {

// 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()) };
}
}

Expand Down
4 changes: 2 additions & 2 deletions objc2/src/rc/weak_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<T: Message> WeakId<T> {
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,
Expand All @@ -70,7 +70,7 @@ impl<T: Message> WeakId<T> {
#[inline]
pub fn load(&self) -> Option<Id<T, Shared>> {
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) }
}

Expand Down

0 comments on commit 7948b63

Please sign in to comment.