From 32946dc4cc12b4a1837be42938345de1b424936f Mon Sep 17 00:00:00 2001 From: Asger Nyman Christiansen Date: Fri, 28 Mar 2025 15:08:50 +0100 Subject: [PATCH 1/2] Fix clippy --- examples/headless/src/main.rs | 4 +-- examples/lighting/src/main.rs | 10 +++--- examples/lights/src/main.rs | 2 +- examples/logo/src/main.rs | 2 +- examples/shapes2d/src/main.rs | 2 +- examples/text/src/main.rs | 2 +- src/core.rs | 7 +---- src/core/buffer.rs | 2 +- src/core/buffer/element_buffer.rs | 2 +- src/core/data_type.rs | 2 +- src/core/program.rs | 4 +-- src/core/texture/texture2d.rs | 4 ++- src/core/texture/texture2d_array.rs | 6 ++-- src/core/texture/texture3d.rs | 4 ++- src/core/texture/texture_cube_map.rs | 6 ++-- src/core/uniform.rs | 2 +- src/gui/egui_gui.rs | 32 +++++++++----------- src/renderer.rs | 2 +- src/renderer/control/control2d.rs | 6 ++-- src/renderer/control/first_person_control.rs | 12 +++----- src/renderer/control/free_orbit_control.rs | 12 +++----- src/renderer/control/orbit_control.rs | 20 ++++++------ src/renderer/geometry/bounding_box.rs | 2 +- src/renderer/light/environment.rs | 6 ++-- src/window.rs | 3 +- src/window/headless.rs | 2 +- 26 files changed, 72 insertions(+), 86 deletions(-) diff --git a/examples/headless/src/main.rs b/examples/headless/src/main.rs index 217b03a86..f936c9d4a 100644 --- a/examples/headless/src/main.rs +++ b/examples/headless/src/main.rs @@ -62,9 +62,7 @@ fn main() { // Render three frames for frame_index in 0..3 { // Set the current transformation of the triangle - model.set_transformation(Mat4::from_angle_y(radians( - (frame_index as f32 * 0.6) as f32, - ))); + model.set_transformation(Mat4::from_angle_y(radians(frame_index as f32 * 0.6))); // Create a render target (a combination of a color and a depth texture) to write into let pixels = RenderTarget::new( diff --git a/examples/lighting/src/main.rs b/examples/lighting/src/main.rs index 945945501..89bbced17 100644 --- a/examples/lighting/src/main.rs +++ b/examples/lighting/src/main.rs @@ -167,12 +167,10 @@ pub async fn run() { ui.add(Slider::new(&mut spot0.intensity, 0.0..=10.0).text("Spot intensity")); ui.add(Slider::new(&mut point0.intensity, 0.0..=1.0).text("Point 0 intensity")); ui.add(Slider::new(&mut point1.intensity, 0.0..=1.0).text("Point 1 intensity")); - if ui.checkbox(&mut shadows_enabled, "Shadows").clicked() { - if !shadows_enabled { - spot0.clear_shadow_map(); - directional0.clear_shadow_map(); - directional1.clear_shadow_map(); - } + if ui.checkbox(&mut shadows_enabled, "Shadows").clicked() && !shadows_enabled { + spot0.clear_shadow_map(); + directional0.clear_shadow_map(); + directional1.clear_shadow_map(); } ui.label("Lighting model"); diff --git a/examples/lights/src/main.rs b/examples/lights/src/main.rs index 6640c3172..760cffaeb 100644 --- a/examples/lights/src/main.rs +++ b/examples/lights/src/main.rs @@ -154,7 +154,7 @@ impl Glow { ); Self { aabb, - light: PointLight::new(&context, 1.0, Srgba::WHITE, pos, Attenuation::default()), + light: PointLight::new(context, 1.0, Srgba::WHITE, pos, Attenuation::default()), velocity: vec3( rng.gen::() * 2.0 - 1.0, rng.gen::() * 2.0 - 1.0, diff --git a/examples/logo/src/main.rs b/examples/logo/src/main.rs index afc713b57..ecb3970e2 100644 --- a/examples/logo/src/main.rs +++ b/examples/logo/src/main.rs @@ -85,7 +85,7 @@ impl Material for LogoMaterial<'_> { } fn use_uniforms(&self, program: &Program, _viewer: &dyn Viewer, _lights: &[&dyn Light]) { - program.use_texture("image", &self.image); + program.use_texture("image", self.image); } fn render_states(&self) -> RenderStates { diff --git a/examples/shapes2d/src/main.rs b/examples/shapes2d/src/main.rs index c30c2981b..39b8e9883 100644 --- a/examples/shapes2d/src/main.rs +++ b/examples/shapes2d/src/main.rs @@ -77,7 +77,7 @@ pub fn main() { .screen() .clear(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0)) .render( - &Camera::new_2d(frame_input.viewport), + Camera::new_2d(frame_input.viewport), line.into_iter().chain(&rectangle).chain(&circle), &[], ); diff --git a/examples/text/src/main.rs b/examples/text/src/main.rs index eb3fa25ef..feaa748b2 100644 --- a/examples/text/src/main.rs +++ b/examples/text/src/main.rs @@ -78,7 +78,7 @@ pub fn main() { frame_input .screen() .clear(ClearState::color_and_depth(1.0, 1.0, 1.0, 1.0, 1.0)) - .render(&camera, &[&text0, &text1, &text2], &[]); + .render(&camera, [&text0, &text1, &text2], &[]); FrameOutput::default() }); } diff --git a/src/core.rs b/src/core.rs index 9a1860b70..6a9ddcb30 100644 --- a/src/core.rs +++ b/src/core.rs @@ -99,12 +99,7 @@ pub(crate) fn full_screen_vertex_shader_source() -> &'static str { mod data_type; use data_type::DataType; fn to_byte_slice(data: &[T]) -> &[u8] { - unsafe { - std::slice::from_raw_parts( - data.as_ptr() as *const _, - data.len() * std::mem::size_of::(), - ) - } + unsafe { std::slice::from_raw_parts(data.as_ptr() as *const _, std::mem::size_of_val(data)) } } fn from_byte_slice(data: &[u8]) -> &[T] { diff --git a/src/core/buffer.rs b/src/core/buffer.rs index db9831fa3..a54689328 100644 --- a/src/core/buffer.rs +++ b/src/core/buffer.rs @@ -94,7 +94,7 @@ impl Buffer { ); self.context.bind_buffer(crate::context::ARRAY_BUFFER, None); } - self.attribute_count = (offset as u32 + data.len() as u32).max(self.attribute_count); + self.attribute_count = (offset + data.len() as u32).max(self.attribute_count); } pub fn attribute_count(&self) -> u32 { diff --git a/src/core/buffer/element_buffer.rs b/src/core/buffer/element_buffer.rs index 789d5b530..67e8e5d9c 100644 --- a/src/core/buffer/element_buffer.rs +++ b/src/core/buffer/element_buffer.rs @@ -95,7 +95,7 @@ impl ElementBuffer { self.context .bind_buffer(crate::context::ELEMENT_ARRAY_BUFFER, None); } - self.count = (offset as u32 + indices.len() as u32).max(self.count); + self.count = (offset + indices.len() as u32).max(self.count); } /// diff --git a/src/core/data_type.rs b/src/core/data_type.rs index 48172eb7c..3cfe7069a 100644 --- a/src/core/data_type.rs +++ b/src/core/data_type.rs @@ -229,7 +229,7 @@ pub trait DataType: std::fmt::Debug + Clone { fn send_uniform(context: &Context, location: &UniformLocation, data: &[Self]); } -impl DataType for &T { +impl DataType for &T { fn internal_format() -> u32 { T::internal_format() } diff --git a/src/core/program.rs b/src/core/program.rs index 3e86e7cb1..3ca6a5d66 100644 --- a/src/core/program.rs +++ b/src/core/program.rs @@ -501,7 +501,7 @@ impl Program { viewport, element_buffer, 0, - element_buffer.count() as u32, + element_buffer.count(), ) } @@ -561,7 +561,7 @@ impl Program { viewport, element_buffer, 0, - element_buffer.count() as u32, + element_buffer.count(), instance_count, ) } diff --git a/src/core/texture/texture2d.rs b/src/core/texture/texture2d.rs index cc8004ab7..7c1a3a3ce 100644 --- a/src/core/texture/texture2d.rs +++ b/src/core/texture/texture2d.rs @@ -181,7 +181,9 @@ impl Texture2D { /// using low-level context calls inside the callback. /// This function binds the texture and sets the parameters before calling the callback and generates mip maps afterwards. /// - /// **Note:** This function is unsafe and should only be used in special cases, + /// # Safety + /// + /// This function is unsafe and should only be used in special cases, /// for example when you have an uncommon source of data or the data is in a special format like sRGB. /// pub unsafe fn new_unchecked( diff --git a/src/core/texture/texture2d_array.rs b/src/core/texture/texture2d_array.rs index f6d991073..91dade118 100644 --- a/src/core/texture/texture2d_array.rs +++ b/src/core/texture/texture2d_array.rs @@ -25,7 +25,7 @@ impl Texture2DArray { /// pub fn new(context: &Context, cpu_textures: &[&CpuTexture]) -> Self { let cpu_texture = cpu_textures - .get(0) + .first() .expect("Expect at least one texture in a texture array"); match &cpu_texture.data { TextureData::RU8(_) => Self::new_with_data( @@ -304,7 +304,9 @@ impl Texture2DArray { /// using low-level context calls inside the callback. /// This function binds the texture and sets the parameters before calling the callback and generates mip maps afterwards. /// - /// **Note:** This function is unsafe and should only be used in special cases, + /// # Safety + /// + /// This function is unsafe and should only be used in special cases, /// for example when you have an uncommon source of data or the data is in a special format like sRGB. /// pub unsafe fn new_unchecked( diff --git a/src/core/texture/texture3d.rs b/src/core/texture/texture3d.rs index 07afc0d4b..a8502ffca 100644 --- a/src/core/texture/texture3d.rs +++ b/src/core/texture/texture3d.rs @@ -173,7 +173,9 @@ impl Texture3D { /// using low-level context calls inside the callback. /// This function binds the texture and sets the parameters before calling the callback and generates mip maps afterwards. /// - /// **Note:** This function is unsafe and should only be used in special cases, + /// # Safety + /// + /// This function is unsafe and should only be used in special cases, /// for example when you have an uncommon source of data or the data is in a special format like sRGB. /// pub unsafe fn new_unchecked( diff --git a/src/core/texture/texture_cube_map.rs b/src/core/texture/texture_cube_map.rs index eac641ec0..08ff855e9 100644 --- a/src/core/texture/texture_cube_map.rs +++ b/src/core/texture/texture_cube_map.rs @@ -466,7 +466,7 @@ impl TextureCubeMap { let program = Program::from_source( context, full_screen_vertex_shader_source(), - &fragment_shader_source, + fragment_shader_source, ) .expect("Failed compiling shader"); @@ -558,7 +558,9 @@ impl TextureCubeMap { /// using low-level context calls inside the callback. /// This function binds the texture and sets the parameters before calling the callback and generates mip maps afterwards. /// - /// **Note:** This function is unsafe and should only be used in special cases, + /// # Safety + /// + /// This function is unsafe and should only be used in special cases, /// for example when you have an uncommon source of data or the data is in a special format like sRGB. /// pub unsafe fn new_unchecked( diff --git a/src/core/uniform.rs b/src/core/uniform.rs index 6ff31b730..367f2b30a 100644 --- a/src/core/uniform.rs +++ b/src/core/uniform.rs @@ -25,4 +25,4 @@ impl UniformDataType for Matrix2 {} impl UniformDataType for Matrix3 {} impl UniformDataType for Matrix4 {} -impl UniformDataType for &T {} +impl UniformDataType for &T {} diff --git a/src/gui/egui_gui.rs b/src/gui/egui_gui.rs index 7afbe0187..6b2cd5882 100644 --- a/src/gui/egui_gui.rs +++ b/src/gui/egui_gui.rs @@ -59,20 +59,19 @@ impl GUI { device_pixel_ratio: f32, callback: impl FnOnce(&egui::Context), ) -> bool { - self.egui_context - .set_pixels_per_point(device_pixel_ratio as f32); + self.egui_context.set_pixels_per_point(device_pixel_ratio); self.viewport = viewport; let egui_input = egui::RawInput { screen_rect: Some(egui::Rect { min: egui::Pos2 { - x: viewport.x as f32 / device_pixel_ratio as f32, - y: viewport.y as f32 / device_pixel_ratio as f32, + x: viewport.x as f32 / device_pixel_ratio, + y: viewport.y as f32 / device_pixel_ratio, }, max: egui::Pos2 { - x: viewport.x as f32 / device_pixel_ratio as f32 - + viewport.width as f32 / device_pixel_ratio as f32, - y: viewport.y as f32 / device_pixel_ratio as f32 - + viewport.height as f32 / device_pixel_ratio as f32, + x: viewport.x as f32 / device_pixel_ratio + + viewport.width as f32 / device_pixel_ratio, + y: viewport.y as f32 / device_pixel_ratio + + viewport.height as f32 / device_pixel_ratio, }, }), time: Some(accumulated_time_in_ms * 0.001), @@ -123,9 +122,8 @@ impl GUI { if !handled { Some(egui::Event::PointerButton { pos: egui::Pos2 { - x: position.x / device_pixel_ratio as f32, - y: (viewport.height as f32 - position.y) - / device_pixel_ratio as f32, + x: position.x / device_pixel_ratio, + y: (viewport.height as f32 - position.y) / device_pixel_ratio, }, button: button.into(), pressed: true, @@ -144,9 +142,8 @@ impl GUI { if !handled { Some(egui::Event::PointerButton { pos: egui::Pos2 { - x: position.x / device_pixel_ratio as f32, - y: (viewport.height as f32 - position.y) - / device_pixel_ratio as f32, + x: position.x / device_pixel_ratio, + y: (viewport.height as f32 - position.y) / device_pixel_ratio, }, button: button.into(), pressed: false, @@ -161,9 +158,8 @@ impl GUI { } => { if !handled { Some(egui::Event::PointerMoved(egui::Pos2 { - x: position.x / device_pixel_ratio as f32, - y: (viewport.height as f32 - position.y) - / device_pixel_ratio as f32, + x: position.x / device_pixel_ratio, + y: (viewport.height as f32 - position.y) / device_pixel_ratio, })) } else { None @@ -179,7 +175,7 @@ impl GUI { } => { if !handled { Some(egui::Event::MouseWheel { - delta: egui::Vec2::new(delta.0 as f32, delta.1 as f32), + delta: egui::Vec2::new(delta.0, delta.1), unit: egui::MouseWheelUnit::Point, modifiers: modifiers.into(), }) diff --git a/src/renderer.rs b/src/renderer.rs index 096c50bad..4c4da06f5 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -402,7 +402,7 @@ fn combine_ids( let mut id = geometry.0.to_le_bytes().to_vec(); id.extend(effect_material.0.to_le_bytes()); id.extend(lights.map(|l| l.0)); - return id; + id } /// diff --git a/src/renderer/control/control2d.rs b/src/renderer/control/control2d.rs index 5f528bede..7cdfdd022 100644 --- a/src/renderer/control/control2d.rs +++ b/src/renderer/control/control2d.rs @@ -48,10 +48,8 @@ impl Control2D { handled, .. } => { - if !*handled && delta.0.abs() + delta.1.abs() > std::f32::EPSILON { - if delta.0.abs() < std::f32::EPSILON - && delta.1.fract().abs() > std::f32::EPSILON - { + if !*handled && delta.0.abs() + delta.1.abs() > f32::EPSILON { + if delta.0.abs() < f32::EPSILON && delta.1.fract().abs() > f32::EPSILON { self.zoom(camera, delta.1, *position, 0.005); } else { self.pan(camera, *delta, device_pixel_ratio); diff --git a/src/renderer/control/first_person_control.rs b/src/renderer/control/first_person_control.rs index 6afca6bf3..1144f2c9c 100644 --- a/src/renderer/control/first_person_control.rs +++ b/src/renderer/control/first_person_control.rs @@ -30,13 +30,11 @@ impl FirstPersonControl { handled, .. } => { - if !*handled { - if Some(MouseButton::Left) == *button { - camera.yaw(radians(delta.0 * std::f32::consts::PI / 1800.0)); - camera.pitch(radians(delta.1 * std::f32::consts::PI / 1800.0)); - *handled = true; - change = true; - } + if !*handled && Some(MouseButton::Left) == *button { + camera.yaw(radians(delta.0 * std::f32::consts::PI / 1800.0)); + camera.pitch(radians(delta.1 * std::f32::consts::PI / 1800.0)); + *handled = true; + change = true; } } Event::MouseWheel { delta, handled, .. } => { diff --git a/src/renderer/control/free_orbit_control.rs b/src/renderer/control/free_orbit_control.rs index 8828ba85b..0c154258c 100644 --- a/src/renderer/control/free_orbit_control.rs +++ b/src/renderer/control/free_orbit_control.rs @@ -38,13 +38,11 @@ impl FreeOrbitControl { handled, .. } => { - if !*handled { - if Some(MouseButton::Left) == *button { - let speed = 0.01 * self.target.distance(camera.position()) + 0.001; - camera.rotate_around(self.target, speed * delta.0, speed * delta.1); - *handled = true; - change = true; - } + if !*handled && Some(MouseButton::Left) == *button { + let speed = 0.01 * self.target.distance(camera.position()) + 0.001; + camera.rotate_around(self.target, speed * delta.0, speed * delta.1); + *handled = true; + change = true; } } Event::MouseWheel { delta, handled, .. } => { diff --git a/src/renderer/control/orbit_control.rs b/src/renderer/control/orbit_control.rs index be20e5459..5efc84eae 100644 --- a/src/renderer/control/orbit_control.rs +++ b/src/renderer/control/orbit_control.rs @@ -38,17 +38,15 @@ impl OrbitControl { handled, .. } => { - if !*handled { - if Some(MouseButton::Left) == *button { - let speed = 0.01; - camera.rotate_around_with_fixed_up( - self.target, - speed * delta.0, - speed * delta.1, - ); - *handled = true; - change = true; - } + if !*handled && Some(MouseButton::Left) == *button { + let speed = 0.01; + camera.rotate_around_with_fixed_up( + self.target, + speed * delta.0, + speed * delta.1, + ); + *handled = true; + change = true; } } Event::MouseWheel { delta, handled, .. } => { diff --git a/src/renderer/geometry/bounding_box.rs b/src/renderer/geometry/bounding_box.rs index 56cbd1396..33bb639ac 100644 --- a/src/renderer/geometry/bounding_box.rs +++ b/src/renderer/geometry/bounding_box.rs @@ -30,7 +30,7 @@ impl BoundingBox { let max = aabb.max(); let min = aabb.min(); let size = aabb.size(); - let translations = vec![ + let translations = [ min, vec3(min.x, max.y, max.z), vec3(min.x, min.y, max.z), diff --git a/src/renderer/light/environment.rs b/src/renderer/light/environment.rs index 3eb0ebcba..984a933f8 100644 --- a/src/renderer/light/environment.rs +++ b/src/renderer/light/environment.rs @@ -63,7 +63,7 @@ impl Environment { environment_map, side, }, - &Camera::new_2d(viewport), + Camera::new_2d(viewport), &[], ); } @@ -100,7 +100,7 @@ impl Environment { mip, max_mip_levels, }, - &Camera::new_2d(viewport), + Camera::new_2d(viewport), &[], ); } @@ -124,7 +124,7 @@ impl Environment { .clear(ClearState::default()) .apply_screen_material( &BrdfMaterial { lighting_model }, - &Camera::new_2d(viewport), + Camera::new_2d(viewport), &[], ); diff --git a/src/window.rs b/src/window.rs index e32d99c4d..21d467a83 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,7 +1,6 @@ //! //! Window, event handling and context creation for easy setup. -//! * Can be avoided fully by setting up a window, event handling etc. and creating a [Context](crate::core::Context) -//! from a [glow](https://crates.io/crates/glow) OpenGL/WebGL context. +//! * Can be avoided fully by setting up a window, event handling etc. and creating a [Context](crate::core::Context) from a [glow](https://crates.io/crates/glow) OpenGL/WebGL context. //! * If full control over the window and event handling, but not the context creation, is desired, use a [WindowedContext] or [HeadlessContext]. //! * Finally, for an easy setup, use [Window::new] or [Window::from_winit_window], the latter will provide full control over the creation of the window. //! diff --git a/src/window/headless.rs b/src/window/headless.rs index 7a08cae7b..458889395 100644 --- a/src/window/headless.rs +++ b/src/window/headless.rs @@ -73,7 +73,7 @@ fn build_context_headless( el: &EventLoop<()>, ) -> Result, CreationError> { let size_one = PhysicalSize::new(1, 1); - cb.build_headless(&el, size_one) + cb.build_headless(el, size_one) } #[cfg(target_os = "linux")] From 3cf57f826cf8d80734bd3be0a76343f932e0714e Mon Sep 17 00:00:00 2001 From: Asger Nyman Christiansen Date: Fri, 28 Mar 2025 15:25:35 +0100 Subject: [PATCH 2/2] Add clippy to CI --- .github/workflows/rust.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8a56bc2f9..99aa42e08 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -12,9 +12,11 @@ on: env: CARGO_TERM_COLOR: always RUST_BACKTRACE: 1 + RUSTFLAGS: "-Dwarnings" jobs: desktop: + name: Check Desktop runs-on: ${{ matrix.os }} strategy: matrix: @@ -22,7 +24,7 @@ jobs: rust: [stable] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Install Rust Toolchain uses: actions-rs/toolchain@v1 @@ -41,16 +43,22 @@ jobs: command: check args: --examples --all-features - - name: Rustfmt + - name: Formatting uses: actions-rs/cargo@v1 with: command: fmt - args: -- --check + args: --all -- --check + + - name: Clippy + uses: actions-rs/cargo@v1 + with: + command: clippy web: + name: Check Web runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Install Rust Toolchain uses: actions-rs/toolchain@v1