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;
}
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;
};

View File

@ -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
@ -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();

View File

@ -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;
};

View File

@ -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);
};

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.
#[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);
};

View File

@ -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);
};

View File

@ -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);
};

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.
#[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);
};

View File

@ -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);
};

View File

@ -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;
};

View File

@ -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;
};

View File

@ -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(),
));

View File

@ -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() {

View File

@ -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;

View File

@ -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;
};