Skip to content

Re-format let-else statements #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/camera/src/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ fn init<T: Component>(
camera: Query<&Transform, With<Camera3d>>,
objects: Query<(Entity, &Transform), Added<T>>,
) {
let Ok(cam_transform) = camera.get_single() else { return };
let Ok(cam_transform) = camera.get_single() else {
return;
};

for (entity, transform) in objects.iter() {
commands.entity(entity).insert(CameraDistance(
Expand All @@ -61,7 +63,9 @@ fn update(
camera: Query<&Transform, With<Camera3d>>,
mut objects: Query<(&Transform, &mut CameraDistance)>,
) {
let Ok(cam_transform) = camera.get_single() else { return };
let Ok(cam_transform) = camera.get_single() else {
return;
};

for (transform, mut camera_distance) in objects.iter_mut() {
let distance = cam_transform.translation.distance(transform.translation);
Expand Down
11 changes: 5 additions & 6 deletions crates/conf/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,11 @@ fn check_attr(

let arg = &closure.inputs[0];
let Pat::Type(arg) = arg else {
return Err(syn::Error::new_spanned(
attr,
"expected a closure with one argument",
)
.to_compile_error()
.into());
return Err(
syn::Error::new_spanned(attr, "expected a closure with one argument")
.to_compile_error()
.into(),
);
};

let arg_type = quote! { &#field_type };
Expand Down
4 changes: 2 additions & 2 deletions crates/connector/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ async fn create_game() -> (Network, u16) {

let port = {
let Incomming::Data { reliable, id, data } = &(received.0)[0] else {
panic!("Unexpected data received: {:?}", received);
};
panic!("Unexpected data received: {:?}", received);
};

assert!(reliable);

Expand Down
8 changes: 6 additions & 2 deletions crates/construction/src/manufacturing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ fn enqueue(
mut lines: Query<&mut AssemblyLine>,
) {
for event in events.iter() {
let Ok(mut line) = lines.get_mut(event.factory()) else { continue };
let Ok(mut line) = lines.get_mut(event.factory()) else {
continue;
};
info!(
"Enqueueing manufacturing of {} in {:?}.",
event.unit(),
Expand Down Expand Up @@ -410,7 +412,9 @@ fn produce(
loop {
assembly.blocks_mut().map_capacity = *player_count >= PLAYER_MAX_UNITS;

let Some(unit_type) = assembly.produce(time.elapsed()) else { break };
let Some(unit_type) = assembly.produce(time.elapsed()) else {
break;
};
*player_count += 1;

deliver_events.send(DeliverEvent::new(factory, unit_type));
Expand Down
13 changes: 10 additions & 3 deletions crates/controller/src/commands/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ fn right_click_handler(
}) {
Some(enemy) => attack_events.send(GroupAttackEvent::new(enemy)),
None => {
let Some(target) = pointer.terrain_point().map(|p| p.to_flat()) else { return };
let Some(target) = pointer.terrain_point().map(|p| p.to_flat()) else {
return;
};
send_events.send(SendSelectedEvent::new(target));
location_events.send(DeliveryLocationSelectedEvent::new(target));
}
Expand All @@ -227,7 +229,10 @@ fn double_click_handler(
SelectionMode::Replace
};

let Some(targeted_entity_type) = pointer.entity().and_then(|entity| playable.get(entity).ok()) else {
let Some(targeted_entity_type) = pointer
.entity()
.and_then(|entity| playable.get(entity).ok())
else {
return;
};

Expand All @@ -244,7 +249,9 @@ fn move_camera_arrows_system(
mut move_events: EventWriter<MoveCameraHorizontallyEvent>,
) {
for key_event in key_events.iter() {
let Some(key_code) = key_event.key_code else { continue };
let Some(key_code) = key_event.key_code else {
continue;
};

let mut direction = Vec2::ZERO;
if key_code == KeyCode::Left {
Expand Down
2 changes: 1 addition & 1 deletion crates/controller/src/hud/actionbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn update(
) {
commands.entity(bar_node.0).despawn_descendants();

let Some(active) = active.0 else {return };
let Some(active) = active.0 else { return };
let object_type = *objects.get(active).unwrap();

if let Some(factory) = solids.get(object_type).factory() {
Expand Down
12 changes: 9 additions & 3 deletions crates/controller/src/hud/minimap/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ struct CameraPoint<'w, 's> {
impl<'w, 's> CameraPoint<'w, 's> {
fn point(&self, ndc: Vec2) -> Option<Vec2> {
let ray = self.ray.ray(ndc);
let Some(intersection) = self.terrain.cast_ray_msl(&ray, f32::INFINITY) else { return None };
let Some(intersection) = self.terrain.cast_ray_msl(&ray, f32::INFINITY) else {
return None;
};
let point = ray.origin + ray.dir * intersection.toi;
Some(self.ui_coords.flat_to_rel(point.to_flat()))
}
Expand Down Expand Up @@ -153,12 +155,16 @@ fn endpoints_to_line(start: Option<Vec2>, end: Option<Vec2>) -> Option<(Vec2, Ve
let aabb = Aabb::new(Point::new(0., 0.), Point::new(1., 1.));
if !aabb.contains_local_point(&start) {
let ray = Ray::new(start, end - start);
let Some(toi) = aabb.cast_local_ray(&ray, 1., false) else { return None };
let Some(toi) = aabb.cast_local_ray(&ray, 1., false) else {
return None;
};
start = ray.origin + toi * ray.dir;
}
if !aabb.contains_local_point(&end) {
let ray = Ray::new(end, start - end);
let Some(toi) = aabb.cast_local_ray(&ray, 1., false) else { return None};
let Some(toi) = aabb.cast_local_ray(&ray, 1., false) else {
return None;
};
end = ray.origin + toi * ray.dir;
}

Expand Down
4 changes: 3 additions & 1 deletion crates/controller/src/hud/minimap/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ fn click_handler(
bounds: Res<MapBounds>,
mut click_events: EventWriter<MinimapClickEvent>,
) {
let Some(cursor) = window_query.single().cursor_position() else { return };
let Some(cursor) = window_query.single().cursor_position() else {
return;
};
for event in input_events.iter() {
if event.state != ButtonState::Released {
continue;
Expand Down
4 changes: 1 addition & 3 deletions crates/core/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ where
F: Fn() -> Option<SyncPathBuf>,
{
let Some(base_dir) = base_dir() else {
return Err(
DirError("Base directory cannot be established.")
);
return Err(DirError("Base directory cannot be established."));
};

Ok(AsyncPathBuf::from(base_dir).join("DigitalExtinction"))
Expand Down
4 changes: 3 additions & 1 deletion crates/gui/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ impl<'w, 's> ButtonOps<'w, 's> {
/// [`Text`] component. The text must consist of a single section. If such
/// a child is found, its text is changed.
pub fn set_text(&mut self, entity: Entity, text: String) -> Result<(), &'static str> {
let Ok(children) = self.button_query.get(entity) else { return Err("Button does not exist.") };
let Ok(children) = self.button_query.get(entity) else {
return Err("Button does not exist.");
};
for &child in children.iter() {
if let Ok(mut text_component) = self.text_query.get_mut(child) {
if text_component.sections.len() == 1 {
Expand Down
4 changes: 3 additions & 1 deletion crates/gui/src/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ fn input_system(
mut characters: EventReader<ReceivedCharacter>,
mut keyboard: EventReader<KeyboardInput>,
) {
let Some((mut text_box, children)) = focused.get_current_mut() else { return };
let Some((mut text_box, children)) = focused.get_current_mut() else {
return;
};

let text_id = children
.iter()
Expand Down
4 changes: 3 additions & 1 deletion crates/lobby/src/auth/passwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ impl DbPassword {
/// Securely check that a given password corresponds to the password
/// represented by `self`.
pub(super) fn check(&self, password: &str) -> bool {
let Ok(hashed) = Self::hash(password, &self.1) else { return false };
let Ok(hashed) = Self::hash(password, &self.1) else {
return false;
};
self.0.ct_eq(&hashed).into()
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/lobby_client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ impl<'w> AuthenticatedClient<'w> {
&self,
requestable: &T,
) -> Result<Task<Result<T::Response>>> {
let Some(client) = self.client.as_ref() else { bail!("Client not yet set up.") };
let Some(client) = self.client.as_ref() else {
bail!("Client not yet set up.")
};
let request = client.create(self.auth.token(), requestable)?;
Ok(client.fire::<T>(request))
}
Expand Down
8 changes: 6 additions & 2 deletions crates/lobby_client/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ fn setup_client(
if client.is_some() {
return true.into();
}
let Some(conf) = conf else { return false.into() };
let Some(conf) = conf else {
return false.into();
};

let client = LobbyClient::build(conf.multiplayer().server().clone());
commands.insert_resource(client);
Expand All @@ -42,7 +44,9 @@ fn set_token<T>(mut events: EventReader<ResponseEvent<T>>, mut auth: ResMut<Auth
where
T: LobbyRequest<Response = Token>,
{
let Some(event) = events.iter().last() else { return };
let Some(event) = events.iter().last() else {
return;
};
let Ok(token) = event.result() else { return };
auth.set_token(token.token().to_owned());
}
10 changes: 8 additions & 2 deletions crates/map/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ impl TryFrom<&Path> for MapHash {
type Error = PathError;

fn try_from(path: &Path) -> Result<Self, Self::Error> {
let Some(file_name) = path.file_name() else { return Err(PathError::FileNameError("No file name in the path.")) };
let Some(file_name) = file_name.to_str() else { return Err(PathError::FileNameError("File name is not a valid UTF-8 string.")) };
let Some(file_name) = path.file_name() else {
return Err(PathError::FileNameError("No file name in the path."));
};
let Some(file_name) = file_name.to_str() else {
return Err(PathError::FileNameError(
"File name is not a valid UTF-8 string.",
));
};

if file_name.ends_with(MAP_FILE_SUFFIX) {
Self::from_hex(&file_name[0..file_name.len() - MAP_FILE_SUFFIX.len()])
Expand Down
10 changes: 6 additions & 4 deletions crates/map/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ pub async fn load_metadata<P: AsRef<Path>>(path: P) -> LoadingResult<MapMetadata
let mut entry = loading_io_error!(entry);
let path = loading_io_error!(entry.path());
let Some(path) = path.to_str() else {
return Err(MapLoadingError::ArchiveContent(
String::from("The map archive contains an entry with non-UTF-8 path.")));
return Err(MapLoadingError::ArchiveContent(String::from(
"The map archive contains an entry with non-UTF-8 path.",
)));
};

if path == METADATA_JSON_ENTRY {
Expand All @@ -78,8 +79,9 @@ pub async fn load_map<P: AsRef<Path>>(path: P) -> LoadingResult<Map> {
let mut entry = loading_io_error!(entry);
let path = loading_io_error!(entry.path());
let Some(path) = path.to_str() else {
return Err(MapLoadingError::ArchiveContent(
String::from("The map archive contains an entry with non-UTF-8 path.")));
return Err(MapLoadingError::ArchiveContent(String::from(
"The map archive contains an entry with non-UTF-8 path.",
)));
};

if path == METADATA_JSON_ENTRY {
Expand Down
4 changes: 3 additions & 1 deletion crates/menu/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ fn map_selected_system(
mut buttons: ButtonOps,
mut toasts: EventWriter<ToastEvent>,
) {
let Some(event) = map_selected_events.iter().last() else { return };
let Some(event) = map_selected_events.iter().last() else {
return;
};
let hash = match MapHash::try_from(event.path()) {
Ok(hash) => hash,
Err(error) => {
Expand Down
4 changes: 3 additions & 1 deletion crates/menu/src/gamelisting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ fn list_games_system(
mut events: EventReader<ResponseEvent<ListGamesRequest>>,
mut toasts: EventWriter<ToastEvent>,
) {
let Some(event) = events.iter().last() else { return };
let Some(event) = events.iter().last() else {
return;
};
commands.entity(table.0).despawn_descendants();

match event.result() {
Expand Down
2 changes: 1 addition & 1 deletion crates/menu/src/mapselection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn init_buttons(
) {
let Some(mut task) = task else { return };
let Some(result) = future::block_on(future::poll_once(&mut task.0)) else {
return
return;
};

let map_entries = match result {
Expand Down
4 changes: 3 additions & 1 deletion crates/menu/src/singleplayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ fn button_system(
}

fn map_selected_system(mut events: EventReader<MapSelectedEvent>, mut map: ResMut<SelectedMap>) {
let Some(event) = events.iter().last() else { return };
let Some(event) = events.iter().last() else {
return;
};
map.0 = Some(event.path().into());
}
4 changes: 3 additions & 1 deletion crates/movement/src/altitude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ fn update(
) {
objects.par_iter_mut().for_each_mut(
|(&object_type, mut horizontal, mut climbing, transform)| {
let Some(flight) = solids.get(object_type).flight() else { return };
let Some(flight) = solids.get(object_type).flight() else {
return;
};
let height = transform.translation.y;

let desired_height = if horizontal.stationary() {
Expand Down
12 changes: 9 additions & 3 deletions crates/net/src/connection/databuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ impl DataBuf {
///
/// Panics if `buf` len is smaller than length of found data.
pub(super) fn get(&self, id: DatagramId, buf: &mut [u8]) -> Option<usize> {
let Some(slot_index) = self.slot_index(id) else { return None };
let Some(slot_index) = self.slot_index(id) else {
return None;
};

let front = self.slots.front().unwrap();
let slot = self.slots.get(slot_index).unwrap();
Expand All @@ -77,7 +79,9 @@ impl DataBuf {
/// Removes data stored with ID `id` or does nothing if such data do not
/// exist.
pub(super) fn remove(&mut self, id: DatagramId) {
let Some(slot_index) = self.slot_index(id) else { return };
let Some(slot_index) = self.slot_index(id) else {
return;
};
self.slots.get_mut(slot_index).unwrap().used = false;

while let Some(front) = self.slots.front() {
Expand All @@ -94,7 +98,9 @@ impl DataBuf {

/// Get index (withing slots deque) of the slot with ID `id`.
fn slot_index(&self, id: DatagramId) -> Option<usize> {
let Some(&ordinal) = self.ordinals.get(&id) else { return None };
let Some(&ordinal) = self.ordinals.get(&id) else {
return None;
};
// Slots can't be empty since the ordinal was found.
let front = self.slots.front().unwrap();
Some(ordinal.wrapping_sub(front.ordinal))
Expand Down
3 changes: 2 additions & 1 deletion crates/net/src/tasks/dreceiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ pub(super) async fn run(
break;
}

let Ok(result) = timeout(Duration::from_millis(500), messages.recv(&mut buffer)).await else {
let Ok(result) = timeout(Duration::from_millis(500), messages.recv(&mut buffer)).await
else {
continue;
};

Expand Down
4 changes: 3 additions & 1 deletion crates/net/src/tasks/dsender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ pub(super) async fn run(port: u16, datagrams: Receiver<OutDatagram>, messages: M
let mut buffer = [0u8; MAX_DATAGRAM_SIZE];

loop {
let Ok(datagram) = datagrams.recv().await else { break };
let Ok(datagram) = datagrams.recv().await else {
break;
};
if let Err(err) = messages
.send(
&mut buffer,
Expand Down