Skip to content

Commit

Permalink
Replace map + unwrap_or(true) with is_none_or (#17070)
Browse files Browse the repository at this point in the history
# Objective

Reduce all varieties of `my_maybe.map(|x| x.is_true).unwrap_or(true)`
using `is_none_or`.
  • Loading branch information
ickshonpe authored Dec 31, 2024
1 parent d502796 commit c73daea
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 30 deletions.
3 changes: 1 addition & 2 deletions crates/bevy_ecs/src/removal_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_hierarchy/src/query_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}

Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_picking/src/mesh_picking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
10 changes: 2 additions & 8 deletions crates/bevy_reflect/derive/src/container_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Parent>())
// 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::<Parent>())
{
world.entity_mut(parent).add_child(entity);
}
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_sprite/src/picking_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
11 changes: 3 additions & 8 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c73daea

Please sign in to comment.