Replace map + unwrap_or(false) with is_some_and (#17067)

# Objective

The `my_option.map(|inner| inner.is_whatever).unwrap_or(false)` pattern
is fragile and ugly.

Replace it with `is_some_and` everywhere.
This commit is contained in:
ickshonpe 2024-12-31 20:28:02 +00:00 committed by GitHub
parent c73daea341
commit 7a5a734452
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 26 additions and 47 deletions

View File

@ -126,8 +126,7 @@ impl Animatable for bool {
fn blend(inputs: impl Iterator<Item = BlendInput<Self>>) -> Self { fn blend(inputs: impl Iterator<Item = BlendInput<Self>>) -> Self {
inputs inputs
.max_by_key(|x| FloatOrd(x.weight)) .max_by_key(|x| FloatOrd(x.weight))
.map(|input| input.value) .is_some_and(|input| input.value)
.unwrap_or(false)
} }
} }

View File

@ -254,8 +254,7 @@ impl PluginGroupBuilder {
pub fn enabled<T: Plugin>(&self) -> bool { pub fn enabled<T: Plugin>(&self) -> bool {
self.plugins self.plugins
.get(&TypeId::of::<T>()) .get(&TypeId::of::<T>())
.map(|e| e.enabled) .is_some_and(|e| e.enabled)
.unwrap_or(false)
} }
/// Finds the index of a target [`Plugin`]. /// Finds the index of a target [`Plugin`].

View File

@ -54,10 +54,7 @@ pub(crate) fn get_asset_path(root: &Path, absolute_path: &Path) -> (PathBuf, boo
root.display() root.display()
) )
}); });
let is_meta = relative_path let is_meta = relative_path.extension().is_some_and(|e| e == "meta");
.extension()
.map(|e| e == "meta")
.unwrap_or(false);
let asset_path = if is_meta { let asset_path = if is_meta {
relative_path.with_extension("") relative_path.with_extension("")
} else { } else {

View File

@ -1877,12 +1877,11 @@ impl World {
self.storages self.storages
.resources .resources
.get(component_id) .get(component_id)
.and_then(|resource| { .is_some_and(|resource| {
resource resource.get_ticks().is_some_and(|ticks| {
.get_ticks() ticks.is_added(self.last_change_tick(), self.read_change_tick())
.map(|ticks| ticks.is_added(self.last_change_tick(), self.read_change_tick())) })
}) })
.unwrap_or(false)
} }
/// Returns `true` if a resource of type `R` exists and was modified since the world's /// Returns `true` if a resource of type `R` exists and was modified since the world's
@ -1895,8 +1894,7 @@ impl World {
pub fn is_resource_changed<R: Resource>(&self) -> bool { pub fn is_resource_changed<R: Resource>(&self) -> bool {
self.components self.components
.get_resource_id(TypeId::of::<R>()) .get_resource_id(TypeId::of::<R>())
.map(|component_id| self.is_resource_changed_by_id(component_id)) .is_some_and(|component_id| self.is_resource_changed_by_id(component_id))
.unwrap_or(false)
} }
/// Returns `true` if a resource with id `component_id` exists and was modified since the world's /// Returns `true` if a resource with id `component_id` exists and was modified since the world's
@ -1910,12 +1908,11 @@ impl World {
self.storages self.storages
.resources .resources
.get(component_id) .get(component_id)
.and_then(|resource| { .is_some_and(|resource| {
resource resource.get_ticks().is_some_and(|ticks| {
.get_ticks() ticks.is_changed(self.last_change_tick(), self.read_change_tick())
.map(|ticks| ticks.is_changed(self.last_change_tick(), self.read_change_tick())) })
}) })
.unwrap_or(false)
} }
/// Retrieves the change ticks for the given resource. /// Retrieves the change ticks for the given resource.

View File

@ -235,8 +235,7 @@ impl Location {
camera camera
.logical_viewport_rect() .logical_viewport_rect()
.map(|rect| rect.contains(self.position)) .is_some_and(|rect| rect.contains(self.position))
.unwrap_or(false)
} }
} }

View File

@ -352,8 +352,7 @@ impl dyn PartialReflect {
#[inline] #[inline]
pub fn represents<T: Reflect + TypePath>(&self) -> bool { pub fn represents<T: Reflect + TypePath>(&self) -> bool {
self.get_represented_type_info() self.get_represented_type_info()
.map(|t| t.type_path() == T::type_path()) .is_some_and(|t| t.type_path() == T::type_path())
.unwrap_or(false)
} }
/// Downcasts the value to type `T`, consuming the trait object. /// Downcasts the value to type `T`, consuming the trait object.

View File

@ -48,10 +48,7 @@ impl<P: ReflectSerializerProcessor> Serialize for StructSerializer<'_, P> {
)?; )?;
for (index, value) in self.struct_value.iter_fields().enumerate() { for (index, value) in self.struct_value.iter_fields().enumerate() {
if serialization_data if serialization_data.is_some_and(|data| data.is_field_skipped(index)) {
.map(|data| data.is_field_skipped(index))
.unwrap_or(false)
{
continue; continue;
} }
let key = struct_info.field_at(index).unwrap().name(); let key = struct_info.field_at(index).unwrap().name();

View File

@ -57,10 +57,7 @@ impl<P: ReflectSerializerProcessor> Serialize for TupleStructSerializer<'_, P> {
)?; )?;
for (index, value) in self.tuple_struct.iter_fields().enumerate() { for (index, value) in self.tuple_struct.iter_fields().enumerate() {
if serialization_data if serialization_data.is_some_and(|data| data.is_field_skipped(index)) {
.map(|data| data.is_field_skipped(index))
.unwrap_or(false)
{
continue; continue;
} }
state.serialize_field(&TypedReflectSerializer::new_internal( state.serialize_field(&TypedReflectSerializer::new_internal(

View File

@ -98,8 +98,7 @@ fn sprite_picking(
camera camera
.target .target
.normalize(primary_window) .normalize(primary_window)
.map(|x| x == location.target) .is_some_and(|x| x == location.target)
.unwrap_or(false)
}) })
else { else {
continue; continue;

View File

@ -94,8 +94,7 @@ impl RelativeCursorPosition {
/// A helper function to check if the mouse is over the node /// A helper function to check if the mouse is over the node
pub fn mouse_over(&self) -> bool { pub fn mouse_over(&self) -> bool {
self.normalized self.normalized
.map(|position| self.normalized_visible_node_rect.contains(position)) .is_some_and(|position| self.normalized_visible_node_rect.contains(position))
.unwrap_or(false)
} }
} }
@ -274,15 +273,13 @@ pub fn ui_focus_system(
}; };
let contains_cursor = relative_cursor_position_component.mouse_over() let contains_cursor = relative_cursor_position_component.mouse_over()
&& cursor_position && cursor_position.is_some_and(|point| {
.map(|point| { pick_rounded_rect(
pick_rounded_rect( *point - node_rect.center(),
*point - node_rect.center(), node_rect.size(),
node_rect.size(), node.node.border_radius,
node.node.border_radius, )
) });
})
.unwrap_or(false);
// Save the relative cursor position to the correct component // Save the relative cursor position to the correct component
if let Some(mut node_relative_cursor_position_component) = node.relative_cursor_position if let Some(mut node_relative_cursor_position_component) = node.relative_cursor_position

View File

@ -215,8 +215,7 @@ pub fn ui_layout_system(
|| node.is_changed() || node.is_changed()
|| content_size || content_size
.as_ref() .as_ref()
.map(|c| c.is_changed() || c.measure.is_some()) .is_some_and(|c| c.is_changed() || c.measure.is_some())
.unwrap_or(false)
{ {
let layout_context = LayoutContext::new( let layout_context = LayoutContext::new(
camera.scale_factor, camera.scale_factor,