Skip to content

Commit 4603799

Browse files
committed
fix clippy warnings for macos
- Warnings related to unsafe code has not been touched. - The number of warnings has been reduced from 381 to 7 errors (denied lints). Fixes: - replaced unmaintained objc crate with fork SSheldon/rust-objc#125. - Ideally it would be replaced with https://github.com/madsmtm/objc2 but that'd be too much work. - simplified zero pointers and redundant casts and clones. - fixed slow zero-filling vector initialization. - simplified unneeded explicit returns. - simplified unnecessary lifetimes. - ...among others.
1 parent 0183ccd commit 4603799

File tree

6 files changed

+35
-41
lines changed

6 files changed

+35
-41
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ libc = "0.2"
4343
ndk-sys = "0.2"
4444

4545
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
46-
objc = "0.2"
46+
objc = { package = "objc-rs", version = "0.2" }
4747

4848
[dev-dependencies]
4949
glam = { version = "0.24", features = ["scalar-math"] }

src/graphics/metal.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ pub struct MetalContext {
281281
current_ub_offset: u64,
282282
}
283283

284+
impl Default for MetalContext {
285+
fn default() -> Self {
286+
Self::new()
287+
}
288+
}
289+
284290
impl MetalContext {
285291
pub fn new() -> MetalContext {
286292
unsafe {
@@ -589,6 +595,7 @@ impl RenderingBackend for MetalContext {
589595
BufferSource::Slice(data) => data.size,
590596
BufferSource::Empty { size, .. } => *size,
591597
};
598+
#[allow(clippy::needless_range_loop)]
592599
for i in 0..BUFFERS_IN_ROTATION {
593600
let buffer: ObjcId = if let BufferSource::Slice(data) = &data {
594601
debug_assert!(data.is_slice);
@@ -619,7 +626,7 @@ impl RenderingBackend for MetalContext {
619626
}
620627
let buffer = Buffer {
621628
raw,
622-
size: size as usize,
629+
size,
623630
value: 0,
624631
next_value: 0,
625632
};
@@ -804,8 +811,8 @@ impl RenderingBackend for MetalContext {
804811
let raw_texture = self.textures.get(texture).texture;
805812
let region = MTLRegion {
806813
origin: MTLOrigin {
807-
x: 0 as u64,
808-
y: 0 as u64,
814+
x: 0_u64,
815+
y: 0_u64,
809816
z: 0,
810817
},
811818
size: MTLSize {
@@ -1155,11 +1162,7 @@ impl RenderingBackend for MetalContext {
11551162
None => {
11561163
let (screen_width, screen_height) = crate::window::screen_size();
11571164
(
1158-
{
1159-
let a = msg_send_![self.view, currentRenderPassDescriptor];
1160-
//msg_send_![a, retain];
1161-
a
1162-
},
1165+
msg_send_![self.view, currentRenderPassDescriptor],
11631166
screen_width as f64,
11641167
screen_height as f64,
11651168
)

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![doc = include_str!("../README.md")]
22
#![allow(
33
clippy::collapsible_if,
4+
clippy::collapsible_else_if,
45
clippy::unused_unit,
56
clippy::identity_op,
67
clippy::missing_safety_doc

src/native/apple/apple_util.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,30 @@ pub unsafe fn cfstring_ref_to_string(cfstring: CFStringRef) -> String {
6767
kCFStringEncodingUTF8,
6868
0,
6969
false,
70-
0 as *mut u8,
70+
std::ptr::null_mut::<u8>(),
7171
0,
7272
&mut num_bytes,
7373
);
7474
if converted == 0 || num_bytes == 0 {
7575
return String::new();
7676
}
77-
let mut buffer = Vec::new();
78-
buffer.resize(num_bytes as usize, 0u8);
77+
let mut buffer = vec![0u8; num_bytes as usize];
7978
CFStringGetBytes(
8079
cfstring,
8180
range,
8281
kCFStringEncodingUTF8,
8382
0,
8483
false,
85-
buffer.as_mut_ptr() as *mut u8,
84+
buffer.as_mut_ptr(),
8685
num_bytes,
87-
0 as *mut u64,
86+
std::ptr::null_mut::<u64>(),
8887
);
89-
if let Ok(val) = String::from_utf8(buffer) {
90-
val
91-
} else {
92-
String::new()
93-
}
88+
String::from_utf8(buffer).unwrap_or_default()
9489
}
9590

9691
pub fn load_webkit_cursor(cursor_name_str: &str) -> ObjcId {
9792
unsafe {
98-
static CURSOR_ROOT: &'static str = "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/Resources/cursors";
93+
static CURSOR_ROOT: &str = "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/Resources/cursors";
9994
let cursor_root = str_to_nsstring(CURSOR_ROOT);
10095
let cursor_name = str_to_nsstring(cursor_name_str);
10196
let cursor_pdf = str_to_nsstring("cursor.pdf");
@@ -137,7 +132,7 @@ pub fn get_event_char(event: ObjcId) -> Option<char> {
137132
}
138133
let chars = nsstring_to_string(characters);
139134

140-
if chars.len() == 0 {
135+
if chars.is_empty() {
141136
return None;
142137
}
143138
Some(chars.chars().next().unwrap())
@@ -402,7 +397,7 @@ pub fn keycode_to_menu_key(keycode: KeyCode, shift: bool) -> &'static str {
402397
}
403398
}
404399

405-
pub unsafe fn superclass<'a>(this: &'a Object) -> &'a Class {
400+
pub unsafe fn superclass(this: &Object) -> &Class {
406401
let superclass: ObjcId = msg_send![this, superclass];
407402
&*(superclass as *const _)
408403
}

src/native/apple/frameworks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ pub enum NSDragOperation {
853853

854854
unsafe impl Encode for NSDragOperation {
855855
fn encode() -> Encoding {
856-
let encoding = format!("Q");
856+
let encoding = "Q".to_string();
857857
unsafe { Encoding::from_str(&encoding) }
858858
}
859859
}

src/native/macos.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ pub fn define_app_delegate() -> *const Class {
256256
yes1 as extern "C" fn(&Object, Sel, ObjcId) -> BOOL,
257257
);
258258
}
259-
260-
return decl.register();
259+
decl.register()
261260
}
262261

263262
pub fn define_cocoa_window_delegate() -> *const Class {
@@ -284,9 +283,9 @@ pub fn define_cocoa_window_delegate() -> *const Class {
284283
}
285284
}
286285
if native_display().lock().unwrap().quit_ordered {
287-
return YES;
286+
YES
288287
} else {
289-
return NO;
288+
NO
290289
}
291290
}
292291

@@ -328,11 +327,7 @@ pub fn define_cocoa_window_delegate() -> *const Class {
328327
let responds: bool = msg_send![payload.window, respondsToSelector:sel!(occlusionState)];
329328
if responds {
330329
let state: u64 = msg_send![payload.window, occlusionState];
331-
if state & NSWindowOcclusionStateVisible != 0 {
332-
payload.occluded = false;
333-
} else {
334-
payload.occluded = true;
335-
}
330+
payload.occluded = state & NSWindowOcclusionStateVisible == 0;
336331
}
337332
}
338333
}
@@ -375,7 +370,7 @@ pub fn define_cocoa_window_delegate() -> *const Class {
375370
// Store internal state as user data
376371
decl.add_ivar::<*mut c_void>("display_ptr");
377372

378-
return decl.register();
373+
decl.register()
379374
}
380375

381376
unsafe fn get_proc_address(name: *const u8) -> Option<unsafe extern "C" fn()> {
@@ -485,7 +480,7 @@ unsafe fn view_base_decl(decl: &mut ClassDecl) {
485480
let cursor_id = *payload
486481
.cursors
487482
.entry(current_cursor)
488-
.or_insert_with(|| load_mouse_cursor(current_cursor.clone()));
483+
.or_insert_with(|| load_mouse_cursor(current_cursor));
489484
assert!(!cursor_id.is_null());
490485
cursor_id
491486
};
@@ -737,10 +732,9 @@ pub fn define_opengl_view_class() -> *const Class {
737732

738733
view_base_decl(&mut decl);
739734
}
740-
741735
decl.add_ivar::<*mut c_void>("display_ptr");
742736

743-
return decl.register();
737+
decl.register()
744738
}
745739

746740
pub fn define_metal_view_class() -> *const Class {
@@ -769,7 +763,7 @@ pub fn define_metal_view_class() -> *const Class {
769763
view_base_decl(&mut decl);
770764
}
771765

772-
return decl.register();
766+
decl.register()
773767
}
774768

775769
fn get_window_payload(this: &Object) -> &mut MacosDisplay {
@@ -797,6 +791,7 @@ unsafe fn create_metal_view(_: &mut MacosDisplay, sample_count: i32, _: bool) ->
797791
view
798792
}
799793

794+
#[allow(clippy::vec_init_then_push)]
800795
unsafe fn create_opengl_view(
801796
display: &mut MacosDisplay,
802797
sample_count: i32,
@@ -865,12 +860,12 @@ impl crate::native::Clipboard for MacosClipboard {
865860
}
866861

867862
unsafe extern "C" fn release_data(info: *mut c_void, _: *const c_void, _: usize) {
868-
drop(Box::from_raw(info));
863+
drop(Box::from_raw(info as *mut &[u8]));
869864
}
870865

871866
unsafe fn set_icon(ns_app: ObjcId, icon: &Icon) {
872-
let width = 64 as usize;
873-
let height = 64 as usize;
867+
let width = 64_usize;
868+
let height = 64_usize;
874869
let colors = &icon.big[..];
875870
let rgb = CGColorSpaceCreateDeviceRGB();
876871
let bits_per_component: usize = 8; // number of bits in UInt8
@@ -1069,7 +1064,7 @@ where
10691064
let window: ObjcId = msg_send![
10701065
window,
10711066
initWithContentRect: window_frame
1072-
styleMask: window_masks as u64
1067+
styleMask: window_masks
10731068
backing: NSBackingStoreType::NSBackingStoreBuffered as u64
10741069
defer: NO
10751070
];

0 commit comments

Comments
 (0)