Skip to content

Commit

Permalink
Use new let-else syntax where possible (#6463)
Browse files Browse the repository at this point in the history
# Objective

Let-else syntax is now stable!

## Solution

Use it where possible!
  • Loading branch information
cart committed Nov 4, 2022
1 parent 1bd3d85 commit e590537
Show file tree
Hide file tree
Showing 15 changed files with 21 additions and 49 deletions.
4 changes: 1 addition & 3 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,9 +970,7 @@ pub(crate) fn assign_lights_to_clusters(
continue;
}

let screen_size = if let Some(screen_size) = camera.physical_viewport_size() {
screen_size
} else {
let Some(screen_size) = camera.physical_viewport_size() else {
clusters.clear();
continue;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ impl ReflectTraits {
// Handles `#[reflect( Hash, Default, ... )]`
NestedMeta::Meta(Meta::Path(path)) => {
// Get the first ident in the path (hopefully the path only contains one and not `std::hash::Hash`)
let ident = if let Some(segment) = path.segments.iter().next() {
&segment.ident
} else {
let Some(segment) = path.segments.iter().next() else {
continue;
};
let ident = &segment.ident;
let ident_name = ident.to_string();

// Track the span where the trait is implemented for future errors
Expand Down Expand Up @@ -172,12 +171,12 @@ impl ReflectTraits {
// Handles `#[reflect( Hash(custom_hash_fn) )]`
NestedMeta::Meta(Meta::List(list)) => {
// Get the first ident in the path (hopefully the path only contains one and not `std::hash::Hash`)
let ident = if let Some(segment) = list.path.segments.iter().next() {
segment.ident.to_string()
} else {
let Some(segment) = list.path.segments.iter().next() else {
continue;
};

let ident = segment.ident.to_string();

// Track the span where the trait is implemented for future errors
let span = ident.span();

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_reflect/bevy_reflect_derive/src/type_uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ pub(crate) fn type_uuid_derive(input: proc_macro::TokenStream) -> proc_macro::To

let mut uuid = None;
for attribute in ast.attrs.iter().filter_map(|attr| attr.parse_meta().ok()) {
let name_value = if let Meta::NameValue(name_value) = attribute {
name_value
} else {
let Meta::NameValue(name_value) = attribute else {
continue;
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_reflect/src/enums/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ pub fn enum_hash<TEnum: Enum>(value: &TEnum) -> Option<u64> {
#[inline]
pub fn enum_partial_eq<TEnum: Enum>(a: &TEnum, b: &dyn Reflect) -> Option<bool> {
// Both enums?
let b = if let ReflectRef::Enum(e) = b.reflect_ref() {
e
} else {
let ReflectRef::Enum(b) = b.reflect_ref() else {
return Some(false);
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ pub fn list_apply<L: List>(a: &mut L, b: &dyn Reflect) {
/// Returns [`None`] if the comparison couldn't even be performed.
#[inline]
pub fn list_partial_eq<L: List>(a: &L, b: &dyn Reflect) -> Option<bool> {
let list = if let ReflectRef::List(list) = b.reflect_ref() {
list
} else {
let ReflectRef::List(list) = b.reflect_ref() else {
return Some(false);
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,7 @@ impl<'a> ExactSizeIterator for MapIter<'a> {}
/// Returns [`None`] if the comparison couldn't even be performed.
#[inline]
pub fn map_partial_eq<M: Map>(a: &M, b: &dyn Reflect) -> Option<bool> {
let map = if let ReflectRef::Map(map) = b.reflect_ref() {
map
} else {
let ReflectRef::Map(map) = b.reflect_ref() else {
return Some(false);
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_reflect/src/struct_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,7 @@ impl Typed for DynamicStruct {
/// Returns [`None`] if the comparison couldn't even be performed.
#[inline]
pub fn struct_partial_eq<S: Struct>(a: &S, b: &dyn Reflect) -> Option<bool> {
let struct_value = if let ReflectRef::Struct(struct_value) = b.reflect_ref() {
struct_value
} else {
let ReflectRef::Struct(struct_value) = b.reflect_ref() else {
return Some(false);
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,7 @@ pub fn tuple_apply<T: Tuple>(a: &mut T, b: &dyn Reflect) {
/// Returns [`None`] if the comparison couldn't even be performed.
#[inline]
pub fn tuple_partial_eq<T: Tuple>(a: &T, b: &dyn Reflect) -> Option<bool> {
let b = if let ReflectRef::Tuple(tuple) = b.reflect_ref() {
tuple
} else {
let ReflectRef::Tuple(b) = b.reflect_ref() else {
return Some(false);
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_reflect/src/tuple_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,7 @@ impl Typed for DynamicTupleStruct {
/// Returns [`None`] if the comparison couldn't even be performed.
#[inline]
pub fn tuple_struct_partial_eq<S: TupleStruct>(a: &S, b: &dyn Reflect) -> Option<bool> {
let tuple_struct = if let ReflectRef::TupleStruct(tuple_struct) = b.reflect_ref() {
tuple_struct
} else {
let ReflectRef::TupleStruct(tuple_struct) = b.reflect_ref() else {
return Some(false);
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_render/macros/src/as_bind_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ pub fn derive_as_bind_group(ast: syn::DeriveInput) -> Result<TokenStream> {
// Read field-level attributes
for field in fields.iter() {
for attr in &field.attrs {
let attr_ident = if let Some(ident) = attr.path.get_ident() {
ident
} else {
let Some(attr_ident) = attr.path.get_ident() else {
continue;
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_render/src/camera/camera_driver_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ impl Node for CameraDriverNode {
continue;
}

let swap_chain_texture = if let Some(swap_chain_texture) = &window.swap_chain_texture {
swap_chain_texture
} else {
let Some(swap_chain_texture) = &window.swap_chain_texture else {
continue;
};

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_render/src/texture/basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ pub fn basis_buffer_to_image(
return Err(TextureError::InvalidData("Invalid header".to_string()));
}

let image0_info = if let Some(image_info) = transcoder.image_info(buffer, 0) {
image_info
} else {
let Some(image0_info) = transcoder.image_info(buffer, 0) else {
return Err(TextureError::InvalidData(
"Failed to get image info".to_string(),
));
Expand Down
7 changes: 3 additions & 4 deletions crates/bevy_ui/src/render/render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ impl Node for UiPassNode {
) -> Result<(), NodeRunError> {
let input_view_entity = graph.get_input_entity(Self::IN_VIEW)?;

let (transparent_phase, target, camera_ui) =
if let Ok(result) = self.ui_view_query.get_manual(world, input_view_entity) {
result
} else {
let Ok((transparent_phase, target, camera_ui)) =
self.ui_view_query.get_manual(world, input_view_entity)
else {
return Ok(());
};
if transparent_phase.items.is_empty() {
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,7 @@ pub fn winit_runner_with(mut app: App) {
return;
};

let window = if let Some(window) = windows.get_mut(window_id) {
window
} else {
let Some(window) = windows.get_mut(window_id) else {
// If we're here, this window was previously opened
info!("Skipped event for closed window: {:?}", window_id);
return;
Expand Down
4 changes: 1 addition & 3 deletions examples/games/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ fn collision_system(
windows: Res<Windows>,
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
) {
let window = if let Some(window) = windows.get_primary() {
window
} else {
let Some(window) = windows.get_primary() else {
return;
};

Expand Down

0 comments on commit e590537

Please sign in to comment.