Skip to content

Commit 30d83ea

Browse files
committed
Fix remaining clippy lints
1 parent e42c706 commit 30d83ea

File tree

12 files changed

+96
-44
lines changed

12 files changed

+96
-44
lines changed

objc/src/declare.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ impl ClassDecl {
159159
/// Adds a method with the given name and implementation to self.
160160
/// Panics if the method wasn't sucessfully added
161161
/// or if the selector and function take different numbers of arguments.
162-
/// Unsafe because the caller must ensure that the types match those that
163-
/// are expected when the method is invoked from Objective-C.
162+
///
163+
/// # Safety
164+
///
165+
/// The caller must ensure that the types match those that are expected
166+
/// when the method is invoked from Objective-C.
164167
pub unsafe fn add_method<F>(&mut self, sel: Sel, func: F)
165168
where
166169
F: MethodImplementation<Callee = Object>,
@@ -182,8 +185,11 @@ impl ClassDecl {
182185
/// Adds a class method with the given name and implementation to self.
183186
/// Panics if the method wasn't sucessfully added
184187
/// or if the selector and function take different numbers of arguments.
185-
/// Unsafe because the caller must ensure that the types match those that
186-
/// are expected when the method is invoked from Objective-C.
188+
///
189+
/// # Safety
190+
///
191+
/// The caller must ensure that the types match those that are expected
192+
/// when the method is invoked from Objective-C.
187193
pub unsafe fn add_class_method<F>(&mut self, sel: Sel, func: F)
188194
where
189195
F: MethodImplementation<Callee = Class>,

objc/src/exception.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ use objc_exception;
33
use crate::rc::StrongPtr;
44
use crate::runtime::Object;
55

6+
// Comment copied from `objc_exception`
7+
8+
/// Tries to execute the given closure and catches an Objective-C exception
9+
/// if one is thrown.
10+
///
11+
/// Returns a `Result` that is either `Ok` if the closure succeeded without an
12+
/// exception being thrown, or an `Err` with a pointer to an exception if one
13+
/// was thrown. The exception is retained and so must be released.
14+
///
15+
/// # Safety
16+
///
17+
/// This encourages unwinding through the closure from Objective-C, which is
18+
/// not safe.
619
pub unsafe fn catch_exception<F, R>(closure: F) -> Result<R, StrongPtr>
720
where
821
F: FnOnce() -> R,

objc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The bindings can be used on Linux or *BSD utilizing the
6363
#![crate_name = "objc"]
6464
#![crate_type = "lib"]
6565
#![warn(missing_docs)]
66+
#![allow(clippy::missing_safety_doc)]
6667

6768
pub use objc_encode::{Encode, Encoding};
6869

objc/src/rc/strong.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ impl StrongPtr {
1212
/// Constructs a `StrongPtr` to a newly created object that already has a
1313
/// +1 retain count. This will not retain the object.
1414
/// When dropped, the object will be released.
15-
/// Unsafe because the caller must ensure the given object pointer is valid.
15+
///
16+
/// # Safety
17+
///
18+
/// The caller must ensure the given object pointer is valid.
1619
pub unsafe fn new(ptr: *mut Object) -> Self {
1720
StrongPtr(ptr)
1821
}
1922

2023
/// Retains the given object and constructs a `StrongPtr` to it.
2124
/// When dropped, the object will be released.
22-
/// Unsafe because the caller must ensure the given object pointer is valid.
25+
///
26+
/// # Safety
27+
///
28+
/// The caller must ensure the given object pointer is valid.
2329
pub unsafe fn retain(ptr: *mut Object) -> Self {
2430
StrongPtr(runtime::objc_retain(ptr))
2531
}

objc/src/rc/weak.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ pub struct WeakPtr(Box<UnsafeCell<*mut Object>>);
1414

1515
impl WeakPtr {
1616
/// Constructs a `WeakPtr` to the given object.
17-
/// Unsafe because the caller must ensure the given object pointer is valid.
17+
///
18+
/// # Safety
19+
///
20+
/// The caller must ensure the given object pointer is valid.
1821
pub unsafe fn new(obj: *mut Object) -> Self {
1922
let ptr = Box::new(UnsafeCell::new(ptr::null_mut()));
2023
runtime::objc_initWeak(ptr.get(), obj);

objc/src/runtime.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ impl Sel {
177177

178178
/// Wraps a raw pointer to a selector into a `Sel` object.
179179
///
180+
/// # Safety
181+
///
182+
/// The pointer must a valid, registered selector.
183+
///
180184
/// This is almost never what you want; use `Sel::register()` instead.
181185
#[inline]
182186
pub unsafe fn from_ptr(ptr: *const c_void) -> Sel {
@@ -478,8 +482,10 @@ impl Object {
478482

479483
/// Returns a reference to the ivar of self with the given name.
480484
/// Panics if self has no ivar with the given name.
481-
/// Unsafe because the caller must ensure that the ivar is actually
482-
/// of type `T`.
485+
///
486+
/// # Safety
487+
///
488+
/// The caller must ensure that the ivar is actually of type `T`.
483489
pub unsafe fn get_ivar<T>(&self, name: &str) -> &T
484490
where
485491
T: Encode,
@@ -503,8 +509,10 @@ impl Object {
503509

504510
/// Returns a mutable reference to the ivar of self with the given name.
505511
/// Panics if self has no ivar with the given name.
506-
/// Unsafe because the caller must ensure that the ivar is actually
507-
/// of type `T`.
512+
///
513+
/// # Safety
514+
///
515+
/// The caller must ensure that the ivar is actually of type `T`.
508516
pub unsafe fn get_mut_ivar<T>(&mut self, name: &str) -> &mut T
509517
where
510518
T: Encode,
@@ -528,8 +536,10 @@ impl Object {
528536

529537
/// Sets the value of the ivar of self with the given name.
530538
/// Panics if self has no ivar with the given name.
531-
/// Unsafe because the caller must ensure that the ivar is actually
532-
/// of type `T`.
539+
///
540+
/// # Safety
541+
///
542+
/// The caller must ensure that the ivar is actually of type `T`.
533543
pub unsafe fn set_ivar<T>(&mut self, name: &str, value: T)
534544
where
535545
T: Encode,

objc_encode/src/parse.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,43 @@ fn rm_enc_prefix<'a>(s: &'a str, enc: &Encoding<'_>) -> Option<&'a str> {
3636
Sel => ":",
3737
Unknown => "?",
3838
BitField(b) => {
39-
let s = rm_prefix(s, "b")?;
39+
let s = s.strip_prefix('b')?;
4040
return rm_int_prefix(s, b);
4141
}
4242
Pointer(t) => {
43-
let s = rm_prefix(s, "^")?;
43+
let s = s.strip_prefix('^')?;
4444
return rm_enc_prefix(s, t);
4545
}
4646
Array(len, item) => {
4747
let mut s = s;
48-
s = rm_prefix(s, "[")?;
48+
s = s.strip_prefix('[')?;
4949
s = rm_int_prefix(s, len)?;
5050
s = rm_enc_prefix(s, item)?;
51-
return rm_prefix(s, "]");
51+
return s.strip_prefix(']');
5252
}
5353
Struct(name, fields) => {
5454
let mut s = s;
55-
s = rm_prefix(s, "{")?;
56-
s = rm_prefix(s, name)?;
57-
s = rm_prefix(s, "=")?;
55+
s = s.strip_prefix('{')?;
56+
s = s.strip_prefix(name)?;
57+
s = s.strip_prefix('=')?;
5858
for field in fields {
5959
s = rm_enc_prefix(s, field)?;
6060
}
61-
return rm_prefix(s, "}");
61+
return s.strip_prefix('}');
6262
}
6363
Union(name, members) => {
6464
let mut s = s;
65-
s = rm_prefix(s, "(")?;
66-
s = rm_prefix(s, name)?;
67-
s = rm_prefix(s, "=")?;
65+
s = s.strip_prefix('(')?;
66+
s = s.strip_prefix(name)?;
67+
s = s.strip_prefix('=')?;
6868
for member in members {
6969
s = rm_enc_prefix(s, member)?;
7070
}
71-
return rm_prefix(s, ")");
71+
return s.strip_prefix(')');
7272
}
7373
};
7474

75-
rm_prefix(s, code)
75+
s.strip_prefix(code)
7676
}
7777

7878
fn chomp_int(s: &str) -> Option<(u32, &str)> {
@@ -88,14 +88,6 @@ fn rm_int_prefix(s: &str, other: u32) -> Option<&str> {
8888
chomp_int(s).and_then(|(n, t)| if other == n { Some(t) } else { None })
8989
}
9090

91-
fn rm_prefix<'a>(s: &'a str, other: &str) -> Option<&'a str> {
92-
if s.starts_with(other) {
93-
Some(&s[other.len()..])
94-
} else {
95-
None
96-
}
97-
}
98-
9991
pub fn eq_enc(s: &str, enc: &Encoding<'_>) -> bool {
10092
// strip qualifiers
10193
let s = s.trim_start_matches(QUALIFIERS);

objc_exception/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ pub enum Exception {}
2222
/// Throws an Objective-C exception.
2323
/// The argument must be a pointer to an Objective-C object.
2424
///
25-
/// Unsafe because this unwinds from Objective-C.
25+
/// # Safety
26+
///
27+
/// This unwinds from Objective-C, and the exception must be caught using an
28+
/// Objective-C exception handler.
2629
pub unsafe fn throw(exception: *mut Exception) -> ! {
2730
RustObjCExceptionThrow(exception as *mut _);
2831
unreachable!();
@@ -64,7 +67,9 @@ where
6467
/// exception being thrown, or an `Err` with a pointer to an exception if one
6568
/// was thrown. The exception is retained and so must be released.
6669
///
67-
/// Unsafe because this encourages unwinding through the closure from
70+
/// # Safety
71+
///
72+
/// This encourages unwinding through the closure from
6873
/// Objective-C, which is not safe.
6974
pub unsafe fn r#try<F, R>(closure: F) -> Result<R, *mut Exception>
7075
where

objc_foundation/src/data.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub trait INSData: INSObject {
1212
unsafe { msg_send![self, length] }
1313
}
1414

15+
fn is_empty(&self) -> bool {
16+
self.len() == 0
17+
}
18+
1519
fn bytes(&self) -> &[u8] {
1620
let ptr: *const c_void = unsafe { msg_send![self, bytes] };
1721
// The bytes pointer may be null for length zero

objc_foundation/src/enumerator.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ impl<'a, T> NSEnumerator<'a, T>
2121
where
2222
T: INSObject,
2323
{
24+
/// TODO
25+
///
26+
/// # Safety
27+
///
28+
/// The object pointer must be a valid `NSEnumerator` with `Owned`
29+
/// ownership.
2430
pub unsafe fn from_ptr(ptr: *mut Object) -> NSEnumerator<'a, T> {
2531
NSEnumerator {
2632
id: Id::from_ptr(ptr),

objc_foundation/src/string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ pub trait INSString: INSObject {
3636
unsafe { msg_send![self, lengthOfBytesUsingEncoding: UTF8_ENCODING] }
3737
}
3838

39+
fn is_empty(&self) -> bool {
40+
self.len() == 0
41+
}
42+
3943
fn as_str(&self) -> &str {
4044
let bytes = unsafe {
4145
let bytes: *const c_char = msg_send![self, UTF8String];

objc_id/src/id.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ where
5454

5555
/// Constructs an `Id` from a pointer to an unretained object and
5656
/// retains it. Panics if the pointer is null.
57-
/// Unsafe because the pointer must be to a valid object and
58-
/// the caller must ensure the ownership is correct.
57+
///
58+
/// # Safety
59+
///
60+
/// The pointer must be to a valid object and the caller must ensure the
61+
/// ownership is correct.
5962
pub unsafe fn from_ptr(ptr: *mut T) -> Id<T, O> {
6063
assert!(
6164
!ptr.is_null(),
@@ -67,8 +70,11 @@ where
6770
/// Constructs an `Id` from a pointer to a retained object; this won't
6871
/// retain the pointer, so the caller must ensure the object has a +1
6972
/// retain count. Panics if the pointer is null.
70-
/// Unsafe because the pointer must be to a valid object and
71-
/// the caller must ensure the ownership is correct.
73+
///
74+
/// # Safety
75+
///
76+
/// The pointer must be to a valid object and the caller must ensure the
77+
/// ownership is correct.
7278
pub unsafe fn from_retained_ptr(ptr: *mut T) -> Id<T, O> {
7379
assert!(
7480
!ptr.is_null(),
@@ -125,10 +131,6 @@ where
125131
fn eq(&self, other: &Id<T, O>) -> bool {
126132
self.deref() == other.deref()
127133
}
128-
129-
fn ne(&self, other: &Id<T, O>) -> bool {
130-
self.deref() != other.deref()
131-
}
132134
}
133135

134136
impl<T, O> Eq for Id<T, O> where T: Eq {}

0 commit comments

Comments
 (0)