From 52c2893073d5f07ee0c9afc59dcfc330fd230077 Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Sun, 12 May 2024 16:41:54 -0700 Subject: [PATCH] enable `if_then_some_else_none` lint --- Cargo.toml | 1 + src/api/client_server/sync.rs | 60 ++++++++----------- src/database.rs | 8 +-- .../key_value/rooms/state_compressor.rs | 2 +- 4 files changed, 27 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4d10883b..f7c57201 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ filetype_is_file = "warn" float_cmp_const = "warn" format_push_string = "warn" get_unwrap = "warn" +if_then_some_else_none = "warn" lossy_float_literal = "warn" mem_forget = "warn" mod_module_files = "warn" diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index 0e3cc38c..d1bcda0e 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -996,31 +996,20 @@ async fn load_joined_room( .filter_map(|r| r.ok()), ); - let notification_count = if send_notification_counts { - Some( + let notification_count = send_notification_counts + .then(|| { services() .rooms .user - .notification_count(sender_user, room_id)? - .try_into() - .expect("notification count can't go that high"), - ) - } else { - None - }; + .notification_count(sender_user, room_id) + }) + .transpose()? + .map(|x| x.try_into().expect("notification count can't go that high")); - let highlight_count = if send_notification_counts { - Some( - services() - .rooms - .user - .highlight_count(sender_user, room_id)? - .try_into() - .expect("highlight count can't go that high"), - ) - } else { - None - }; + let highlight_count = send_notification_counts + .then(|| services().rooms.user.highlight_count(sender_user, room_id)) + .transpose()? + .map(|x| x.try_into().expect("highlight count can't go that high")); let prev_batch = timeline_pdus .first() @@ -1557,13 +1546,7 @@ pub(crate) async fn sync_events_v4_route( PduCount::Normal(c) => c.to_string(), })) })? - .or_else(|| { - if roomsince != &0 { - Some(roomsince.to_string()) - } else { - None - } - }); + .or_else(|| (roomsince != &0).then(|| roomsince.to_string())); let room_events: Vec<_> = timeline_pdus .iter() @@ -1708,16 +1691,21 @@ pub(crate) async fn sync_events_v4_route( lists, rooms, extensions: sync_events::v4::Extensions { - to_device: if body.extensions.to_device.enabled.unwrap_or(false) { - Some(sync_events::v4::ToDevice { - events: services() + to_device: body + .extensions + .to_device + .enabled + .unwrap_or(false) + .then(|| { + services() .users - .get_to_device_events(&sender_user, &sender_device)?, - next_batch: next_batch.to_string(), + .get_to_device_events(&sender_user, &sender_device) + .map(|events| sync_events::v4::ToDevice { + events, + next_batch: next_batch.to_string(), + }) }) - } else { - None - }, + .transpose()?, e2ee: sync_events::v4::E2EE { device_lists: DeviceLists { changed: device_list_changes.into_iter().collect(), diff --git a/src/database.rs b/src/database.rs index 8e684079..7bc56703 100644 --- a/src/database.rs +++ b/src/database.rs @@ -763,13 +763,7 @@ impl KeyValueDatabase { let batch2: Vec<_> = db .tokenids .iter() - .filter_map(|(key, _)| { - if key.starts_with(b"!") { - Some(key) - } else { - None - } - }) + .filter_map(|(key, _)| key.starts_with(b"!").then_some(key)) .collect(); for key in batch2 { diff --git a/src/database/key_value/rooms/state_compressor.rs b/src/database/key_value/rooms/state_compressor.rs index 65ea603e..ab06d8f3 100644 --- a/src/database/key_value/rooms/state_compressor.rs +++ b/src/database/key_value/rooms/state_compressor.rs @@ -14,7 +14,7 @@ impl service::rooms::state_compressor::Data for KeyValueDatabase { .ok_or_else(|| Error::bad_database("State hash does not exist"))?; let parent = utils::u64_from_bytes(&value[0..size_of::()]).expect("bytes have right length"); - let parent = if parent != 0 { Some(parent) } else { None }; + let parent = (parent != 0).then_some(parent); let mut add_mode = true; let mut added = HashSet::new();