From 0e9f6e92ea509c1a285d8c85a828674f5ed32b84 Mon Sep 17 00:00:00 2001 From: Kanabenki Date: Tue, 28 Nov 2023 05:15:27 +0100 Subject: [PATCH] Add `clippy::manual_let_else` at warn level to lints (#10684) # Objective Related to #10612. Enable the [`clippy::manual_let_else`](https://rust-lang.github.io/rust-clippy/master/#manual_let_else) lint as a warning. The `let else` form seems more idiomatic to me than a `match`/`if else` that either match a pattern or diverge, and from the clippy doc, the lint doesn't seem to have any possible false positive. ## Solution Add the lint as warning in `Cargo.toml`, refactor places where the lint triggers. --- Cargo.toml | 1 + crates/bevy_asset/src/reflect.rs | 7 ++- crates/bevy_core_pipeline/src/bloom/mod.rs | 10 ++-- .../src/contrast_adaptive_sharpening/mod.rs | 10 ++-- .../src/core_2d/main_pass_2d_node.rs | 13 +++-- crates/bevy_core_pipeline/src/core_2d/mod.rs | 5 +- crates/bevy_core_pipeline/src/core_3d/mod.rs | 5 +- crates/bevy_core_pipeline/src/fxaa/mod.rs | 10 ++-- crates/bevy_core_pipeline/src/skybox/mod.rs | 10 ++-- .../src/tonemapping/node.rs | 5 +- .../bevy_core_pipeline/src/upscaling/node.rs | 5 +- crates/bevy_ecs/src/query/iter.rs | 5 +- .../bevy_ecs/src/world/unsafe_world_cell.rs | 5 +- crates/bevy_pbr/src/lib.rs | 10 ++-- crates/bevy_pbr/src/light.rs | 5 +- .../bevy_reflect_derive/src/type_uuid.rs | 2 + crates/bevy_render/src/mesh/mesh/mod.rs | 53 ++++++++----------- crates/bevy_ui/src/lib.rs | 5 +- crates/bevy_ui/src/render/mod.rs | 5 +- examples/shader/shader_instancing.rs | 5 +- examples/stress_tests/many_lights.rs | 5 +- 21 files changed, 77 insertions(+), 104 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e0fd2c16c20c..cc9c26056daa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ members = [ [workspace.lints.clippy] type_complexity = "allow" doc_markdown = "warn" +manual_let_else = "warn" undocumented_unsafe_blocks = "warn" redundant_else = "warn" match_same_arms = "warn" diff --git a/crates/bevy_asset/src/reflect.rs b/crates/bevy_asset/src/reflect.rs index 6e00323826de1..352007cb22362 100644 --- a/crates/bevy_asset/src/reflect.rs +++ b/crates/bevy_asset/src/reflect.rs @@ -268,13 +268,12 @@ mod tests { }; let handle = reflect_asset.add(&mut app.world, &value); - let strukt = match reflect_asset + let ReflectMut::Struct(strukt) = reflect_asset .get_mut(&mut app.world, handle) .unwrap() .reflect_mut() - { - ReflectMut::Struct(s) => s, - _ => unreachable!(), + else { + unreachable!(); }; strukt .field_mut("field") diff --git a/crates/bevy_core_pipeline/src/bloom/mod.rs b/crates/bevy_core_pipeline/src/bloom/mod.rs index 4c0d8ebf8f9cd..858a1af352e6e 100644 --- a/crates/bevy_core_pipeline/src/bloom/mod.rs +++ b/crates/bevy_core_pipeline/src/bloom/mod.rs @@ -55,9 +55,8 @@ impl Plugin for BloomPlugin { UniformComponentPlugin::::default(), )); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app @@ -101,9 +100,8 @@ impl Plugin for BloomPlugin { } fn finish(&self, app: &mut App) { - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app diff --git a/crates/bevy_core_pipeline/src/contrast_adaptive_sharpening/mod.rs b/crates/bevy_core_pipeline/src/contrast_adaptive_sharpening/mod.rs index 7efa3f5028cc7..f0f45f9876953 100644 --- a/crates/bevy_core_pipeline/src/contrast_adaptive_sharpening/mod.rs +++ b/crates/bevy_core_pipeline/src/contrast_adaptive_sharpening/mod.rs @@ -116,9 +116,8 @@ impl Plugin for CASPlugin { UniformComponentPlugin::::default(), )); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app .init_resource::>() @@ -155,9 +154,8 @@ impl Plugin for CASPlugin { } fn finish(&self, app: &mut App) { - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app.init_resource::(); } diff --git a/crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs b/crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs index 708193524b70a..252a8d974d2c4 100644 --- a/crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs +++ b/crates/bevy_core_pipeline/src/core_2d/main_pass_2d_node.rs @@ -46,13 +46,12 @@ impl Node for MainPass2dNode { world: &World, ) -> Result<(), NodeRunError> { let view_entity = graph.view_entity(); - let (camera, transparent_phase, target, camera_2d) = - if let Ok(result) = self.query.get_manual(world, view_entity) { - result - } else { - // no target - return Ok(()); - }; + let Ok((camera, transparent_phase, target, camera_2d)) = + self.query.get_manual(world, view_entity) + else { + // no target + return Ok(()); + }; { #[cfg(feature = "trace")] let _main_pass_2d = info_span!("main_pass_2d").entered(); diff --git a/crates/bevy_core_pipeline/src/core_2d/mod.rs b/crates/bevy_core_pipeline/src/core_2d/mod.rs index 530d48cde38a5..f9e9fb88dcb97 100644 --- a/crates/bevy_core_pipeline/src/core_2d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_2d/mod.rs @@ -48,9 +48,8 @@ impl Plugin for Core2dPlugin { app.register_type::() .add_plugins(ExtractComponentPlugin::::default()); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index f003ed6191f2b..1a38e5eb932c5 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -86,9 +86,8 @@ impl Plugin for Core3dPlugin { .add_plugins((SkyboxPlugin, ExtractComponentPlugin::::default())) .add_systems(PostUpdate, check_msaa); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app diff --git a/crates/bevy_core_pipeline/src/fxaa/mod.rs b/crates/bevy_core_pipeline/src/fxaa/mod.rs index a0ccc00fbad1c..26fd1bd13a013 100644 --- a/crates/bevy_core_pipeline/src/fxaa/mod.rs +++ b/crates/bevy_core_pipeline/src/fxaa/mod.rs @@ -89,9 +89,8 @@ impl Plugin for FxaaPlugin { app.register_type::(); app.add_plugins(ExtractComponentPlugin::::default()); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app .init_resource::>() @@ -117,9 +116,8 @@ impl Plugin for FxaaPlugin { } fn finish(&self, app: &mut App) { - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app.init_resource::(); } diff --git a/crates/bevy_core_pipeline/src/skybox/mod.rs b/crates/bevy_core_pipeline/src/skybox/mod.rs index cfb7d788e0ced..2d31cea138732 100644 --- a/crates/bevy_core_pipeline/src/skybox/mod.rs +++ b/crates/bevy_core_pipeline/src/skybox/mod.rs @@ -36,9 +36,8 @@ impl Plugin for SkyboxPlugin { app.add_plugins(ExtractComponentPlugin::::default()); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app @@ -53,9 +52,8 @@ impl Plugin for SkyboxPlugin { } fn finish(&self, app: &mut App) { - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; let render_device = render_app.world.resource::().clone(); diff --git a/crates/bevy_core_pipeline/src/tonemapping/node.rs b/crates/bevy_core_pipeline/src/tonemapping/node.rs index 1d1c95d970850..6d97af876a690 100644 --- a/crates/bevy_core_pipeline/src/tonemapping/node.rs +++ b/crates/bevy_core_pipeline/src/tonemapping/node.rs @@ -51,9 +51,8 @@ impl ViewNode for TonemappingNode { return Ok(()); } - let pipeline = match pipeline_cache.get_render_pipeline(view_tonemapping_pipeline.0) { - Some(pipeline) => pipeline, - None => return Ok(()), + let Some(pipeline) = pipeline_cache.get_render_pipeline(view_tonemapping_pipeline.0) else { + return Ok(()); }; let post_process = target.post_process_write(); diff --git a/crates/bevy_core_pipeline/src/upscaling/node.rs b/crates/bevy_core_pipeline/src/upscaling/node.rs index 536b2b9437515..a81eb96de84b1 100644 --- a/crates/bevy_core_pipeline/src/upscaling/node.rs +++ b/crates/bevy_core_pipeline/src/upscaling/node.rs @@ -67,9 +67,8 @@ impl ViewNode for UpscalingNode { } }; - let pipeline = match pipeline_cache.get_render_pipeline(upscaling_target.0) { - Some(pipeline) => pipeline, - None => return Ok(()), + let Some(pipeline) = pipeline_cache.get_render_pipeline(upscaling_target.0) else { + return Ok(()); }; let pass_descriptor = RenderPassDescriptor { diff --git a/crates/bevy_ecs/src/query/iter.rs b/crates/bevy_ecs/src/query/iter.rs index 2ed3433593e58..f5359a25fbf07 100644 --- a/crates/bevy_ecs/src/query/iter.rs +++ b/crates/bevy_ecs/src/query/iter.rs @@ -124,9 +124,8 @@ where unsafe fn fetch_next_aliased_unchecked(&mut self) -> Option> { for entity in self.entity_iter.by_ref() { let entity = *entity.borrow(); - let location = match self.entities.get(entity) { - Some(location) => location, - None => continue, + let Some(location) = self.entities.get(entity) else { + continue; }; if !self diff --git a/crates/bevy_ecs/src/world/unsafe_world_cell.rs b/crates/bevy_ecs/src/world/unsafe_world_cell.rs index f5dd64c72530b..4359e25a699c2 100644 --- a/crates/bevy_ecs/src/world/unsafe_world_cell.rs +++ b/crates/bevy_ecs/src/world/unsafe_world_cell.rs @@ -656,9 +656,8 @@ impl<'w> UnsafeEntityCell<'w> { /// - If you have a [`ComponentId`] instead of a [`TypeId`], consider using [`Self::contains_id`]. #[inline] pub fn contains_type_id(self, type_id: TypeId) -> bool { - let id = match self.world.components().get_id(type_id) { - Some(id) => id, - None => return false, + let Some(id) = self.world.components().get_id(type_id) else { + return false; }; self.contains_id(id) } diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index d50c4028888a4..ed1d3f79dfa08 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -329,9 +329,8 @@ impl Plugin for PbrPlugin { }, ); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; // Extract the required data from the main world @@ -365,9 +364,8 @@ impl Plugin for PbrPlugin { } fn finish(&self, app: &mut App) { - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; // Extract the required data from the main world diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 266d24746500e..a3c1b25441f42 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1203,9 +1203,8 @@ pub(crate) fn assign_lights_to_clusters( mut max_point_lights_warning_emitted: Local, render_device: Option>, ) { - let render_device = match render_device { - Some(render_device) => render_device, - None => return, + let Some(render_device) = render_device else { + return; }; global_lights.entities.clear(); diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs b/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs index 56372edd0ee2a..8affed7ca7c7a 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs @@ -8,6 +8,8 @@ use uuid::Uuid; pub(crate) fn type_uuid_derive(input: DeriveInput) -> syn::Result { let mut uuid = None; + + #[allow(clippy::manual_let_else)] for attribute in input .attrs .iter() diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 3ba1998333bf4..1a348702af39f 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -449,9 +449,8 @@ impl Mesh { indices.map(|i| values[i]).collect() } - let indices = match self.indices.take() { - Some(indices) => indices, - None => return, + let Some(indices) = self.indices.take() else { + return; }; for attributes in self.attributes.values_mut() { @@ -1173,38 +1172,32 @@ fn generate_tangents_for_mesh(mesh: &Mesh) -> Result, GenerateTang other => return Err(GenerateTangentsError::UnsupportedTopology(other)), }; - let positions = match mesh.attribute(Mesh::ATTRIBUTE_POSITION).ok_or( + let positions = mesh.attribute(Mesh::ATTRIBUTE_POSITION).ok_or( GenerateTangentsError::MissingVertexAttribute(Mesh::ATTRIBUTE_POSITION.name), - )? { - VertexAttributeValues::Float32x3(vertices) => vertices, - _ => { - return Err(GenerateTangentsError::InvalidVertexAttributeFormat( - Mesh::ATTRIBUTE_POSITION.name, - VertexFormat::Float32x3, - )) - } + )?; + let VertexAttributeValues::Float32x3(positions) = positions else { + return Err(GenerateTangentsError::InvalidVertexAttributeFormat( + Mesh::ATTRIBUTE_POSITION.name, + VertexFormat::Float32x3, + )); }; - let normals = match mesh.attribute(Mesh::ATTRIBUTE_NORMAL).ok_or( + let normals = mesh.attribute(Mesh::ATTRIBUTE_NORMAL).ok_or( GenerateTangentsError::MissingVertexAttribute(Mesh::ATTRIBUTE_NORMAL.name), - )? { - VertexAttributeValues::Float32x3(vertices) => vertices, - _ => { - return Err(GenerateTangentsError::InvalidVertexAttributeFormat( - Mesh::ATTRIBUTE_NORMAL.name, - VertexFormat::Float32x3, - )) - } + )?; + let VertexAttributeValues::Float32x3(normals) = normals else { + return Err(GenerateTangentsError::InvalidVertexAttributeFormat( + Mesh::ATTRIBUTE_NORMAL.name, + VertexFormat::Float32x3, + )); }; - let uvs = match mesh.attribute(Mesh::ATTRIBUTE_UV_0).ok_or( + let uvs = mesh.attribute(Mesh::ATTRIBUTE_UV_0).ok_or( GenerateTangentsError::MissingVertexAttribute(Mesh::ATTRIBUTE_UV_0.name), - )? { - VertexAttributeValues::Float32x2(vertices) => vertices, - _ => { - return Err(GenerateTangentsError::InvalidVertexAttributeFormat( - Mesh::ATTRIBUTE_UV_0.name, - VertexFormat::Float32x2, - )) - } + )?; + let VertexAttributeValues::Float32x2(uvs) = uvs else { + return Err(GenerateTangentsError::InvalidVertexAttributeFormat( + Mesh::ATTRIBUTE_UV_0.name, + VertexFormat::Float32x2, + )); }; let len = positions.len(); diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index f8b2597cc2088..c4d874dffd6ec 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -195,9 +195,8 @@ impl Plugin for UiPlugin { } fn finish(&self, app: &mut App) { - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app.init_resource::(); diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 97fb405e63659..b5cbb5046d700 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -65,9 +65,8 @@ pub enum RenderUiSystem { pub fn build_ui_render(app: &mut App) { load_internal_asset!(app, UI_SHADER_HANDLE, "ui.wgsl", Shader::from_wgsl); - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app diff --git a/examples/shader/shader_instancing.rs b/examples/shader/shader_instancing.rs index fc18bb63c73b7..3ff58c0b963e7 100644 --- a/examples/shader/shader_instancing.rs +++ b/examples/shader/shader_instancing.rs @@ -259,9 +259,8 @@ impl RenderCommand

for DrawMeshInstanced { let Some(mesh_instance) = render_mesh_instances.get(&item.entity()) else { return RenderCommandResult::Failure; }; - let gpu_mesh = match meshes.into_inner().get(mesh_instance.mesh_asset_id) { - Some(gpu_mesh) => gpu_mesh, - None => return RenderCommandResult::Failure, + let Some(gpu_mesh) = meshes.into_inner().get(mesh_instance.mesh_asset_id) else { + return RenderCommandResult::Failure; }; pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..)); diff --git a/examples/stress_tests/many_lights.rs b/examples/stress_tests/many_lights.rs index a64680a25b3f6..c36a04920c792 100644 --- a/examples/stress_tests/many_lights.rs +++ b/examples/stress_tests/many_lights.rs @@ -153,9 +153,8 @@ struct LogVisibleLights; impl Plugin for LogVisibleLights { fn build(&self, app: &mut App) { - let render_app = match app.get_sub_app_mut(RenderApp) { - Ok(render_app) => render_app, - Err(_) => return, + let Ok(render_app) = app.get_sub_app_mut(RenderApp) else { + return; }; render_app.add_systems(Render, print_visible_light_count.in_set(RenderSet::Prepare));