From 0183ccda9c883de36d68b2551b80d04d74e6f02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Thu, 6 Feb 2025 20:09:01 +0100 Subject: [PATCH 1/5] fix clippy warnings, mainly for linux Notes: - Warnings related to unsafe code has not been touched. - The number of warnings has been reduced from 527 to 3. - A few lints has been allowed globally, for convenience. Fixes: - fixed macros to use `$` in `$crate::`. - broke some long lines for readability. - solved some unneded mutable references. - simplified unneeded vecs for arrays. - simplified unneeded explicit returns. - simplified unneeded redundant conversions. - simplified fn signatures returning unit type. - simplified verbose comparisons with booleans. - simplified unnecessary explicit dereferences. - allowed some functions with too many args. - implemented `Default` for several items. - ...among others. --- src/graphics.rs | 61 ++--- src/graphics/gl.rs | 38 +-- src/graphics/gl/cache.rs | 12 +- src/lib.rs | 7 + src/native/egl.rs | 9 +- src/native/gl.rs | 1 + src/native/linux_wayland.rs | 37 +-- src/native/linux_wayland/decorations.rs | 1 + src/native/linux_wayland/extensions.rs | 18 +- src/native/linux_wayland/keycodes.rs | 244 ++++++++--------- src/native/linux_wayland/libwayland_client.rs | 5 +- src/native/linux_wayland/shm.rs | 6 +- src/native/linux_x11.rs | 73 ++--- src/native/linux_x11/clipboard.rs | 15 +- src/native/linux_x11/glx.rs | 63 +++-- src/native/linux_x11/keycodes.rs | 256 +++++++++--------- src/native/linux_x11/libx11_ex.rs | 10 +- src/native/linux_x11/xi_input.rs | 2 +- 18 files changed, 434 insertions(+), 424 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index dde3edb6..dbdfffc6 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -202,18 +202,13 @@ impl VertexFormat { } } -#[derive(Clone, Copy, PartialEq, Debug)] +#[derive(Clone, Copy, Debug, Default, PartialEq)] pub enum VertexStep { + #[default] PerVertex, PerInstance, } -impl Default for VertexStep { - fn default() -> VertexStep { - VertexStep::PerVertex - } -} - #[derive(Clone, Debug)] pub struct BufferLayout { pub stride: i32, @@ -643,10 +638,11 @@ impl From for GLenum { /// Specifies how incoming RGBA values (source) and the RGBA in framebuffer (destination) /// are combined. -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)] pub enum Equation { /// Adds source and destination. Source and destination are multiplied /// by blending parameters before addition. + #[default] Add, /// Subtracts destination from source. Source and destination are /// multiplied by blending parameters before subtraction. @@ -675,12 +671,6 @@ pub enum BlendFactor { SourceAlphaSaturate, } -impl Default for Equation { - fn default() -> Equation { - Equation::Add - } -} - #[derive(Debug, PartialEq, Clone, Copy)] pub enum PrimitiveType { Triangles, @@ -882,6 +872,12 @@ pub struct ElapsedQuery { gl_query: GLuint, } +impl Default for ElapsedQuery { + fn default() -> Self { + Self::new() + } +} + impl ElapsedQuery { pub fn new() -> ElapsedQuery { ElapsedQuery { gl_query: 0 } @@ -964,7 +960,7 @@ impl ElapsedQuery { /// /// Implemented as `glDeleteQueries(...)` on OpenGL/WebGL platforms. pub fn delete(&mut self) { - unsafe { glDeleteQueries(1, &mut self.gl_query) } + unsafe { glDeleteQueries(1, &self.gl_query) } self.gl_query = 0; } } @@ -1082,8 +1078,8 @@ pub struct ContextInfo { /// allowing to see which glsl versions are actually supported. /// Unfortunately, it only works on GL4.3+... and even there it is not quite correct. /// - /// miniquad will take a guess based on GL_VERSION_STRING, current platform and implementation details. - /// Would be all false on metal. + /// miniquad will take a guess based on GL_VERSION_STRING, current platform and implementation + /// details. Would be all false on metal. pub glsl_support: GlslSupport, /// List of platform-dependent features that miniquad failed to make cross-platforms /// and therefore they might be missing. @@ -1104,8 +1100,10 @@ impl ContextInfo { pub trait RenderingBackend { fn info(&self) -> ContextInfo; /// For metal context's ShaderSource should contain MSL source string, for GL - glsl. + /// /// If in doubt, _most_ OpenGL contexts support "#version 100" glsl shaders. - /// So far miniquad never encountered where it can create a rendering context, but `version 100` shaders are not supported. + /// So far miniquad never encountered where it can create a rendering context, + /// but `version 100` shaders are not supported. /// /// Typical `new_shader` invocation for an MSL and `glsl version 100` sources: /// ```ignore @@ -1239,13 +1237,14 @@ pub trait RenderingBackend { /// is recommended instead. fn render_pass_texture(&self, render_pass: RenderPass) -> TextureId { let textures = self.render_pass_color_attachments(render_pass); + #[allow(clippy::len_zero)] if textures.len() == 0 { panic!("depth-only render pass"); } if textures.len() != 1 { panic!("multiple render target render pass"); } - return textures[0]; + textures[0] } /// For depth-only render pass returns empty slice. fn render_pass_color_attachments(&self, render_pass: RenderPass) -> &[TextureId]; @@ -1289,29 +1288,29 @@ pub trait RenderingBackend { /// Delete GPU buffer, leaving handle unmodified. /// - /// More high-level code on top of miniquad probably is going to call this in Drop implementation of some - /// more RAII buffer object. + /// More high-level code on top of miniquad probably is going to call this in Drop + /// implementation of some more RAII buffer object. /// - /// There is no protection against using deleted buffers later. However its not an UB in OpenGl and thats why - /// this function is not marked as unsafe + /// There is no protection against using deleted buffers later. However its not an UB in OpenGl + /// and thats why this function is not marked as unsafe fn delete_buffer(&mut self, buffer: BufferId); /// Delete GPU texture, leaving handle unmodified. /// - /// More high-level code on top of miniquad probably is going to call this in Drop implementation of some - /// more RAII buffer object. + /// More high-level code on top of miniquad probably is going to call this in Drop + /// implementation of some more RAII buffer object. /// - /// There is no protection against using deleted textures later. However its not a CPU-level UB and thats why - /// this function is not marked as unsafe + /// There is no protection against using deleted textures later. However its not a CPU-level UB + /// and thats why this function is not marked as unsafe fn delete_texture(&mut self, texture: TextureId); /// Delete GPU program, leaving handle unmodified. /// - /// More high-level code on top of miniquad probably is going to call this in Drop implementation of some - /// more RAII buffer object. + /// More high-level code on top of miniquad probably is going to call this in Drop + /// implementation of some more RAII buffer object. /// - /// There is no protection against using deleted programs later. However its not a CPU-level Porgram and thats why - /// this function is not marked as unsafe + /// There is no protection against using deleted programs later. However its not a CPU-level + /// Porgram and thats why this function is not marked as unsafe fn delete_shader(&mut self, program: ShaderId); /// Set a new viewport rectangle. diff --git a/src/graphics/gl.rs b/src/graphics/gl.rs index 74be9fd9..37c0787c 100644 --- a/src/graphics/gl.rs +++ b/src/graphics/gl.rs @@ -521,6 +521,12 @@ pub struct GlContext { pub(crate) info: ContextInfo, } +impl Default for GlContext { + fn default() -> Self { + Self::new() + } +} + impl GlContext { pub fn new() -> GlContext { unsafe { @@ -767,7 +773,7 @@ impl GlContext { glStencilFuncSeparate( GL_BACK, back.test_func.into(), - back.test_ref.into(), + back.test_ref, back.test_mask, ); glStencilMaskSeparate(GL_BACK, back.write_mask); @@ -810,6 +816,7 @@ impl GlContext { } } +#[allow(clippy::field_reassign_with_default)] fn gl_info() -> ContextInfo { let version_string = unsafe { glGetString(super::gl::GL_VERSION) }; let gl_version_string = unsafe { std::ffi::CStr::from_ptr(version_string as _) } @@ -826,7 +833,6 @@ fn gl_info() -> ContextInfo { let features = Features { instancing: !gl2, resolve_attachments: !webgl1 && !gl2, - ..Default::default() }; let mut glsl_support = GlslSupport::default(); @@ -872,7 +878,7 @@ fn gl_info() -> ContextInfo { backend: Backend::OpenGl, gl_version_string, glsl_support, - features: features, + features, } } @@ -1005,11 +1011,8 @@ impl RenderingBackend for GlContext { ) { let mut t = self.textures.get(texture); t.resize(self, width, height, source); - match texture.0 { - TextureIdInner::Managed(tex_id) => { - self.textures.0[tex_id].params = t.params; - } - _ => {} + if let TextureIdInner::Managed(tex_id) = texture.0 { + self.textures.0[tex_id].params = t.params; }; } fn texture_read_pixels(&mut self, texture: TextureId, source: &mut [u8]) { @@ -1455,8 +1458,7 @@ impl RenderingBackend for GlContext { TextureOrRenderbuffer::Renderbuffer(id) => id, }; unsafe { - self.cache - .bind_texture(n, texture.params.kind.into(), raw); + self.cache.bind_texture(n, texture.params.kind.into(), raw); glUniform1i(gl_loc, n as i32); } } @@ -1524,13 +1526,11 @@ impl RenderingBackend for GlContext { gl_vbuf: vb.gl_buf, }); } - } else { - if cached_attr.is_some() { - unsafe { - glDisableVertexAttribArray(attr_index as GLuint); - } - *cached_attr = None; + } else if cached_attr.is_some() { + unsafe { + glDisableVertexAttribArray(attr_index as GLuint); } + *cached_attr = None; } } } @@ -1541,7 +1541,7 @@ impl RenderingBackend for GlContext { let mut offset = 0; - for (_, uniform) in shader.uniforms.iter().enumerate() { + for uniform in shader.uniforms.iter() { use UniformType::*; assert!( @@ -1550,8 +1550,8 @@ impl RenderingBackend for GlContext { ); unsafe { - let data = (uniform_ptr as *const f32).offset(offset as isize); - let data_int = (uniform_ptr as *const i32).offset(offset as isize); + let data = (uniform_ptr as *const f32).add(offset); + let data_int = (uniform_ptr as *const i32).add(offset); if let Some(gl_loc) = uniform.gl_loc { match uniform.uniform_type { diff --git a/src/graphics/gl/cache.rs b/src/graphics/gl/cache.rs index 11293161..2a3c1dcc 100644 --- a/src/graphics/gl/cache.rs +++ b/src/graphics/gl/cache.rs @@ -80,11 +80,9 @@ impl GlCache { self.bind_buffer(target, self.stored_vertex_buffer, None); self.stored_vertex_buffer = 0; } - } else { - if self.stored_index_buffer != 0 { - self.bind_buffer(target, self.stored_index_buffer, self.stored_index_type); - self.stored_index_buffer = 0; - } + } else if self.stored_index_buffer != 0 { + self.bind_buffer(target, self.stored_index_buffer, self.stored_index_type); + self.stored_index_buffer = 0; } } @@ -135,9 +133,7 @@ impl GlCache { let cached_attr = &mut self.attributes[attr_index]; if cached_attr.is_some() { - unsafe { - glDisableVertexAttribArray(attr_index as GLuint) - }; + unsafe { glDisableVertexAttribArray(attr_index as GLuint) }; } *cached_attr = None; } diff --git a/src/lib.rs b/src/lib.rs index 7385cd86..fb463729 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,11 @@ #![doc = include_str!("../README.md")] +#![allow( + clippy::collapsible_if, + clippy::unused_unit, + clippy::identity_op, + clippy::missing_safety_doc +)] + pub mod conf; mod event; pub mod fs; diff --git a/src/native/egl.rs b/src/native/egl.rs index 6aefbb0c..341d140f 100644 --- a/src/native/egl.rs +++ b/src/native/egl.rs @@ -278,7 +278,8 @@ pub unsafe fn create_egl_context( sample_count: i32, ) -> Result<(EGLContext, EGLConfig, EGLDisplay), EglError> { let display = (egl.eglGetDisplay.unwrap())(display as _); - if display == /* EGL_NO_DISPLAY */ null_mut() { + if display.is_null() { + // == EGL_NO_DISPLAY return Err(EglError::NoDisplay); } @@ -288,7 +289,7 @@ pub unsafe fn create_egl_context( let alpha_size = if alpha { 8 } else { 0 }; #[rustfmt::skip] - let cfg_attributes = vec![ + let cfg_attributes = [ EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, @@ -340,7 +341,7 @@ pub unsafe fn create_egl_context( if !exact_cfg_found { config = available_cfgs[0]; } - let ctx_attributes = vec![EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE]; + let ctx_attributes = [EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE]; let context = (egl.eglCreateContext.unwrap())( display, config, @@ -351,5 +352,5 @@ pub unsafe fn create_egl_context( return Err(EglError::CreateContextFailed); } - return Ok((context, config, display)); + Ok((context, config, display)) } diff --git a/src/native/gl.rs b/src/native/gl.rs index 9a72702b..145b7403 100644 --- a/src/native/gl.rs +++ b/src/native/gl.rs @@ -319,6 +319,7 @@ macro_rules! gl_loader { } $( + #[allow(clippy::too_many_arguments)] pub unsafe fn $fn($($arg: $t),*) -> $res { __pfns::$fn.unwrap()( $($arg),* ) } diff --git a/src/native/linux_wayland.rs b/src/native/linux_wayland.rs index c82951e0..10bc3045 100644 --- a/src/native/linux_wayland.rs +++ b/src/native/linux_wayland.rs @@ -200,7 +200,7 @@ unsafe extern "C" fn keyboard_handle_leave( EVENTS.push(WaylandEvent::KeyboardLeave); } unsafe extern "C" fn keyboard_handle_key( - data: *mut ::core::ffi::c_void, + _data: *mut ::core::ffi::c_void, _wl_keyboard: *mut wl_keyboard, _serial: u32, _time: u32, @@ -480,7 +480,7 @@ unsafe extern "C" fn xdg_toplevel_handle_configure( width: i32, height: i32, _states: *mut wl_array, -) -> () { +) { assert!(!data.is_null()); let payload: &mut WaylandPayload = &mut *(data as *mut _); let mut d = crate::native_display().lock().unwrap(); @@ -516,7 +516,7 @@ unsafe extern "C" fn xdg_wm_base_handle_ping( data: *mut std::ffi::c_void, toplevel: *mut extensions::xdg_shell::xdg_wm_base, serial: u32, -) -> () { +) { assert!(!data.is_null()); let payload: &mut WaylandPayload = &mut *(data as *mut _); @@ -604,12 +604,12 @@ where ); (display.client.wl_display_roundtrip)(wdisplay); - assert!(display.compositor.is_null() == false); - assert!(display.xdg_wm_base.is_null() == false); - assert!(display.subcompositor.is_null() == false); - assert!(display.seat.is_null() == false); - //assert!(display.keymap.is_null() == false); - //assert!(display.xkb_state.is_null() == false); + assert!(!display.compositor.is_null()); + assert!(!display.xdg_wm_base.is_null()); + assert!(!display.subcompositor.is_null()); + assert!(!display.seat.is_null()); + //assert!(!display.keymap.is_null()); + //assert!(!display.xkb_state.is_null()); let xdg_wm_base_listener = extensions::xdg_shell::xdg_wm_base_listener { ping: Some(xdg_wm_base_handle_ping), @@ -640,7 +640,7 @@ where WL_COMPOSITOR_CREATE_SURFACE, display.client.wl_surface_interface ); - assert!(display.surface.is_null() == false); + assert!(!display.surface.is_null()); let xdg_surface: *mut extensions::xdg_shell::xdg_surface = wl_request_constructor!( display.client, @@ -649,7 +649,7 @@ where &extensions::xdg_shell::xdg_surface_interface, display.surface ); - assert!(xdg_surface.is_null() == false); + assert!(!xdg_surface.is_null()); let xdg_surface_listener = extensions::xdg_shell::xdg_surface_listener { configure: Some(xdg_surface_handle_configure), @@ -667,7 +667,7 @@ where extensions::xdg_shell::xdg_surface::get_toplevel, &extensions::xdg_shell::xdg_toplevel_interface ); - assert!(display.xdg_toplevel.is_null() == false); + assert!(!display.xdg_toplevel.is_null()); let xdg_toplevel_listener = extensions::xdg_shell::xdg_toplevel_listener { configure: Some(xdg_toplevel_handle_configure), @@ -705,7 +705,8 @@ where std::ptr::null_mut(), ); - if egl_surface == /* EGL_NO_SURFACE */ std::ptr::null_mut() { + if egl_surface.is_null() { + // == EGL_NO_SURFACE panic!("surface creation failed"); } if (libegl.eglMakeCurrent.unwrap())(egl_display, egl_surface, egl_surface, context) == 0 { @@ -728,7 +729,7 @@ where libegl.eglGetProcAddress.expect("non-null function pointer")(name.as_ptr() as _) }); - if display.decoration_manager.is_null() == false { + if !display.decoration_manager.is_null() { let server_decoration: *mut extensions::xdg_decoration::zxdg_toplevel_decoration_v1 = wl_request_constructor!( display.client, display.decoration_manager, @@ -736,7 +737,7 @@ where &extensions::xdg_decoration::zxdg_toplevel_decoration_v1_interface, display.xdg_toplevel ); - assert!(server_decoration.is_null() == false); + assert!(!server_decoration.is_null()); wl_request!( display.client, @@ -764,7 +765,7 @@ where let mut repeated_keys: HashSet = HashSet::new(); let (mut last_mouse_x, mut last_mouse_y) = (0.0, 0.0); - while display.closed == false { + while !display.closed { (client.wl_display_dispatch_pending)(wdisplay); if let Some(ref mut event_handler) = display.event_handler { @@ -773,7 +774,7 @@ where (display.xkb.xkb_state_key_get_one_sym)(display.xkb_state, key + 8); let keycode = keycodes::translate(keysym); - event_handler.key_down_event(keycode.clone(), keymods, true); + event_handler.key_down_event(keycode, keymods, true); let chr = keycodes::keysym_to_unicode(&mut display.xkb, keysym); if chr > 0 { @@ -784,6 +785,7 @@ where } while let Ok(request) = rx.try_recv() { + #[allow(clippy::single_match)] match request { Request::SetFullscreen(full) => { if full { @@ -801,7 +803,6 @@ where ); } } - // TODO: implement the other events _ => (), } diff --git a/src/native/linux_wayland/decorations.rs b/src/native/linux_wayland/decorations.rs index a2e02320..c42e2ef3 100644 --- a/src/native/linux_wayland/decorations.rs +++ b/src/native/linux_wayland/decorations.rs @@ -31,6 +31,7 @@ pub(crate) struct Decorations { pub right_decoration: Decoration, } +#[allow(clippy::too_many_arguments)] unsafe fn create_decoration( display: &mut WaylandPayload, compositor: *mut wl_compositor, diff --git a/src/native/linux_wayland/extensions.rs b/src/native/linux_wayland/extensions.rs index f6df5a40..a5105582 100644 --- a/src/native/linux_wayland/extensions.rs +++ b/src/native/linux_wayland/extensions.rs @@ -7,7 +7,7 @@ pub mod xdg_shell; #[macro_export] macro_rules! count { () => (0usize); - ( $x:tt $($xs:tt)* ) => (1usize + crate::count!($($xs)*)); + ( $x:tt $($xs:tt)* ) => (1usize + $crate::count!($($xs)*)); } #[macro_export] @@ -16,7 +16,7 @@ macro_rules! method_consts { }; ($x:expr, ($next:ident, $($rest:ident,)*)) => { pub const $next: u32 = $x; - crate::method_consts!(($x + 1), ($($rest,)*)); + $crate::method_consts!(($x + 1), ($($rest,)*)); }; } @@ -40,18 +40,20 @@ macro_rules! wayland_interface { mod $method_name { use super::*; - pub static mut METHOD_ARGUMENTS_TYPES: [*const wl_interface; crate::count!($($method_argument_name)*)] = [$(unsafe { &$method_argument_name as *const _},)*]; + pub static mut METHOD_ARGUMENTS_TYPES: + [*const wl_interface; $crate::count!($($method_argument_name)*)] = + [$(unsafe { &$method_argument_name as *const _},)*]; } )* - static mut requests: [wl_message; crate::count!($($method_name)*)] = [$(wl_message { + static mut requests: [wl_message; $crate::count!($($method_name)*)] = [$(wl_message { name: concat!(stringify!($method_name), '\0').as_ptr() as _, signature: concat!($method_sign, '\0').as_ptr() as _, types: unsafe { $method_name::METHOD_ARGUMENTS_TYPES.as_ptr() as _ } }), *]; - static mut events: [wl_message; crate::count!($($event_name)*)] = [$(wl_message { + static mut events: [wl_message; $crate::count!($($event_name)*)] = [$(wl_message { name: concat!($event_name, '\0').as_ptr() as _, signature: concat!($event_sign, '\0').as_ptr() as _, types: std::ptr::null_mut() @@ -60,9 +62,9 @@ macro_rules! wayland_interface { pub static mut $name: wl_interface = wl_interface { name: concat!(stringify!($struct_name), '\0').as_ptr() as *const _, version: $version, - method_count: crate::count!($($method_name)*) as i32, + method_count: $crate::count!($($method_name)*) as i32, methods: unsafe { requests.as_ptr() }, - event_count: crate::count!($($event_name)*) as i32, + event_count: $crate::count!($($event_name)*) as i32, events: unsafe { events.as_ptr() }, }; } @@ -74,7 +76,7 @@ macro_rules! wayland_interface { } impl $struct_name { - crate::method_consts!(0, ($($method_name,)*)); + $crate::method_consts!(0, ($($method_name,)*)); } pub use $name::$name; }; diff --git a/src/native/linux_wayland/keycodes.rs b/src/native/linux_wayland/keycodes.rs index 3d9d4cc6..91c35c77 100644 --- a/src/native/linux_wayland/keycodes.rs +++ b/src/native/linux_wayland/keycodes.rs @@ -4,129 +4,129 @@ use crate::native::linux_wayland::libxkbcommon::LibXkbCommon; pub fn translate(keysym: u32) -> KeyCode { // See xkbcommon/xkbcommon-keysyms.h match keysym { - 65307 => return KeyCode::Escape, - 65289 => return KeyCode::Tab, - 65505 => return KeyCode::LeftShift, - 65506 => return KeyCode::RightShift, - 65507 => return KeyCode::LeftControl, - 65508 => return KeyCode::RightControl, - 65511 | 65513 => return KeyCode::LeftAlt, - 65406 | 65027 | 65512 | 65514 => return KeyCode::RightAlt, - 65515 => return KeyCode::LeftSuper, - 65516 => return KeyCode::RightSuper, - 65383 => return KeyCode::Menu, - 65407 => return KeyCode::NumLock, - 65509 => return KeyCode::CapsLock, - 65377 => return KeyCode::PrintScreen, - 65300 => return KeyCode::ScrollLock, - 65299 => return KeyCode::Pause, - 65535 => return KeyCode::Delete, - 65288 => return KeyCode::Backspace, - 65293 => return KeyCode::Enter, - 65360 => return KeyCode::Home, - 65367 => return KeyCode::End, - 65365 => return KeyCode::PageUp, - 65366 => return KeyCode::PageDown, - 65379 => return KeyCode::Insert, - 65361 => return KeyCode::Left, - 65363 => return KeyCode::Right, - 65364 => return KeyCode::Down, - 65362 => return KeyCode::Up, - 65470 => return KeyCode::F1, - 65471 => return KeyCode::F2, - 65472 => return KeyCode::F3, - 65473 => return KeyCode::F4, - 65474 => return KeyCode::F5, - 65475 => return KeyCode::F6, - 65476 => return KeyCode::F7, - 65477 => return KeyCode::F8, - 65478 => return KeyCode::F9, - 65479 => return KeyCode::F10, - 65480 => return KeyCode::F11, - 65481 => return KeyCode::F12, - 65482 => return KeyCode::F13, - 65483 => return KeyCode::F14, - 65484 => return KeyCode::F15, - 65485 => return KeyCode::F16, - 65486 => return KeyCode::F17, - 65487 => return KeyCode::F18, - 65488 => return KeyCode::F19, - 65489 => return KeyCode::F20, - 65490 => return KeyCode::F21, - 65491 => return KeyCode::F22, - 65492 => return KeyCode::F23, - 65493 => return KeyCode::F24, - 65494 => return KeyCode::F25, - 65455 => return KeyCode::KpDivide, - 65450 => return KeyCode::KpMultiply, - 65453 => return KeyCode::KpSubtract, - 65451 => return KeyCode::KpAdd, - 65438 => return KeyCode::Kp0, - 65436 => return KeyCode::Kp1, - 65433 => return KeyCode::Kp2, - 65435 => return KeyCode::Kp3, - 65430 => return KeyCode::Kp4, - 65437 => return KeyCode::Kp5, - 65432 => return KeyCode::Kp6, - 65429 => return KeyCode::Kp7, - 65431 => return KeyCode::Kp8, - 65434 => return KeyCode::Kp9, - 65439 => return KeyCode::KpDecimal, - 65469 => return KeyCode::KpEqual, - 65421 => return KeyCode::KpEnter, - 65 | 97 => return KeyCode::A, - 66 | 98 => return KeyCode::B, - 67 | 99 => return KeyCode::C, - 68 | 100 => return KeyCode::D, - 69 | 101 => return KeyCode::E, - 70 | 102 => return KeyCode::F, - 71 | 103 => return KeyCode::G, - 72 | 104 => return KeyCode::H, - 73 | 105 => return KeyCode::I, - 74 | 106 => return KeyCode::J, - 75 | 107 => return KeyCode::K, - 76 | 108 => return KeyCode::L, - 77 | 109 => return KeyCode::M, - 78 | 110 => return KeyCode::N, - 79 | 111 => return KeyCode::O, - 80 | 112 => return KeyCode::P, - 81 | 113 => return KeyCode::Q, - 82 | 114 => return KeyCode::R, - 83 | 115 => return KeyCode::S, - 84 | 116 => return KeyCode::T, - 85 | 117 => return KeyCode::U, - 86 | 118 => return KeyCode::V, - 87 | 119 => return KeyCode::W, - 88 | 120 => return KeyCode::X, - 89 | 121 => return KeyCode::Y, - 90 | 122 => return KeyCode::Z, - 49 => return KeyCode::Key1, - 50 => return KeyCode::Key2, - 51 => return KeyCode::Key3, - 52 => return KeyCode::Key4, - 53 => return KeyCode::Key5, - 54 => return KeyCode::Key6, - 55 => return KeyCode::Key7, - 56 => return KeyCode::Key8, - 57 => return KeyCode::Key9, - 48 => return KeyCode::Key0, - 32 => return KeyCode::Space, - 45 => return KeyCode::Minus, - 61 => return KeyCode::Equal, - 91 => return KeyCode::LeftBracket, - 93 => return KeyCode::RightBracket, - 92 => return KeyCode::Backslash, - 59 => return KeyCode::Semicolon, - 39 => return KeyCode::Apostrophe, - 96 => return KeyCode::GraveAccent, - 44 => return KeyCode::Comma, - 46 => return KeyCode::Period, - 47 => return KeyCode::Slash, - 60 => return KeyCode::World1, - _ => return KeyCode::Unknown, - }; + 65307 => KeyCode::Escape, + 65289 => KeyCode::Tab, + 65505 => KeyCode::LeftShift, + 65506 => KeyCode::RightShift, + 65507 => KeyCode::LeftControl, + 65508 => KeyCode::RightControl, + 65511 | 65513 => KeyCode::LeftAlt, + 65406 | 65027 | 65512 | 65514 => KeyCode::RightAlt, + 65515 => KeyCode::LeftSuper, + 65516 => KeyCode::RightSuper, + 65383 => KeyCode::Menu, + 65407 => KeyCode::NumLock, + 65509 => KeyCode::CapsLock, + 65377 => KeyCode::PrintScreen, + 65300 => KeyCode::ScrollLock, + 65299 => KeyCode::Pause, + 65535 => KeyCode::Delete, + 65288 => KeyCode::Backspace, + 65293 => KeyCode::Enter, + 65360 => KeyCode::Home, + 65367 => KeyCode::End, + 65365 => KeyCode::PageUp, + 65366 => KeyCode::PageDown, + 65379 => KeyCode::Insert, + 65361 => KeyCode::Left, + 65363 => KeyCode::Right, + 65364 => KeyCode::Down, + 65362 => KeyCode::Up, + 65470 => KeyCode::F1, + 65471 => KeyCode::F2, + 65472 => KeyCode::F3, + 65473 => KeyCode::F4, + 65474 => KeyCode::F5, + 65475 => KeyCode::F6, + 65476 => KeyCode::F7, + 65477 => KeyCode::F8, + 65478 => KeyCode::F9, + 65479 => KeyCode::F10, + 65480 => KeyCode::F11, + 65481 => KeyCode::F12, + 65482 => KeyCode::F13, + 65483 => KeyCode::F14, + 65484 => KeyCode::F15, + 65485 => KeyCode::F16, + 65486 => KeyCode::F17, + 65487 => KeyCode::F18, + 65488 => KeyCode::F19, + 65489 => KeyCode::F20, + 65490 => KeyCode::F21, + 65491 => KeyCode::F22, + 65492 => KeyCode::F23, + 65493 => KeyCode::F24, + 65494 => KeyCode::F25, + 65455 => KeyCode::KpDivide, + 65450 => KeyCode::KpMultiply, + 65453 => KeyCode::KpSubtract, + 65451 => KeyCode::KpAdd, + 65438 => KeyCode::Kp0, + 65436 => KeyCode::Kp1, + 65433 => KeyCode::Kp2, + 65435 => KeyCode::Kp3, + 65430 => KeyCode::Kp4, + 65437 => KeyCode::Kp5, + 65432 => KeyCode::Kp6, + 65429 => KeyCode::Kp7, + 65431 => KeyCode::Kp8, + 65434 => KeyCode::Kp9, + 65439 => KeyCode::KpDecimal, + 65469 => KeyCode::KpEqual, + 65421 => KeyCode::KpEnter, + 65 | 97 => KeyCode::A, + 66 | 98 => KeyCode::B, + 67 | 99 => KeyCode::C, + 68 | 100 => KeyCode::D, + 69 | 101 => KeyCode::E, + 70 | 102 => KeyCode::F, + 71 | 103 => KeyCode::G, + 72 | 104 => KeyCode::H, + 73 | 105 => KeyCode::I, + 74 | 106 => KeyCode::J, + 75 | 107 => KeyCode::K, + 76 | 108 => KeyCode::L, + 77 | 109 => KeyCode::M, + 78 | 110 => KeyCode::N, + 79 | 111 => KeyCode::O, + 80 | 112 => KeyCode::P, + 81 | 113 => KeyCode::Q, + 82 | 114 => KeyCode::R, + 83 | 115 => KeyCode::S, + 84 | 116 => KeyCode::T, + 85 | 117 => KeyCode::U, + 86 | 118 => KeyCode::V, + 87 | 119 => KeyCode::W, + 88 | 120 => KeyCode::X, + 89 | 121 => KeyCode::Y, + 90 | 122 => KeyCode::Z, + 49 => KeyCode::Key1, + 50 => KeyCode::Key2, + 51 => KeyCode::Key3, + 52 => KeyCode::Key4, + 53 => KeyCode::Key5, + 54 => KeyCode::Key6, + 55 => KeyCode::Key7, + 56 => KeyCode::Key8, + 57 => KeyCode::Key9, + 48 => KeyCode::Key0, + 32 => KeyCode::Space, + 45 => KeyCode::Minus, + 61 => KeyCode::Equal, + 91 => KeyCode::LeftBracket, + 93 => KeyCode::RightBracket, + 92 => KeyCode::Backslash, + 59 => KeyCode::Semicolon, + 39 => KeyCode::Apostrophe, + 96 => KeyCode::GraveAccent, + 44 => KeyCode::Comma, + 46 => KeyCode::Period, + 47 => KeyCode::Slash, + 60 => KeyCode::World1, + _ => KeyCode::Unknown, + } } pub unsafe extern "C" fn keysym_to_unicode(libxkbcommon: &mut LibXkbCommon, keysym: u32) -> i32 { - return (libxkbcommon.xkb_keysym_to_utf32)(keysym as u32) as i32; + (libxkbcommon.xkb_keysym_to_utf32)(keysym) as i32 } diff --git a/src/native/linux_wayland/libwayland_client.rs b/src/native/linux_wayland/libwayland_client.rs index 344ae79f..58ac5b26 100644 --- a/src/native/linux_wayland/libwayland_client.rs +++ b/src/native/linux_wayland/libwayland_client.rs @@ -664,9 +664,7 @@ impl LibWaylandClient { interface: *const wl_interface, version: u32, ) -> *mut std::ffi::c_void { - let id: *mut wl_proxy; - - id = (self.wl_proxy_marshal_constructor_versioned)( + let id: *mut wl_proxy = (self.wl_proxy_marshal_constructor_versioned)( wl_registry as _, WL_REGISTRY_BIND, interface as _, @@ -676,7 +674,6 @@ impl LibWaylandClient { version, std::ptr::null_mut::(), ); - id as *mut _ } } diff --git a/src/native/linux_wayland/shm.rs b/src/native/linux_wayland/shm.rs index ee3bd9e1..657b4411 100644 --- a/src/native/linux_wayland/shm.rs +++ b/src/native/linux_wayland/shm.rs @@ -14,7 +14,7 @@ unsafe extern "C" fn create_tmpfile_cloexec(tmpname: *mut libc::c_char) -> libc: if fd >= 0 { libc::unlink(tmpname); } - return fd; + fd } unsafe extern "C" fn create_anonymous_file(size: usize) -> libc::c_int { @@ -33,7 +33,7 @@ unsafe extern "C" fn create_anonymous_file(size: usize) -> libc::c_int { libc::close(fd); panic!("Cant create temp file") } - return fd; + fd } pub unsafe fn create_shm_buffer( @@ -94,5 +94,5 @@ pub unsafe fn create_shm_buffer( libc::munmap(data, length as _); wl_shm_pool_destroy(libwayland, pool); - return buffer; + buffer } diff --git a/src/native/linux_x11.rs b/src/native/linux_x11.rs index 5ddb9eef..368dd76a 100644 --- a/src/native/linux_x11.rs +++ b/src/native/linux_x11.rs @@ -53,16 +53,16 @@ pub struct X11Display { impl X11Display { unsafe fn process_event(&mut self, event: &mut XEvent, event_handler: &mut dyn EventHandler) { - match (*event).type_0 { + match event.type_0 { 2 => { - let keycode = (*event).xkey.keycode as libc::c_int; + let keycode = event.xkey.keycode as libc::c_int; let key = keycodes::translate_key(&mut self.libx11, self.display, keycode); let repeat = self.repeated_keycodes[(keycode & 0xff) as usize]; self.repeated_keycodes[(keycode & 0xff) as usize] = true; - let mods = keycodes::translate_mod((*event).xkey.state as libc::c_int); + let mods = keycodes::translate_mod(event.xkey.state as libc::c_int); let mut keysym: KeySym = 0; (self.libx11.XLookupString)( - &mut (*event).xkey, + &mut event.xkey, std::ptr::null_mut(), 0 as libc::c_int, &mut keysym, @@ -77,21 +77,21 @@ impl X11Display { event_handler.key_down_event(key, mods, repeat); } 3 => { - let keycode = (*event).xkey.keycode; + let keycode = event.xkey.keycode; let key = keycodes::translate_key(&mut self.libx11, self.display, keycode as _); self.repeated_keycodes[(keycode & 0xff) as usize] = false; - let mods = keycodes::translate_mod((*event).xkey.state as libc::c_int); + let mods = keycodes::translate_mod(event.xkey.state as libc::c_int); event_handler.key_up_event(key, mods); } 4 => { - let btn = keycodes::translate_mouse_button((*event).xbutton.button as _); - let x = (*event).xmotion.x as libc::c_float; - let y = (*event).xmotion.y as libc::c_float; + let btn = keycodes::translate_mouse_button(event.xbutton.button as _); + let x = event.xmotion.x as libc::c_float; + let y = event.xmotion.y as libc::c_float; if btn != crate::event::MouseButton::Unknown { event_handler.mouse_button_down_event(btn, x, y); } else { - match (*event).xbutton.button { + match event.xbutton.button { 4 => { event_handler.mouse_wheel_event(0.0, 1.0); } @@ -109,9 +109,9 @@ impl X11Display { } } 5 => { - let btn = keycodes::translate_mouse_button((*event).xbutton.button as _); - let x = (*event).xmotion.x as libc::c_float; - let y = (*event).xmotion.y as libc::c_float; + let btn = keycodes::translate_mouse_button(event.xbutton.button as _); + let x = event.xmotion.x as libc::c_float; + let y = event.xmotion.y as libc::c_float; if btn != crate::event::MouseButton::Unknown { event_handler.mouse_button_up_event(btn, x, y); @@ -124,8 +124,8 @@ impl X11Display { // Mouse Leave } 6 => { - let x = (*event).xmotion.x as libc::c_float; - let y = (*event).xmotion.y as libc::c_float; + let x = event.xmotion.x as libc::c_float; + let y = event.xmotion.y as libc::c_float; event_handler.mouse_motion_event(x, y); } 9 => { @@ -136,14 +136,14 @@ impl X11Display { } 22 => { let mut d = crate::native_display().try_lock().unwrap(); - let left = (*event).xconfigure.x; - let top = (*event).xconfigure.y; + let left = event.xconfigure.x; + let top = event.xconfigure.y; d.screen_position = (left as _, top as _); - if (*event).xconfigure.width != d.screen_width - || (*event).xconfigure.height != d.screen_height + if event.xconfigure.width != d.screen_width + || event.xconfigure.height != d.screen_height { - let width = (*event).xconfigure.width; - let height = (*event).xconfigure.height; + let width = event.xconfigure.width; + let height = event.xconfigure.height; d.screen_width = width; d.screen_height = height; drop(d); @@ -152,8 +152,8 @@ impl X11Display { } 33 => { let mut d = crate::native_display().try_lock().unwrap(); - if (*event).xclient.message_type == self.libx11.extensions.wm_protocols { - let protocol = (*event).xclient.data.l[0 as libc::c_int as usize] as Atom; + if event.xclient.message_type == self.libx11.extensions.wm_protocols { + let protocol = event.xclient.data.l[0 as libc::c_int as usize] as Atom; if protocol == self.libx11.extensions.wm_delete_window { d.quit_requested = true; } @@ -171,9 +171,9 @@ impl X11Display { 17 => {} // GenericEvent - 35 if Some((*event).xcookie.extension) == self.libxi.xi_extension_opcode => { - if (*event).xcookie.evtype == xi_input::XI_RawMotion { - let (dx, dy) = self.libxi.read_cookie(&mut (*event).xcookie, self.display); + 35 if Some(event.xcookie.extension) == self.libxi.xi_extension_opcode => { + if event.xcookie.evtype == xi_input::XI_RawMotion { + let (dx, dy) = self.libxi.read_cookie(&mut event.xcookie, self.display); event_handler.raw_mouse_motion(dx as f32, dy as f32); } } @@ -247,7 +247,7 @@ impl X11Display { serial: 0, send_event: true as _, message_type: wm_state, - window: window, + window, display: self.display, format: 32, data: ClientMessageData { @@ -438,7 +438,7 @@ where // them all in one frame. // However, when there are no events in the queue, +1 hack // will block main thread and release the cpu until the new event. - count = count + 1; + count += 1; } for _ in 0..count { @@ -498,7 +498,8 @@ where std::ptr::null_mut(), ); - if egl_surface == /* EGL_NO_SURFACE */ std::ptr::null_mut() { + if egl_surface.is_null() { + // == EGL_NO_SURFACE panic!("surface creation failed"); } if (egl_lib.eglMakeCurrent.unwrap())(egl_display, egl_surface, egl_surface, context) == 0 { @@ -546,7 +547,7 @@ where let block_on_wait = conf.platform.blocking_event_loop && !display.update_requested; if block_on_wait { // same thing as in glx loop, explained there - count = count + 1; + count += 1; } for _ in 0..count { let mut xevent = _XEvent { type_0: 0 }; @@ -625,19 +626,19 @@ where match conf.platform.linux_x11_gl { crate::conf::LinuxX11Gl::GLXOnly => { - glx_main_loop(display, &conf, f, x11_screen).ok().unwrap(); + glx_main_loop(display, conf, f, x11_screen).ok().unwrap(); } crate::conf::LinuxX11Gl::EGLOnly => { - egl_main_loop(display, &conf, f).ok().unwrap(); + egl_main_loop(display, conf, f).ok().unwrap(); } crate::conf::LinuxX11Gl::GLXWithEGLFallback => { - if let Err(display) = glx_main_loop(display, &conf, f, x11_screen) { - egl_main_loop(display, &conf, f).ok().unwrap(); + if let Err(display) = glx_main_loop(display, conf, f, x11_screen) { + egl_main_loop(display, conf, f).ok().unwrap(); } } crate::conf::LinuxX11Gl::EGLWithGLXFallback => { - if let Err(display) = egl_main_loop(display, &conf, f) { - glx_main_loop(display, &conf, f, x11_screen).ok().unwrap(); + if let Err(display) = egl_main_loop(display, conf, f) { + glx_main_loop(display, conf, f, x11_screen).ok().unwrap(); } } } diff --git a/src/native/linux_x11/clipboard.rs b/src/native/linux_x11/clipboard.rs index aba5ab58..611952fd 100644 --- a/src/native/linux_x11/clipboard.rs +++ b/src/native/linux_x11/clipboard.rs @@ -1,7 +1,9 @@ #![allow(non_upper_case_globals, non_snake_case)] //! Clipboard implementation for X11 -//! Clipboard API on X11 is pretty weird https://www.uninformativ.de/blog/postings/2017-04-02/0/POSTING-en.html +//! +//! Clipboard API on X11 is pretty weird +//! //! so use this with caution. use super::libx11::*; @@ -20,7 +22,7 @@ unsafe fn get_clipboard( bufname: *const libc::c_char, fmtname: *const libc::c_char, ) -> Option { - let mut result = 0 as *mut libc::c_char; + let mut result = std::ptr::null_mut::(); let mut ressize: libc::c_ulong = 0; let mut restail: libc::c_ulong = 0; let mut resbits: libc::c_int = 0; @@ -59,7 +61,7 @@ unsafe fn get_clipboard( } } if event.xselection.property != 0 { - let read_size = (100 as u32 * std::mem::size_of::() as u32) as libc::c_long; + let read_size = (100_u32 * std::mem::size_of::() as u32) as libc::c_long; let mut bytes: Vec = vec![]; let mut offset: libc::c_long = 0 as libc::c_long; loop { @@ -94,8 +96,7 @@ unsafe fn get_clipboard( } } } - - return None; + None } // Next message for clipboard request @@ -161,8 +162,8 @@ pub(crate) unsafe fn respond_to_clipboard_request( UTF8, 8 as libc::c_int, PropModeReplace, - message.as_bytes().as_ptr() as *const u8 as *const _, - message.as_bytes().len() as _, + message.as_bytes().as_ptr(), + message.len() as _, ); (libx11.XSendEvent)( diff --git a/src/native/linux_x11/glx.rs b/src/native/linux_x11/glx.rs index f6308b77..804e8017 100644 --- a/src/native/linux_x11/glx.rs +++ b/src/native/linux_x11/glx.rs @@ -1,4 +1,4 @@ -#![allow(dead_code, non_snake_case)] +#![allow(dead_code, non_snake_case, clippy::upper_case_acronyms)] use super::{libx11::*, X11Error}; @@ -403,20 +403,18 @@ unsafe fn choose_fbconfig( multisample: bool, desired_sample_count: i32, ) -> GLXFBConfig { - let native_configs: *mut GLXFBConfig; - let closest: *const GLFBConfig; let mut native_count: libc::c_int = 0; let mut usable_count; - let vendor; let mut trust_window_bit = true; - vendor = (libgl.glxGetClientString.unwrap())(display, GLX_VENDOR); + let vendor = (libgl.glxGetClientString.unwrap())(display, GLX_VENDOR); if !vendor.is_null() && libc::strcmp(vendor, b"Chromium\x00" as *const u8 as *const libc::c_char) == 0 as libc::c_int { trust_window_bit = false } - native_configs = (libgl.glxGetFBConfigs.unwrap())(display, screen, &mut native_count); + let native_configs: *mut GLXFBConfig = + (libgl.glxGetFBConfigs.unwrap())(display, screen, &mut native_count); if native_configs.is_null() || native_count == 0 { panic!("GLX: No GLXFBConfigs returned"); @@ -432,7 +430,7 @@ unsafe fn choose_fbconfig( let glx_attrib = |fbconfig, attrib| { let mut value: libc::c_int = 0; (libgl.glxGetFBConfigAttrib.unwrap())(display, fbconfig, attrib, &mut value); - return value; + value }; if 0 == glx_attrib(n, GLX_RENDER_TYPE) & GLX_RGBA_BIT { @@ -461,30 +459,34 @@ unsafe fn choose_fbconfig( usable_count += 1 } - let mut desired = GLFBConfig::default(); - desired.red_bits = 8; - desired.green_bits = 8; - desired.blue_bits = 8; - desired.alpha_bits = 8; - desired.depth_bits = 24; - desired.stencil_bits = 8; - desired.doublebuffer = true; - desired.samples = if desired_sample_count > 1 { - desired_sample_count - } else { - 0 - }; - closest = gl_choose_fbconfig( - &mut desired, - usable_configs.as_mut_ptr(), - usable_count as libc::c_uint, - ); let mut result = 0 as GLXFBConfig; - if !closest.is_null() { - result = (*closest).handle as GLXFBConfig + #[allow(clippy::field_reassign_with_default)] + { + let mut desired = GLFBConfig::default(); + desired.red_bits = 8; + desired.green_bits = 8; + desired.blue_bits = 8; + desired.alpha_bits = 8; + desired.depth_bits = 24; + desired.stencil_bits = 8; + desired.doublebuffer = true; + desired.samples = if desired_sample_count > 1 { + desired_sample_count + } else { + 0 + }; + + let closest: *const GLFBConfig = gl_choose_fbconfig( + &desired, + usable_configs.as_mut_ptr(), + usable_count as libc::c_uint, + ); + if !closest.is_null() { + result = (*closest).handle as GLXFBConfig + } + (libx11.XFree)(native_configs as *mut libc::c_void); } - (libx11.XFree)(native_configs as *mut libc::c_void); - return result; + result } pub unsafe extern "C" fn gl_choose_fbconfig( @@ -559,6 +561,7 @@ pub unsafe extern "C" fn gl_choose_fbconfig( extra_diff += ((*desired).samples - (*current).samples) * ((*desired).samples - (*current).samples); } + #[allow(clippy::comparison_chain)] if missing < least_missing { closest = current } else if missing == least_missing { @@ -580,5 +583,5 @@ pub unsafe extern "C" fn gl_choose_fbconfig( } } } - return closest; + closest } diff --git a/src/native/linux_x11/keycodes.rs b/src/native/linux_x11/keycodes.rs index f8ee3e17..f48b9859 100644 --- a/src/native/linux_x11/keycodes.rs +++ b/src/native/linux_x11/keycodes.rs @@ -16,127 +16,127 @@ pub unsafe fn translate_key(libx11: &mut LibX11, display: *mut Display, scancode let keysym = *keysyms.offset(0 as libc::c_int as isize); (libx11.XFree)(keysyms as *mut libc::c_void); match keysym { - 65307 => return KeyCode::Escape, - 65289 => return KeyCode::Tab, - 65505 => return KeyCode::LeftShift, - 65506 => return KeyCode::RightShift, - 65507 => return KeyCode::LeftControl, - 65508 => return KeyCode::RightControl, - 65511 | 65513 => return KeyCode::LeftAlt, - 65406 | 65027 | 65512 | 65514 => return KeyCode::RightAlt, - 65515 => return KeyCode::LeftSuper, - 65516 => return KeyCode::RightSuper, - 65383 => return KeyCode::Menu, - 65407 => return KeyCode::NumLock, - 65509 => return KeyCode::CapsLock, - 65377 => return KeyCode::PrintScreen, - 65300 => return KeyCode::ScrollLock, - 65299 => return KeyCode::Pause, - 65535 => return KeyCode::Delete, - 65288 => return KeyCode::Backspace, - 65293 => return KeyCode::Enter, - 65360 => return KeyCode::Home, - 65367 => return KeyCode::End, - 65365 => return KeyCode::PageUp, - 65366 => return KeyCode::PageDown, - 65379 => return KeyCode::Insert, - 65361 => return KeyCode::Left, - 65363 => return KeyCode::Right, - 65364 => return KeyCode::Down, - 65362 => return KeyCode::Up, - 65470 => return KeyCode::F1, - 65471 => return KeyCode::F2, - 65472 => return KeyCode::F3, - 65473 => return KeyCode::F4, - 65474 => return KeyCode::F5, - 65475 => return KeyCode::F6, - 65476 => return KeyCode::F7, - 65477 => return KeyCode::F8, - 65478 => return KeyCode::F9, - 65479 => return KeyCode::F10, - 65480 => return KeyCode::F11, - 65481 => return KeyCode::F12, - 65482 => return KeyCode::F13, - 65483 => return KeyCode::F14, - 65484 => return KeyCode::F15, - 65485 => return KeyCode::F16, - 65486 => return KeyCode::F17, - 65487 => return KeyCode::F18, - 65488 => return KeyCode::F19, - 65489 => return KeyCode::F20, - 65490 => return KeyCode::F21, - 65491 => return KeyCode::F22, - 65492 => return KeyCode::F23, - 65493 => return KeyCode::F24, - 65494 => return KeyCode::F25, - 65455 => return KeyCode::KpDivide, - 65450 => return KeyCode::KpMultiply, - 65453 => return KeyCode::KpSubtract, - 65451 => return KeyCode::KpAdd, - 65438 => return KeyCode::Kp0, - 65436 => return KeyCode::Kp1, - 65433 => return KeyCode::Kp2, - 65435 => return KeyCode::Kp3, - 65430 => return KeyCode::Kp4, - 65437 => return KeyCode::Kp5, - 65432 => return KeyCode::Kp6, - 65429 => return KeyCode::Kp7, - 65431 => return KeyCode::Kp8, - 65434 => return KeyCode::Kp9, - 65439 => return KeyCode::KpDecimal, - 65469 => return KeyCode::KpEqual, - 65421 => return KeyCode::KpEnter, - 97 => return KeyCode::A, - 98 => return KeyCode::B, - 99 => return KeyCode::C, - 100 => return KeyCode::D, - 101 => return KeyCode::E, - 102 => return KeyCode::F, - 103 => return KeyCode::G, - 104 => return KeyCode::H, - 105 => return KeyCode::I, - 106 => return KeyCode::J, - 107 => return KeyCode::K, - 108 => return KeyCode::L, - 109 => return KeyCode::M, - 110 => return KeyCode::N, - 111 => return KeyCode::O, - 112 => return KeyCode::P, - 113 => return KeyCode::Q, - 114 => return KeyCode::R, - 115 => return KeyCode::S, - 116 => return KeyCode::T, - 117 => return KeyCode::U, - 118 => return KeyCode::V, - 119 => return KeyCode::W, - 120 => return KeyCode::X, - 121 => return KeyCode::Y, - 122 => return KeyCode::Z, - 49 => return KeyCode::Key1, - 50 => return KeyCode::Key2, - 51 => return KeyCode::Key3, - 52 => return KeyCode::Key4, - 53 => return KeyCode::Key5, - 54 => return KeyCode::Key6, - 55 => return KeyCode::Key7, - 56 => return KeyCode::Key8, - 57 => return KeyCode::Key9, - 48 => return KeyCode::Key0, - 32 => return KeyCode::Space, - 45 => return KeyCode::Minus, - 61 => return KeyCode::Equal, - 91 => return KeyCode::LeftBracket, - 93 => return KeyCode::RightBracket, - 92 => return KeyCode::Backslash, - 59 => return KeyCode::Semicolon, - 39 => return KeyCode::Apostrophe, - 96 => return KeyCode::GraveAccent, - 44 => return KeyCode::Comma, - 46 => return KeyCode::Period, - 47 => return KeyCode::Slash, - 60 => return KeyCode::World1, - _ => return KeyCode::Unknown, - }; + 65307 => KeyCode::Escape, + 65289 => KeyCode::Tab, + 65505 => KeyCode::LeftShift, + 65506 => KeyCode::RightShift, + 65507 => KeyCode::LeftControl, + 65508 => KeyCode::RightControl, + 65511 | 65513 => KeyCode::LeftAlt, + 65406 | 65027 | 65512 | 65514 => KeyCode::RightAlt, + 65515 => KeyCode::LeftSuper, + 65516 => KeyCode::RightSuper, + 65383 => KeyCode::Menu, + 65407 => KeyCode::NumLock, + 65509 => KeyCode::CapsLock, + 65377 => KeyCode::PrintScreen, + 65300 => KeyCode::ScrollLock, + 65299 => KeyCode::Pause, + 65535 => KeyCode::Delete, + 65288 => KeyCode::Backspace, + 65293 => KeyCode::Enter, + 65360 => KeyCode::Home, + 65367 => KeyCode::End, + 65365 => KeyCode::PageUp, + 65366 => KeyCode::PageDown, + 65379 => KeyCode::Insert, + 65361 => KeyCode::Left, + 65363 => KeyCode::Right, + 65364 => KeyCode::Down, + 65362 => KeyCode::Up, + 65470 => KeyCode::F1, + 65471 => KeyCode::F2, + 65472 => KeyCode::F3, + 65473 => KeyCode::F4, + 65474 => KeyCode::F5, + 65475 => KeyCode::F6, + 65476 => KeyCode::F7, + 65477 => KeyCode::F8, + 65478 => KeyCode::F9, + 65479 => KeyCode::F10, + 65480 => KeyCode::F11, + 65481 => KeyCode::F12, + 65482 => KeyCode::F13, + 65483 => KeyCode::F14, + 65484 => KeyCode::F15, + 65485 => KeyCode::F16, + 65486 => KeyCode::F17, + 65487 => KeyCode::F18, + 65488 => KeyCode::F19, + 65489 => KeyCode::F20, + 65490 => KeyCode::F21, + 65491 => KeyCode::F22, + 65492 => KeyCode::F23, + 65493 => KeyCode::F24, + 65494 => KeyCode::F25, + 65455 => KeyCode::KpDivide, + 65450 => KeyCode::KpMultiply, + 65453 => KeyCode::KpSubtract, + 65451 => KeyCode::KpAdd, + 65438 => KeyCode::Kp0, + 65436 => KeyCode::Kp1, + 65433 => KeyCode::Kp2, + 65435 => KeyCode::Kp3, + 65430 => KeyCode::Kp4, + 65437 => KeyCode::Kp5, + 65432 => KeyCode::Kp6, + 65429 => KeyCode::Kp7, + 65431 => KeyCode::Kp8, + 65434 => KeyCode::Kp9, + 65439 => KeyCode::KpDecimal, + 65469 => KeyCode::KpEqual, + 65421 => KeyCode::KpEnter, + 97 => KeyCode::A, + 98 => KeyCode::B, + 99 => KeyCode::C, + 100 => KeyCode::D, + 101 => KeyCode::E, + 102 => KeyCode::F, + 103 => KeyCode::G, + 104 => KeyCode::H, + 105 => KeyCode::I, + 106 => KeyCode::J, + 107 => KeyCode::K, + 108 => KeyCode::L, + 109 => KeyCode::M, + 110 => KeyCode::N, + 111 => KeyCode::O, + 112 => KeyCode::P, + 113 => KeyCode::Q, + 114 => KeyCode::R, + 115 => KeyCode::S, + 116 => KeyCode::T, + 117 => KeyCode::U, + 118 => KeyCode::V, + 119 => KeyCode::W, + 120 => KeyCode::X, + 121 => KeyCode::Y, + 122 => KeyCode::Z, + 49 => KeyCode::Key1, + 50 => KeyCode::Key2, + 51 => KeyCode::Key3, + 52 => KeyCode::Key4, + 53 => KeyCode::Key5, + 54 => KeyCode::Key6, + 55 => KeyCode::Key7, + 56 => KeyCode::Key8, + 57 => KeyCode::Key9, + 48 => KeyCode::Key0, + 32 => KeyCode::Space, + 45 => KeyCode::Minus, + 61 => KeyCode::Equal, + 91 => KeyCode::LeftBracket, + 93 => KeyCode::RightBracket, + 92 => KeyCode::Backslash, + 59 => KeyCode::Semicolon, + 39 => KeyCode::Apostrophe, + 96 => KeyCode::GraveAccent, + 44 => KeyCode::Comma, + 46 => KeyCode::Period, + 47 => KeyCode::Slash, + 60 => KeyCode::World1, + _ => KeyCode::Unknown, + } } pub unsafe fn translate_mod(x11_mods: i32) -> KeyMods { @@ -153,21 +153,21 @@ pub unsafe fn translate_mod(x11_mods: i32) -> KeyMods { if x11_mods & super::libx11::Mod4Mask != 0 { mods.logo = true; } - return mods; + mods } pub unsafe fn translate_mouse_button(button: i32) -> MouseButton { match button { - 1 => return MouseButton::Left, - 2 => return MouseButton::Middle, - 3 => return MouseButton::Right, - _ => return MouseButton::Unknown, - }; + 1 => MouseButton::Left, + 2 => MouseButton::Middle, + 3 => MouseButton::Right, + _ => MouseButton::Unknown, + } } pub unsafe extern "C" fn keysym_to_unicode( libxkbcommon: &mut LibXkbCommon, keysym: super::libx11::KeySym, ) -> i32 { - return (libxkbcommon.xkb_keysym_to_utf32)(keysym as u32) as i32; + (libxkbcommon.xkb_keysym_to_utf32)(keysym as u32) as i32 } diff --git a/src/native/linux_x11/libx11_ex.rs b/src/native/linux_x11/libx11_ex.rs index 8efeb7ae..fc85013a 100644 --- a/src/native/linux_x11/libx11_ex.rs +++ b/src/native/linux_x11/libx11_ex.rs @@ -10,7 +10,7 @@ impl LibX11 { if !db.is_null() { let mut value = XrmValue { size: 0, - addr: 0 as *mut libc::c_char, + addr: std::ptr::null_mut::(), }; let mut type_ = std::ptr::null_mut(); if (self.XrmGetResource)( @@ -37,7 +37,7 @@ impl LibX11 { event: *mut XErrorEvent, ) -> libc::c_int { eprintln!("Error: {}", (*event).error_code); - return 0 as libc::c_int; + 0 as libc::c_int } (self.XSetErrorHandler)(Some( @@ -88,7 +88,7 @@ impl LibX11 { 8, PropModeReplace, c_title.as_ptr() as *mut libc::c_uchar, - libc::strlen(c_title.as_ptr()) as libc::c_int, + c_title.as_bytes().len() as libc::c_int, ); (self.XChangeProperty)( display, @@ -98,7 +98,7 @@ impl LibX11 { 8 as libc::c_int, PropModeReplace, c_title.as_ptr() as *mut libc::c_uchar, - libc::strlen(c_title.as_ptr()) as libc::c_int, + c_title.as_bytes().len() as libc::c_int, ); (self.XFlush)(display); } @@ -223,7 +223,7 @@ impl LibX11 { (self.XSetWMProtocols)(display, window, protocols.as_mut_ptr(), 1 as libc::c_int); let hints = (self.XAllocSizeHints)(); (*hints).flags |= PWinGravity; - if conf.window_resizable == false { + if !conf.window_resizable { (*hints).flags |= PMinSize | PMaxSize; (*hints).min_width = conf.window_width; (*hints).min_height = conf.window_height; diff --git a/src/native/linux_x11/xi_input.rs b/src/native/linux_x11/xi_input.rs index 85d392e5..403e2253 100644 --- a/src/native/linux_x11/xi_input.rs +++ b/src/native/linux_x11/xi_input.rs @@ -147,7 +147,7 @@ impl LibXi { (self.XGetEventData)(display, xcookie); - let raw_event = (*xcookie).data as *mut xi_input::XIRawEvent; + let raw_event = xcookie.data as *mut xi_input::XIRawEvent; let dx = *(*raw_event).raw_values; let dy = *(*raw_event).raw_values.offset(1); From 4603799f8728856eef4e752e1c4f8b916dd5bfbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Fri, 7 Feb 2025 10:30:01 +0100 Subject: [PATCH 2/5] 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 https://github.com/SSheldon/rust-objc/issues/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. --- Cargo.toml | 2 +- src/graphics/metal.rs | 19 +++++++++++-------- src/lib.rs | 1 + src/native/apple/apple_util.rs | 21 ++++++++------------- src/native/apple/frameworks.rs | 2 +- src/native/macos.rs | 31 +++++++++++++------------------ 6 files changed, 35 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73db940c..18f1fe97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/graphics/metal.rs b/src/graphics/metal.rs index 7ac8870f..39004579 100644 --- a/src/graphics/metal.rs +++ b/src/graphics/metal.rs @@ -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 { @@ -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); @@ -619,7 +626,7 @@ impl RenderingBackend for MetalContext { } let buffer = Buffer { raw, - size: size as usize, + size, value: 0, next_value: 0, }; @@ -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 { @@ -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, ) diff --git a/src/lib.rs b/src/lib.rs index fb463729..7f396c82 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 diff --git a/src/native/apple/apple_util.rs b/src/native/apple/apple_util.rs index 999f1e6c..8e4268c8 100644 --- a/src/native/apple/apple_util.rs +++ b/src/native/apple/apple_util.rs @@ -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::(), 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::(), ); - 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"); @@ -137,7 +132,7 @@ pub fn get_event_char(event: ObjcId) -> Option { } let chars = nsstring_to_string(characters); - if chars.len() == 0 { + if chars.is_empty() { return None; } Some(chars.chars().next().unwrap()) @@ -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 _) } diff --git a/src/native/apple/frameworks.rs b/src/native/apple/frameworks.rs index 6dbb40b0..f92b0551 100644 --- a/src/native/apple/frameworks.rs +++ b/src/native/apple/frameworks.rs @@ -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) } } } diff --git a/src/native/macos.rs b/src/native/macos.rs index c5aebef8..f867ec34 100644 --- a/src/native/macos.rs +++ b/src/native/macos.rs @@ -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 { @@ -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 } } @@ -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; } } } @@ -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 { @@ -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 }; @@ -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 { @@ -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 { @@ -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, @@ -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 @@ -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 ]; From 628e1d3485bf26809a2f36a80329151fd1a1f61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Fri, 7 Feb 2025 10:50:39 +0100 Subject: [PATCH 3/5] fix clippy warnings for windows - Warnings related to unsafe code has not been touched. - The number of warnings has been reduced from 19 to 0. Fixes: - simplified redundant casts. - simplified bool comparisons. - simplified unnecessary lifetimes. - simplified unneeded explicit returns. - simplified unnecessary map_or. - made transmute types explict. - ...among others. --- src/native/module.rs | 2 +- src/native/windows.rs | 19 ++++++++++-------- src/native/windows/wgl.rs | 42 +++++++++++++++++++++------------------ 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/native/module.rs b/src/native/module.rs index bcad5075..5ca90b1e 100644 --- a/src/native/module.rs +++ b/src/native/module.rs @@ -77,7 +77,7 @@ mod windows { if proc.is_null() { return Err(Error::DlSymError(name.to_string())); } - return Ok(unsafe { std::mem::transmute_copy(&proc) }); + Ok(unsafe { std::mem::transmute_copy(&proc) }) } } diff --git a/src/native/windows.rs b/src/native/windows.rs index 7f382e0f..de328aad 100644 --- a/src/native/windows.rs +++ b/src/native/windows.rs @@ -442,7 +442,7 @@ unsafe extern "system" fn win32_wndproc( dy = dy / 65535.0 * height; } - event_handler.raw_mouse_motion(dx as f32, dy as f32); + event_handler.raw_mouse_motion(dx, dy); } WM_MOUSELEAVE => { @@ -465,7 +465,7 @@ unsafe extern "system" fn win32_wndproc( let repeat = !!(lparam & 0x40000000) != 0; let mods = key_mods(); if chr > 0 { - if let Some(chr) = char::from_u32(chr as u32) { + if let Some(chr) = char::from_u32(chr) { event_handler.char_event(chr, mods, repeat); } } @@ -571,7 +571,7 @@ unsafe fn create_win_icon_from_image(width: u32, height: u32, colors: &[u8]) -> if color.is_null() { return None; } - assert!(target.is_null() == false); + assert!(!target.is_null()); let mask = CreateBitmap(width as _, height as _, 1, 1, std::ptr::null()); if mask.is_null() { @@ -697,7 +697,7 @@ unsafe fn create_window( GetModuleHandleW(NULL as _), // hInstance NULL as _, // lparam ); - assert!(hwnd.is_null() == false); + assert!(!hwnd.is_null()); let mut rawinputdevice: RAWINPUTDEVICE = std::mem::zeroed(); rawinputdevice.usUsagePage = HID_USAGE_PAGE_GENERIC; @@ -715,7 +715,7 @@ unsafe fn create_window( ShowWindow(hwnd, SW_SHOW); let dc = GetDC(hwnd); - assert!(dc.is_null() == false); + assert!(!dc.is_null()); DragAcceptFiles(hwnd, TRUE); @@ -742,7 +742,7 @@ unsafe fn create_msg_window() -> (HWND, HDC) { NULL, ); assert!( - msg_hwnd.is_null() == false, + !msg_hwnd.is_null(), "Win32: failed to create helper window!" ); ShowWindow(msg_hwnd, SW_HIDE); @@ -753,7 +753,7 @@ unsafe fn create_msg_window() -> (HWND, HDC) { } let msg_dc = GetDC(msg_hwnd); assert!( - msg_dc.is_null() == false, + !msg_dc.is_null(), "Win32: failed to obtain helper window DC!" ); @@ -771,7 +771,10 @@ impl WindowsDisplay { eprintln!("Load GL func {:?} failed.", proc); return None; } - Some(std::mem::transmute(proc_ptr)) + Some(std::mem::transmute::< + *mut winapi::shared::minwindef::__some_function, + unsafe extern "C" fn(), + >(proc_ptr)) } /// updates current window and framebuffer size from the window's client rect, diff --git a/src/native/windows/wgl.rs b/src/native/windows/wgl.rs index 6500c1c8..923f0777 100644 --- a/src/native/windows/wgl.rs +++ b/src/native/windows/wgl.rs @@ -162,6 +162,7 @@ pub unsafe fn gl_choose_fbconfig( extra_diff += (desired.samples - current.samples) * (desired.samples - current.samples); } + #[allow(clippy::comparison_chain)] if missing < least_missing { closest = Some(i); } else if missing == least_missing { @@ -176,7 +177,7 @@ pub unsafe fn gl_choose_fbconfig( // Least number of missing buffers is the most important heuristic, // then color buffer size match and lastly size match for other buffers - if closest.map_or(false, |closest| closest == i) { + if closest == Some(i) { least_missing = missing; least_color_diff = color_diff; least_extra_diff = extra_diff @@ -207,7 +208,7 @@ unsafe fn get_wgl_proc_address(libopengl32: &mut LibOpengl32, proc: &str) -> if proc.is_null() { return None; } - return Some(std::mem::transmute_copy(&proc)); + Some(std::mem::transmute_copy(&proc)) } impl Wgl { @@ -230,7 +231,7 @@ impl Wgl { if rc.is_null() { panic!("WGL: Failed to create dummy context"); } - if (display.libopengl32.wglMakeCurrent)(display.msg_dc, rc) == false { + if !(display.libopengl32.wglMakeCurrent)(display.msg_dc, rc) { panic!("WGL: Failed to make context current"); } @@ -249,7 +250,7 @@ impl Wgl { if let Some(getExtensionsStringEXT) = GetExtensionsStringEXT { let extensions = getExtensionsStringEXT(); - if extensions.is_null() == false { + if !extensions.is_null() { let extensions_string = std::ffi::CStr::from_ptr(extensions).to_string_lossy(); if extensions_string.contains(ext) { return true; @@ -259,7 +260,7 @@ impl Wgl { if let Some(getExtensionsStringARB) = GetExtensionsStringARB { let extensions = getExtensionsStringARB((display.libopengl32.wglGetCurrentDC)()); - if extensions.is_null() == false { + if !extensions.is_null() { let extensions_string = std::ffi::CStr::from_ptr(extensions).to_string_lossy(); if extensions_string.contains(ext) { @@ -267,7 +268,7 @@ impl Wgl { } } } - return false; + false }; let arb_multisample = wgl_ext_supported("WGL_ARB_multisample"); @@ -311,7 +312,7 @@ impl Wgl { ) { panic!("WGL: Failed to retrieve pixel format attribute"); } - return value; + value } unsafe fn wgl_find_pixel_format(&self, display: &mut WindowsDisplay, sample_count: i32) -> u32 { @@ -353,19 +354,22 @@ impl Wgl { } assert!(usable_count > 0); - let mut desired = GlFbconfig::default(); - desired.red_bits = 8; - desired.green_bits = 8; - desired.blue_bits = 8; - desired.alpha_bits = 8; - desired.depth_bits = 24; - desired.stencil_bits = 8; - desired.doublebuffer = true; - desired.samples = sample_count; - let closest = gl_choose_fbconfig(&mut desired, &usable_configs[..]); let mut pixel_format = 0; - if let Some(closest) = closest { - pixel_format = usable_configs[closest].handle; + #[allow(clippy::field_reassign_with_default)] + { + let mut desired = GlFbconfig::default(); + desired.red_bits = 8; + desired.green_bits = 8; + desired.blue_bits = 8; + desired.alpha_bits = 8; + desired.depth_bits = 24; + desired.stencil_bits = 8; + desired.doublebuffer = true; + desired.samples = sample_count; + let closest = gl_choose_fbconfig(&mut desired, &usable_configs[..]); + if let Some(closest) = closest { + pixel_format = usable_configs[closest].handle; + } } pixel_format } From fe5a319cfb339801e0bcebe5ab57c85668f74e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Fri, 7 Feb 2025 11:12:28 +0100 Subject: [PATCH 4/5] fix clippy warnings for wasm - Warnings related to unsafe code has not been touched. - The number of warnings has been reduced from 7 to 3 errors (denied lints). - made thread_local initializer const. - simplified unneded explicit returns. - reformat confusing else if. --- src/fs.rs | 1 + src/graphics/gl.rs | 3 +-- src/native/wasm.rs | 2 +- src/native/wasm/keycodes.rs | 12 ++++++------ 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index faff64a6..2212b042 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -83,6 +83,7 @@ mod wasm { use std::{cell::RefCell, collections::HashMap, thread_local}; thread_local! { + #[allow(clippy::type_complexity)] static FILES: RefCell>> = RefCell::new(HashMap::new()); } diff --git a/src/graphics/gl.rs b/src/graphics/gl.rs index 37c0787c..44fcfa3b 100644 --- a/src/graphics/gl.rs +++ b/src/graphics/gl.rs @@ -868,9 +868,8 @@ fn gl_info() -> ContextInfo { glsl_support.v150 = true; // MacOS is defaulting to 3.2 and GLSL 150 } else if gl_version_string.starts_with("4") || gl_version_string.starts_with("3.3") { glsl_support.v330 = true; - } else // gl 3.0, 3.1, 3.2 maps to 1.30, 1.40, 1.50 glsl versions - if gl_version_string.starts_with("3") { + } else if gl_version_string.starts_with("3") { glsl_support.v130 = true; } diff --git a/src/native/wasm.rs b/src/native/wasm.rs index 3ca4be42..f48326d4 100644 --- a/src/native/wasm.rs +++ b/src/native/wasm.rs @@ -27,7 +27,7 @@ use crate::{ thread_local! { static EVENT_HANDLER: RefCell>> = RefCell::new(None); - static REQUESTS: RefCell>> = RefCell::new(None); + static REQUESTS: RefCell>> = const { RefCell::new(None) }; } fn tl_event_handler T>(f: F) -> T { EVENT_HANDLER.with(|globals| { diff --git a/src/native/wasm/keycodes.rs b/src/native/wasm/keycodes.rs index 1399474a..b85cf5c3 100644 --- a/src/native/wasm/keycodes.rs +++ b/src/native/wasm/keycodes.rs @@ -6,11 +6,11 @@ use crate::event::{KeyCode, KeyMods, MouseButton, TouchPhase}; pub fn translate_mouse_button(button: i32) -> MouseButton { match button { - 0 => return MouseButton::Left, - 1 => return MouseButton::Right, - 2 => return MouseButton::Middle, - _ => return MouseButton::Unknown, - }; + 0 => MouseButton::Left, + 1 => MouseButton::Right, + 2 => MouseButton::Middle, + _ => MouseButton::Unknown, + } } pub fn translate_mod(wasm_mods: i32) -> KeyMods { const SAPP_MODIFIER_SHIFT: i32 = 1; @@ -31,7 +31,7 @@ pub fn translate_mod(wasm_mods: i32) -> KeyMods { if wasm_mods & SAPP_MODIFIER_SUPER != 0 { mods.logo = true; } - return mods; + mods } pub fn translate_keycode(keycode: i32) -> KeyCode { From 2a9998d02c96d0293929680aba455ef91c2d86d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Fri, 7 Feb 2025 11:17:53 +0100 Subject: [PATCH 5/5] fix clippy warnings for ios - Warnings related to unsafe code has not been touched. - The number of warnings has been reduced from 9 to 2 + 7 errors (denied lints). - simplified manual range contains. - simplified unneded explicit returns. - simplified destructuring with match to if let. --- src/native/ios.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/native/ios.rs b/src/native/ios.rs index f600430c..a98831ed 100644 --- a/src/native/ios.rs +++ b/src/native/ios.rs @@ -107,18 +107,15 @@ enum Message { unsafe impl Send for Message {} thread_local! { - static MESSAGES_TX: RefCell>> = RefCell::new(None); + static MESSAGES_TX: RefCell>> = const { RefCell::new(None) }; } impl MainThreadState { fn process_request(&mut self, request: crate::native::Request) { use crate::native::Request::*; - match request { - ScheduleUpdate => { - self.update_requested = true; - } - _ => {} + if let ScheduleUpdate = request { + self.update_requested = true; } } } @@ -275,7 +272,7 @@ pub fn define_glk_or_mtk_view(superclass: &Class) -> *const Class { } decl.add_ivar::<*mut c_void>("display_ptr"); - return decl.register(); + decl.register() } unsafe fn get_proc_address(name: *const u8) -> Option { @@ -371,7 +368,7 @@ pub fn define_glk_or_mtk_view_dlg(superclass: &Class) -> *const Class { } decl.add_ivar::<*mut c_void>("display_ptr"); - return decl.register(); + decl.register() } // metal or opengl view and the objects required to collect all the window events @@ -691,8 +688,7 @@ pub fn define_app_delegate() -> *const Class { application_will_resign_active as extern "C" fn(&Object, Sel, ObjcId), ); } - - return decl.register(); + decl.register() } fn define_textfield_dlg() -> *const Class { @@ -719,7 +715,7 @@ fn define_textfield_dlg() -> *const Class { let c: u16 = msg_send![string, characterAtIndex: i]; match c { - c if c >= 32 && (c < 0xD800 || c > 0xDFFF) => { + c if c >= 32 && !(0xD800..=0xDFFF).contains(&c) => { send_message(Message::Character { character: c as u32, }) @@ -778,7 +774,7 @@ fn define_textfield_dlg() -> *const Class { ); } decl.add_ivar::<*mut c_void>("display_ptr"); - return decl.register(); + decl.register() } pub fn log(message: &str) { @@ -825,6 +821,7 @@ pub fn load_file(path: &str, on_loaded: F) // this is the way to pass argument to UiApplicationMain // this static will be used exactly once, to .take() the "run" arguments +#[allow(clippy::type_complexity)] static mut RUN_ARGS: Option<(Box Box>, Conf)> = None; pub unsafe fn run(conf: Conf, f: F)