Skip to content

Commit

Permalink
fix clippy warnings, mainly for linux
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joseluis committed Feb 7, 2025
1 parent dfc1a6f commit 0183ccd
Show file tree
Hide file tree
Showing 18 changed files with 434 additions and 424 deletions.
61 changes: 30 additions & 31 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -643,10 +638,11 @@ impl From<Comparison> 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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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.
Expand Down
38 changes: 19 additions & 19 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 _) }
Expand All @@ -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();
Expand Down Expand Up @@ -872,7 +878,7 @@ fn gl_info() -> ContextInfo {
backend: Backend::OpenGl,
gl_version_string,
glsl_support,
features: features,
features,
}
}

Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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!(
Expand All @@ -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 {
Expand Down
12 changes: 4 additions & 8 deletions src/graphics/gl/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/native/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -351,5 +352,5 @@ pub unsafe fn create_egl_context(
return Err(EglError::CreateContextFailed);
}

return Ok((context, config, display));
Ok((context, config, display))
}
1 change: 1 addition & 0 deletions src/native/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ macro_rules! gl_loader {
}

$(
#[allow(clippy::too_many_arguments)]
pub unsafe fn $fn($($arg: $t),*) -> $res {
__pfns::$fn.unwrap()( $($arg),* )
}
Expand Down
Loading

0 comments on commit 0183ccd

Please sign in to comment.