Use new let-else syntax where possible (#6463)

# Objective

Let-else syntax is now stable!

## Solution

Use it where possible!
This commit is contained in:
Carter Anderson 2022-11-04 21:32:09 +00:00
parent 1bd3d85769
commit e5905379de
15 changed files with 21 additions and 49 deletions

View File

@ -970,9 +970,7 @@ pub(crate) fn assign_lights_to_clusters(
continue; continue;
} }
let screen_size = if let Some(screen_size) = camera.physical_viewport_size() { let Some(screen_size) = camera.physical_viewport_size() else {
screen_size
} else {
clusters.clear(); clusters.clear();
continue; continue;
}; };

View File

@ -137,11 +137,10 @@ impl ReflectTraits {
// Handles `#[reflect( Hash, Default, ... )]` // Handles `#[reflect( Hash, Default, ... )]`
NestedMeta::Meta(Meta::Path(path)) => { NestedMeta::Meta(Meta::Path(path)) => {
// Get the first ident in the path (hopefully the path only contains one and not `std::hash::Hash`) // 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() { let Some(segment) = path.segments.iter().next() else {
&segment.ident
} else {
continue; continue;
}; };
let ident = &segment.ident;
let ident_name = ident.to_string(); let ident_name = ident.to_string();
// Track the span where the trait is implemented for future errors // Track the span where the trait is implemented for future errors
@ -172,12 +171,12 @@ impl ReflectTraits {
// Handles `#[reflect( Hash(custom_hash_fn) )]` // Handles `#[reflect( Hash(custom_hash_fn) )]`
NestedMeta::Meta(Meta::List(list)) => { NestedMeta::Meta(Meta::List(list)) => {
// Get the first ident in the path (hopefully the path only contains one and not `std::hash::Hash`) // 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() { let Some(segment) = list.path.segments.iter().next() else {
segment.ident.to_string()
} else {
continue; continue;
}; };
let ident = segment.ident.to_string();
// Track the span where the trait is implemented for future errors // Track the span where the trait is implemented for future errors
let span = ident.span(); let span = ident.span();

View File

@ -24,9 +24,7 @@ pub(crate) fn type_uuid_derive(input: proc_macro::TokenStream) -> proc_macro::To
let mut uuid = None; let mut uuid = None;
for attribute in ast.attrs.iter().filter_map(|attr| attr.parse_meta().ok()) { for attribute in ast.attrs.iter().filter_map(|attr| attr.parse_meta().ok()) {
let name_value = if let Meta::NameValue(name_value) = attribute { let Meta::NameValue(name_value) = attribute else {
name_value
} else {
continue; continue;
}; };

View File

@ -26,9 +26,7 @@ pub fn enum_hash<TEnum: Enum>(value: &TEnum) -> Option<u64> {
#[inline] #[inline]
pub fn enum_partial_eq<TEnum: Enum>(a: &TEnum, b: &dyn Reflect) -> Option<bool> { pub fn enum_partial_eq<TEnum: Enum>(a: &TEnum, b: &dyn Reflect) -> Option<bool> {
// Both enums? // Both enums?
let b = if let ReflectRef::Enum(e) = b.reflect_ref() { let ReflectRef::Enum(b) = b.reflect_ref() else {
e
} else {
return Some(false); return Some(false);
}; };

View File

@ -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. /// Returns [`None`] if the comparison couldn't even be performed.
#[inline] #[inline]
pub fn list_partial_eq<L: List>(a: &L, b: &dyn Reflect) -> Option<bool> { pub fn list_partial_eq<L: List>(a: &L, b: &dyn Reflect) -> Option<bool> {
let list = if let ReflectRef::List(list) = b.reflect_ref() { let ReflectRef::List(list) = b.reflect_ref() else {
list
} else {
return Some(false); return Some(false);
}; };

View File

@ -371,9 +371,7 @@ impl<'a> ExactSizeIterator for MapIter<'a> {}
/// Returns [`None`] if the comparison couldn't even be performed. /// Returns [`None`] if the comparison couldn't even be performed.
#[inline] #[inline]
pub fn map_partial_eq<M: Map>(a: &M, b: &dyn Reflect) -> Option<bool> { pub fn map_partial_eq<M: Map>(a: &M, b: &dyn Reflect) -> Option<bool> {
let map = if let ReflectRef::Map(map) = b.reflect_ref() { let ReflectRef::Map(map) = b.reflect_ref() else {
map
} else {
return Some(false); return Some(false);
}; };

View File

@ -480,9 +480,7 @@ impl Typed for DynamicStruct {
/// Returns [`None`] if the comparison couldn't even be performed. /// Returns [`None`] if the comparison couldn't even be performed.
#[inline] #[inline]
pub fn struct_partial_eq<S: Struct>(a: &S, b: &dyn Reflect) -> Option<bool> { 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() { let ReflectRef::Struct(struct_value) = b.reflect_ref() else {
struct_value
} else {
return Some(false); return Some(false);
}; };

View File

@ -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. /// Returns [`None`] if the comparison couldn't even be performed.
#[inline] #[inline]
pub fn tuple_partial_eq<T: Tuple>(a: &T, b: &dyn Reflect) -> Option<bool> { pub fn tuple_partial_eq<T: Tuple>(a: &T, b: &dyn Reflect) -> Option<bool> {
let b = if let ReflectRef::Tuple(tuple) = b.reflect_ref() { let ReflectRef::Tuple(b) = b.reflect_ref() else {
tuple
} else {
return Some(false); return Some(false);
}; };

View File

@ -381,9 +381,7 @@ impl Typed for DynamicTupleStruct {
/// Returns [`None`] if the comparison couldn't even be performed. /// Returns [`None`] if the comparison couldn't even be performed.
#[inline] #[inline]
pub fn tuple_struct_partial_eq<S: TupleStruct>(a: &S, b: &dyn Reflect) -> Option<bool> { 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() { let ReflectRef::TupleStruct(tuple_struct) = b.reflect_ref() else {
tuple_struct
} else {
return Some(false); return Some(false);
}; };

View File

@ -116,9 +116,7 @@ pub fn derive_as_bind_group(ast: syn::DeriveInput) -> Result<TokenStream> {
// Read field-level attributes // Read field-level attributes
for field in fields.iter() { for field in fields.iter() {
for attr in &field.attrs { for attr in &field.attrs {
let attr_ident = if let Some(ident) = attr.path.get_ident() { let Some(attr_ident) = attr.path.get_ident() else {
ident
} else {
continue; continue;
}; };

View File

@ -78,9 +78,7 @@ impl Node for CameraDriverNode {
continue; continue;
} }
let swap_chain_texture = if let Some(swap_chain_texture) = &window.swap_chain_texture { let Some(swap_chain_texture) = &window.swap_chain_texture else {
swap_chain_texture
} else {
continue; continue;
}; };

View File

@ -20,9 +20,7 @@ pub fn basis_buffer_to_image(
return Err(TextureError::InvalidData("Invalid header".to_string())); return Err(TextureError::InvalidData("Invalid header".to_string()));
} }
let image0_info = if let Some(image_info) = transcoder.image_info(buffer, 0) { let Some(image0_info) = transcoder.image_info(buffer, 0) else {
image_info
} else {
return Err(TextureError::InvalidData( return Err(TextureError::InvalidData(
"Failed to get image info".to_string(), "Failed to get image info".to_string(),
)); ));

View File

@ -54,10 +54,9 @@ impl Node for UiPassNode {
) -> Result<(), NodeRunError> { ) -> Result<(), NodeRunError> {
let input_view_entity = graph.get_input_entity(Self::IN_VIEW)?; let input_view_entity = graph.get_input_entity(Self::IN_VIEW)?;
let (transparent_phase, target, camera_ui) = let Ok((transparent_phase, target, camera_ui)) =
if let Ok(result) = self.ui_view_query.get_manual(world, input_view_entity) { self.ui_view_query.get_manual(world, input_view_entity)
result else {
} else {
return Ok(()); return Ok(());
}; };
if transparent_phase.items.is_empty() { if transparent_phase.items.is_empty() {

View File

@ -398,9 +398,7 @@ pub fn winit_runner_with(mut app: App) {
return; return;
}; };
let window = if let Some(window) = windows.get_mut(window_id) { let Some(window) = windows.get_mut(window_id) else {
window
} else {
// If we're here, this window was previously opened // If we're here, this window was previously opened
info!("Skipped event for closed window: {:?}", window_id); info!("Skipped event for closed window: {:?}", window_id);
return; return;

View File

@ -248,9 +248,7 @@ fn collision_system(
windows: Res<Windows>, windows: Res<Windows>,
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>, mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
) { ) {
let window = if let Some(window) = windows.get_primary() { let Some(window) = windows.get_primary() else {
window
} else {
return; return;
}; };