Use Option::is_some_and where applicable

pull/2036/head
Jonas Platte 2023-06-07 15:10:44 +02:00 committed by Jonas Platte
parent 0b9c082e11
commit c19d72f0f4
17 changed files with 60 additions and 94 deletions

View File

@ -17,7 +17,7 @@ default-members = ["benchmarks", "crates/*"]
resolver = "2"
[workspace.package]
rust-version = "1.65"
rust-version = "1.70"
[workspace.dependencies]
anyhow = "1.0.68"

View File

@ -376,8 +376,7 @@ impl AppService {
let client = self.user(None).await?;
let key = [USER_KEY, localpart.as_ref().as_bytes()].concat();
let store = client.store().get_custom_value(&key).await?;
let registered =
store.and_then(|vec| vec.first().copied()).map_or(false, |b| b == u8::from(true));
let registered = store.is_some_and(|vec| vec.first().copied() == Some(u8::from(true)));
Ok(registered)
}

View File

@ -249,7 +249,7 @@ where
C::Redacted: RedactedStateEventContent,
{
fn has_event_id(&self, ev_id: &EventId) -> bool {
self.as_ref().and_then(|ev| ev.event_id()).map_or(false, |id| id == ev_id)
self.as_ref().is_some_and(|ev| ev.event_id() == Some(ev_id))
}
}

View File

@ -123,7 +123,7 @@ impl Room {
/// Whether this room's [`RoomType`] is `m.space`.
pub fn is_space(&self) -> bool {
self.inner.read().unwrap().room_type().map_or(false, |t| *t == RoomType::Space)
self.inner.read().unwrap().room_type().is_some_and(|t| *t == RoomType::Space)
}
/// Get the unread notification counts.
@ -461,8 +461,7 @@ impl Room {
self.store.get_presence_event(user_id).await?.and_then(|e| e.deserialize().ok());
let profile = self.store.get_profile(self.room_id(), user_id).await?;
let max_power_level = self.max_power_level();
let is_room_creator =
self.inner.read().unwrap().creator().map(|c| c == user_id).unwrap_or(false);
let is_room_creator = self.inner.read().unwrap().creator() == Some(user_id);
let power = self
.store
@ -489,9 +488,7 @@ impl Room {
.await?
.map(|c| c.deserialize())
.transpose()?
.map(|e| e.content)
.map(|l| l.ignored_users.contains_key(member_event.user_id()))
.unwrap_or(false);
.is_some_and(|e| e.content.ignored_users.contains_key(member_event.user_id()));
Ok(Some(RoomMember {
event: Arc::new(member_event),

View File

@ -94,12 +94,7 @@ impl AmbiguityCache {
// incorrect AmbiguityChange overwriting the correct one. In other
// words, this method is not idempotent so we make it by ignoring
// duplicate events.
if self
.changes
.get(room_id)
.map(|c| c.contains_key(member_event.event_id()))
.unwrap_or(false)
{
if self.changes.get(room_id).is_some_and(|c| c.contains_key(member_event.event_id())) {
return Ok(());
}
@ -118,7 +113,7 @@ impl AmbiguityCache {
old_map.as_mut().and_then(|o| o.remove(member_event.state_key()));
let ambiguated_member =
new_map.as_mut().and_then(|n| n.add(member_event.state_key().clone()));
let ambiguous = new_map.as_ref().map(|n| n.is_ambiguous()).unwrap_or(false);
let ambiguous = new_map.as_ref().is_some_and(|n| n.is_ambiguous());
self.update(room_id, old_map, new_map);

View File

@ -1,7 +1,7 @@
use std::{env, process};
fn main() {
let is_wasm = env::var_os("CARGO_CFG_TARGET_ARCH").map_or(false, |arch| arch == "wasm32");
let is_wasm = env::var_os("CARGO_CFG_TARGET_ARCH").is_some_and(|arch| arch == "wasm32");
if is_wasm && env::var_os("CARGO_FEATURE_JS").is_none() {
eprintln!(
"\n\

View File

@ -160,7 +160,7 @@ impl BackupMachine {
/// Are we able to back up room keys to the server?
pub async fn enabled(&self) -> bool {
self.backup_key.read().await.as_ref().map(|b| b.backup_version().is_some()).unwrap_or(false)
self.backup_key.read().await.as_ref().is_some_and(|b| b.backup_version().is_some())
}
/// Check if our own device has signed the given signed JSON payload.

View File

@ -173,8 +173,8 @@ impl Device {
self.user_id() == self.verification_machine.own_user_id()
&& self.device_id() == self.verification_machine.own_device_id()
&& self.ed25519_key().map(|k| k == own_ed25519_key).unwrap_or(false)
&& self.curve25519_key().map(|k| k == own_curve25519_key).unwrap_or(false)
&& self.ed25519_key().is_some_and(|k| k == own_ed25519_key)
&& self.curve25519_key().is_some_and(|k| k == own_curve25519_key)
}
/// Does the given `InboundGroupSession` belong to this device?
@ -285,36 +285,28 @@ impl Device {
/// Is this device cross signed by its owner?
pub fn is_cross_signed_by_owner(&self) -> bool {
self.device_owner_identity
.as_ref()
.map(|device_identity| match device_identity {
// If it's one of our own devices, just check that
// we signed the device.
ReadOnlyUserIdentities::Own(identity) => {
identity.is_device_signed(&self.inner).is_ok()
}
// If it's a device from someone else, check
// if the other user has signed this device.
ReadOnlyUserIdentities::Other(device_identity) => {
device_identity.is_device_signed(&self.inner).is_ok()
}
})
.unwrap_or(false)
self.device_owner_identity.as_ref().is_some_and(|device_identity| match device_identity {
// If it's one of our own devices, just check that
// we signed the device.
ReadOnlyUserIdentities::Own(identity) => identity.is_device_signed(&self.inner).is_ok(),
// If it's a device from someone else, check
// if the other user has signed this device.
ReadOnlyUserIdentities::Other(device_identity) => {
device_identity.is_device_signed(&self.inner).is_ok()
}
})
}
/// Is the device owner verified by us?
pub fn is_device_owner_verified(&self) -> bool {
self.device_owner_identity
.as_ref()
.map(|id| match id {
ReadOnlyUserIdentities::Own(own_identity) => own_identity.is_verified(),
ReadOnlyUserIdentities::Other(other_identity) => self
.own_identity
.as_ref()
.map(|oi| oi.is_verified() && oi.is_identity_signed(other_identity).is_ok())
.unwrap_or(false),
})
.unwrap_or(false)
self.device_owner_identity.as_ref().is_some_and(|id| match id {
ReadOnlyUserIdentities::Own(own_identity) => own_identity.is_verified(),
ReadOnlyUserIdentities::Other(other_identity) => {
self.own_identity.as_ref().is_some_and(|oi| {
oi.is_verified() && oi.is_identity_signed(other_identity).is_ok()
})
}
})
}
/// Request an interactive verification with this `Device`.
@ -767,27 +759,22 @@ impl ReadOnlyDevice {
own_identity: &Option<ReadOnlyOwnUserIdentity>,
device_owner: &Option<ReadOnlyUserIdentities>,
) -> bool {
own_identity.as_ref().map_or(false, |own_identity| {
own_identity.as_ref().is_some_and(|own_identity| {
// Our own identity needs to be marked as verified.
own_identity.is_verified()
&& device_owner
.as_ref()
.map(|device_identity| match device_identity {
// If it's one of our own devices, just check that
// we signed the device.
ReadOnlyUserIdentities::Own(_) => {
own_identity.is_device_signed(self).map_or(false, |_| true)
}
&& device_owner.as_ref().is_some_and(|device_identity| match device_identity {
// If it's one of our own devices, just check that
// we signed the device.
ReadOnlyUserIdentities::Own(_) => own_identity.is_device_signed(self).is_ok(),
// If it's a device from someone else, first check
// that our user has signed the other user and then
// check if the other user has signed this device.
ReadOnlyUserIdentities::Other(device_identity) => {
own_identity.is_identity_signed(device_identity).map_or(false, |_| true)
&& device_identity.is_device_signed(self).map_or(false, |_| true)
}
})
.unwrap_or(false)
// If it's a device from someone else, first check
// that our user has signed the other user and then
// check if the other user has signed this device.
ReadOnlyUserIdentities::Other(device_identity) => {
own_identity.is_identity_signed(device_identity).is_ok()
&& device_identity.is_device_signed(self).is_ok()
}
})
})
}

View File

@ -200,10 +200,7 @@ impl Deref for UserIdentity {
impl UserIdentity {
/// Is this user identity verified.
pub fn is_verified(&self) -> bool {
self.own_identity
.as_ref()
.map(|o| o.is_identity_signed(&self.inner).is_ok())
.unwrap_or(false)
self.own_identity.as_ref().is_some_and(|o| o.is_identity_signed(&self.inner).is_ok())
}
/// Manually verify this user.

View File

@ -554,11 +554,8 @@ impl OutboundGroupSession {
share_info
.get(device.user_id())
.and_then(|d| {
let info = d.get(device.device_id())?;
Some(matches!(info, ShareInfo::Withheld(c) if c == code))
})
.unwrap_or(false)
.and_then(|d| d.get(device.device_id()))
.is_some_and(|info| matches!(info, ShareInfo::Withheld(c) if c == code))
})
})
}

View File

@ -334,20 +334,17 @@ impl PrivateCrossSigningIdentity {
let master_differs = self
.master_public_key()
.await
.map(|master| &master != public_identity.master_key())
.unwrap_or(false);
.is_some_and(|master| &master != public_identity.master_key());
let user_signing_differs = self
.user_signing_public_key()
.await
.map(|subkey| &subkey != public_identity.user_signing_key())
.unwrap_or(false);
.is_some_and(|subkey| &subkey != public_identity.user_signing_key());
let self_signing_differs = self
.self_signing_public_key()
.await
.map(|subkey| &subkey != public_identity.self_signing_key())
.unwrap_or(false);
.is_some_and(|subkey| &subkey != public_identity.self_signing_key());
ClearResult {
master_cleared: master_differs,

View File

@ -130,10 +130,7 @@ impl SessionManager {
#[allow(dead_code)]
pub fn is_device_wedged(&self, device: &ReadOnlyDevice) -> bool {
self.wedged_devices
.get(device.user_id())
.map(|d| d.contains(device.device_id()))
.unwrap_or(false)
self.wedged_devices.get(device.user_id()).is_some_and(|d| d.contains(device.device_id()))
}
/// Check if the session was created to unwedge a Device.
@ -298,7 +295,7 @@ impl SessionManager {
}
fn is_user_timed_out(&self, user_id: &UserId, device_id: &DeviceId) -> bool {
self.failed_devices.get(user_id).map(|d| d.contains(device_id)).unwrap_or(false)
self.failed_devices.get(user_id).is_some_and(|d| d.contains(device_id))
}
/// Receive a successful key claim response and create new Olm sessions with

View File

@ -648,9 +648,9 @@ impl IdentitiesBeingVerified {
if self
.identity_being_verified
.as_ref()
.map_or(false, |i| i.master_key() == identity.master_key())
.is_some_and(|i| i.master_key() == identity.master_key())
{
if verified_identities.map_or(false, |i| i.contains(&identity)) {
if verified_identities.is_some_and(|i| i.contains(&identity)) {
trace!(
user_id = self.other_user_id().as_str(),
"Marking the user identity of as verified."
@ -719,7 +719,7 @@ impl IdentitiesBeingVerified {
return Ok(None);
}
if verified_devices.map_or(false, |v| v.contains(&device)) {
if verified_devices.is_some_and(|v| v.contains(&device)) {
trace!(
user_id = device.user_id().as_str(),
device_id = device.device_id().as_str(),

View File

@ -385,7 +385,7 @@ impl<P: RoomDataProvider> TimelineInner<P> {
pub(super) async fn add_loading_indicator(&self) {
let mut state = self.state.lock().await;
if state.items.front().map_or(false, |item| item.is_loading_indicator()) {
if state.items.front().is_some_and(|item| item.is_loading_indicator()) {
warn!("There is already a loading indicator");
return;
}
@ -397,7 +397,7 @@ impl<P: RoomDataProvider> TimelineInner<P> {
pub(super) async fn remove_loading_indicator(&self, more_messages: bool) {
let mut state = self.state.lock().await;
if !state.items.front().map_or(false, |item| item.is_loading_indicator()) {
if !state.items.front().is_some_and(|item| item.is_loading_indicator()) {
warn!("There is no loading indicator");
return;
}
@ -870,7 +870,7 @@ impl TimelineInnerState {
#[instrument(skip_all)]
fn set_fully_read_event(&mut self, fully_read_event_id: OwnedEventId) {
// A similar event has been handled already. We can ignore it.
if self.fully_read_event.as_ref().map_or(false, |id| *id == fully_read_event_id) {
if self.fully_read_event.as_ref().is_some_and(|id| *id == fully_read_event_id) {
return;
}

View File

@ -119,7 +119,7 @@ impl Timeline {
pub async fn paginate_backwards(&self, mut options: PaginationOptions<'_>) -> Result<()> {
let mut start_lock = self.start_token.lock().await;
if start_lock.is_none() {
if self.inner.items().await.front().map_or(false, |item| item.is_timeline_start()) {
if self.inner.items().await.front().is_some_and(|item| item.is_timeline_start()) {
warn!("Start of timeline reached, ignoring backwards-pagination request");
return Ok(());
}

View File

@ -252,7 +252,7 @@ fn maybe_update_read_receipt(
.get(receipt.user_id)
.and_then(|receipts| receipts.get(&receipt.receipt_type))
.map(|(event_id, _)| event_id);
if old_event_id.map_or(false, |id| id == receipt.event_id) {
if old_event_id.is_some_and(|id| id == receipt.event_id) {
// Nothing to do.
return false;
}

View File

@ -30,7 +30,7 @@ fn main() {
"only one of the features 'native-tls' or 'rustls-tls' can be enabled",
);
let is_wasm = env::var_os("CARGO_CFG_TARGET_ARCH").map_or(false, |arch| arch == "wasm32");
let is_wasm = env::var_os("CARGO_CFG_TARGET_ARCH").is_some_and(|arch| arch == "wasm32");
if is_wasm {
ensure(
!env_is_set("CARGO_FEATURE_SSO_LOGIN"),