Skip to content

Commit

Permalink
fix clippy warnings for macos
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
joseluis committed Feb 7, 2025
1 parent 0183ccd commit 4603799
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ libc = "0.2"
ndk-sys = "0.2"

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

[dev-dependencies]
glam = { version = "0.24", features = ["scalar-math"] }
Expand Down
19 changes: 11 additions & 8 deletions src/graphics/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ pub struct MetalContext {
current_ub_offset: u64,
}

impl Default for MetalContext {
fn default() -> Self {
Self::new()
}
}

impl MetalContext {
pub fn new() -> MetalContext {
unsafe {
Expand Down Expand Up @@ -589,6 +595,7 @@ impl RenderingBackend for MetalContext {
BufferSource::Slice(data) => data.size,
BufferSource::Empty { size, .. } => *size,
};
#[allow(clippy::needless_range_loop)]
for i in 0..BUFFERS_IN_ROTATION {
let buffer: ObjcId = if let BufferSource::Slice(data) = &data {
debug_assert!(data.is_slice);
Expand Down Expand Up @@ -619,7 +626,7 @@ impl RenderingBackend for MetalContext {
}
let buffer = Buffer {
raw,
size: size as usize,
size,
value: 0,
next_value: 0,
};
Expand Down Expand Up @@ -804,8 +811,8 @@ impl RenderingBackend for MetalContext {
let raw_texture = self.textures.get(texture).texture;
let region = MTLRegion {
origin: MTLOrigin {
x: 0 as u64,
y: 0 as u64,
x: 0_u64,
y: 0_u64,
z: 0,
},
size: MTLSize {
Expand Down Expand Up @@ -1155,11 +1162,7 @@ impl RenderingBackend for MetalContext {
None => {
let (screen_width, screen_height) = crate::window::screen_size();
(
{
let a = msg_send_![self.view, currentRenderPassDescriptor];
//msg_send_![a, retain];
a
},
msg_send_![self.view, currentRenderPassDescriptor],
screen_width as f64,
screen_height as f64,
)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![doc = include_str!("../README.md")]
#![allow(
clippy::collapsible_if,
clippy::collapsible_else_if,
clippy::unused_unit,
clippy::identity_op,
clippy::missing_safety_doc
Expand Down
21 changes: 8 additions & 13 deletions src/native/apple/apple_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,30 @@ pub unsafe fn cfstring_ref_to_string(cfstring: CFStringRef) -> String {
kCFStringEncodingUTF8,
0,
false,
0 as *mut u8,
std::ptr::null_mut::<u8>(),
0,
&mut num_bytes,
);
if converted == 0 || num_bytes == 0 {
return String::new();
}
let mut buffer = Vec::new();
buffer.resize(num_bytes as usize, 0u8);
let mut buffer = vec![0u8; num_bytes as usize];
CFStringGetBytes(
cfstring,
range,
kCFStringEncodingUTF8,
0,
false,
buffer.as_mut_ptr() as *mut u8,
buffer.as_mut_ptr(),
num_bytes,
0 as *mut u64,
std::ptr::null_mut::<u64>(),
);
if let Ok(val) = String::from_utf8(buffer) {
val
} else {
String::new()
}
String::from_utf8(buffer).unwrap_or_default()
}

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

if chars.len() == 0 {
if chars.is_empty() {
return None;
}
Some(chars.chars().next().unwrap())
Expand Down Expand Up @@ -402,7 +397,7 @@ pub fn keycode_to_menu_key(keycode: KeyCode, shift: bool) -> &'static str {
}
}

pub unsafe fn superclass<'a>(this: &'a Object) -> &'a Class {
pub unsafe fn superclass(this: &Object) -> &Class {
let superclass: ObjcId = msg_send![this, superclass];
&*(superclass as *const _)
}
Expand Down
2 changes: 1 addition & 1 deletion src/native/apple/frameworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ pub enum NSDragOperation {

unsafe impl Encode for NSDragOperation {
fn encode() -> Encoding {
let encoding = format!("Q");
let encoding = "Q".to_string();
unsafe { Encoding::from_str(&encoding) }
}
}
Expand Down
31 changes: 13 additions & 18 deletions src/native/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ pub fn define_app_delegate() -> *const Class {
yes1 as extern "C" fn(&Object, Sel, ObjcId) -> BOOL,
);
}

return decl.register();
decl.register()
}

pub fn define_cocoa_window_delegate() -> *const Class {
Expand All @@ -284,9 +283,9 @@ pub fn define_cocoa_window_delegate() -> *const Class {
}
}
if native_display().lock().unwrap().quit_ordered {
return YES;
YES
} else {
return NO;
NO
}
}

Expand Down Expand Up @@ -328,11 +327,7 @@ pub fn define_cocoa_window_delegate() -> *const Class {
let responds: bool = msg_send![payload.window, respondsToSelector:sel!(occlusionState)];
if responds {
let state: u64 = msg_send![payload.window, occlusionState];
if state & NSWindowOcclusionStateVisible != 0 {
payload.occluded = false;
} else {
payload.occluded = true;
}
payload.occluded = state & NSWindowOcclusionStateVisible == 0;
}
}
}
Expand Down Expand Up @@ -375,7 +370,7 @@ pub fn define_cocoa_window_delegate() -> *const Class {
// Store internal state as user data
decl.add_ivar::<*mut c_void>("display_ptr");

return decl.register();
decl.register()
}

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

view_base_decl(&mut decl);
}

decl.add_ivar::<*mut c_void>("display_ptr");

return decl.register();
decl.register()
}

pub fn define_metal_view_class() -> *const Class {
Expand Down Expand Up @@ -769,7 +763,7 @@ pub fn define_metal_view_class() -> *const Class {
view_base_decl(&mut decl);
}

return decl.register();
decl.register()
}

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

#[allow(clippy::vec_init_then_push)]
unsafe fn create_opengl_view(
display: &mut MacosDisplay,
sample_count: i32,
Expand Down Expand Up @@ -865,12 +860,12 @@ impl crate::native::Clipboard for MacosClipboard {
}

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

unsafe fn set_icon(ns_app: ObjcId, icon: &Icon) {
let width = 64 as usize;
let height = 64 as usize;
let width = 64_usize;
let height = 64_usize;
let colors = &icon.big[..];
let rgb = CGColorSpaceCreateDeviceRGB();
let bits_per_component: usize = 8; // number of bits in UInt8
Expand Down Expand Up @@ -1069,7 +1064,7 @@ where
let window: ObjcId = msg_send![
window,
initWithContentRect: window_frame
styleMask: window_masks as u64
styleMask: window_masks
backing: NSBackingStoreType::NSBackingStoreBuffered as u64
defer: NO
];
Expand Down

0 comments on commit 4603799

Please sign in to comment.