From c73daea341283eb4ce1125b6f479a8997d77e56c Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Tue, 31 Dec 2024 20:17:03 +0000 Subject: [PATCH] Replace `map` + `unwrap_or(true)` with `is_none_or` (#17070) # Objective Reduce all varieties of `my_maybe.map(|x| x.is_true).unwrap_or(true)` using `is_none_or`. --- crates/bevy_ecs/src/removal_detection.rs | 3 +-- crates/bevy_hierarchy/src/query_extension.rs | 4 ++-- crates/bevy_picking/src/mesh_picking/mod.rs | 5 +---- .../bevy_reflect/derive/src/container_attributes.rs | 10 ++-------- crates/bevy_scene/src/scene_spawner.rs | 6 +++--- crates/bevy_sprite/src/picking_backend.rs | 4 +--- crates/bevy_ui/src/render/mod.rs | 11 +++-------- 7 files changed, 13 insertions(+), 30 deletions(-) diff --git a/crates/bevy_ecs/src/removal_detection.rs b/crates/bevy_ecs/src/removal_detection.rs index 1aaaa01c95e69..c81a5cfa1eb2c 100644 --- a/crates/bevy_ecs/src/removal_detection.rs +++ b/crates/bevy_ecs/src/removal_detection.rs @@ -233,8 +233,7 @@ impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> { /// Returns `true` if there are no events available to read. pub fn is_empty(&self) -> bool { self.events() - .map(|events| self.reader.is_empty(events)) - .unwrap_or(true) + .is_none_or(|events| self.reader.is_empty(events)) } /// Consumes all available events. diff --git a/crates/bevy_hierarchy/src/query_extension.rs b/crates/bevy_hierarchy/src/query_extension.rs index 6396ddcfb756f..35ebaf8025c36 100644 --- a/crates/bevy_hierarchy/src/query_extension.rs +++ b/crates/bevy_hierarchy/src/query_extension.rs @@ -139,10 +139,10 @@ impl<'w, 's, D: QueryData, F: QueryFilter> HierarchyQueryExt<'w, 's, D, F> for Q { self.iter_descendants_depth_first(entity).filter(|entity| { self.get(*entity) + .ok() // These are leaf nodes if they have the `Children` component but it's empty - .map(|children| children.is_empty()) // Or if they don't have the `Children` component at all - .unwrap_or(true) + .is_none_or(|children| children.is_empty()) }) } diff --git a/crates/bevy_picking/src/mesh_picking/mod.rs b/crates/bevy_picking/src/mesh_picking/mod.rs index a848097a6854f..42671beb7b958 100644 --- a/crates/bevy_picking/src/mesh_picking/mod.rs +++ b/crates/bevy_picking/src/mesh_picking/mod.rs @@ -99,10 +99,7 @@ pub fn update_hits( let entity_layers = layers.get(entity).cloned().unwrap_or_default(); let render_layers_match = cam_layers.intersects(&entity_layers); - let is_pickable = pickables - .get(entity) - .map(|p| p.is_hoverable) - .unwrap_or(true); + let is_pickable = pickables.get(entity).ok().is_none_or(|p| p.is_hoverable); marker_requirement && render_layers_match && is_pickable }, diff --git a/crates/bevy_reflect/derive/src/container_attributes.rs b/crates/bevy_reflect/derive/src/container_attributes.rs index b134c571c0501..bdb94db06bc7e 100644 --- a/crates/bevy_reflect/derive/src/container_attributes.rs +++ b/crates/bevy_reflect/derive/src/container_attributes.rs @@ -89,10 +89,7 @@ pub(crate) struct FromReflectAttrs { impl FromReflectAttrs { /// Returns true if `FromReflect` should be automatically derived as part of the `Reflect` derive. pub fn should_auto_derive(&self) -> bool { - self.auto_derive - .as_ref() - .map(LitBool::value) - .unwrap_or(true) + self.auto_derive.as_ref().is_none_or(LitBool::value) } } @@ -112,10 +109,7 @@ pub(crate) struct TypePathAttrs { impl TypePathAttrs { /// Returns true if `TypePath` should be automatically derived as part of the `Reflect` derive. pub fn should_auto_derive(&self) -> bool { - self.auto_derive - .as_ref() - .map(LitBool::value) - .unwrap_or(true) + self.auto_derive.as_ref().is_none_or(LitBool::value) } } diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index edba01b40c2a8..7117542d83100 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -375,12 +375,12 @@ impl SceneSpawner { // the scene parent if !world .get_entity(entity) + .ok() // This will filter only the scene root entity, as all other from the // scene have a parent - .map(|entity| entity.contains::()) - // Default is true so that it won't run on an entity that wouldn't exist anymore + // Entities that wouldn't exist anymore are also skipped // this case shouldn't happen anyway - .unwrap_or(true) + .is_none_or(|entity| entity.contains::()) { world.entity_mut(parent).add_child(entity); } diff --git a/crates/bevy_sprite/src/picking_backend.rs b/crates/bevy_sprite/src/picking_backend.rs index bd57aaf202036..a88e15fdb9276 100644 --- a/crates/bevy_sprite/src/picking_backend.rs +++ b/crates/bevy_sprite/src/picking_backend.rs @@ -186,9 +186,7 @@ fn sprite_picking( }; blocked = cursor_in_valid_pixels_of_sprite - && picking_behavior - .map(|p| p.should_block_lower) - .unwrap_or(true); + && picking_behavior.is_none_or(|p| p.should_block_lower); cursor_in_valid_pixels_of_sprite.then(|| { let hit_pos_world = diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index f4fd9af02374e..84a458921c16b 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -702,14 +702,9 @@ pub fn extract_text_sections( rect, }); - if text_layout_info - .glyphs - .get(i + 1) - .map(|info| { - info.span_index != current_span || info.atlas_info.texture != atlas_info.texture - }) - .unwrap_or(true) - { + if text_layout_info.glyphs.get(i + 1).is_none_or(|info| { + info.span_index != current_span || info.atlas_info.texture != atlas_info.texture + }) { let id = commands.spawn(TemporaryRenderEntity).id(); extracted_uinodes.uinodes.insert(