matirx_sdk: Fix a bunch of clippy warnings.

This commit is contained in:
Damir Jelić 2020-04-30 14:07:49 +02:00
parent fc0d4a7d35
commit 3bcce962e3
7 changed files with 38 additions and 30 deletions

View file

@ -294,16 +294,13 @@ impl Client {
#[cfg(feature = "encryption")]
{
match e {
RoomEvent::RoomEncrypted(ref mut e) => {
e.room_id = Some(room_id.to_owned());
let mut olm = self.olm.lock().await;
if let RoomEvent::RoomEncrypted(ref mut e) = e {
e.room_id = Some(room_id.to_owned());
let mut olm = self.olm.lock().await;
if let Some(o) = &mut *olm {
decrypted_event = o.decrypt_room_event(&e).await.ok();
}
if let Some(o) = &mut *olm {
decrypted_event = o.decrypt_room_event(&e).await.ok();
}
_ => (),
}
}

View file

@ -150,17 +150,17 @@ impl OlmMachine {
}
/// The unique user id that owns this identity.
pub(crate) fn user_id(&self) -> &UserId {
pub fn user_id(&self) -> &UserId {
&self.user_id
}
/// The unique device id of the device that holds this identity.
pub(crate) fn device_id(&self) -> &DeviceId {
pub fn device_id(&self) -> &DeviceId {
&self.device_id
}
/// Get the public parts of the identity keys.
pub(crate) fn identity_keys(&self) -> &IdentityKeys {
pub fn identity_keys(&self) -> &IdentityKeys {
self.account.identity_keys()
}
@ -289,7 +289,7 @@ impl OlmMachine {
continue;
};
let one_time_key = if let Some(k) = key_map.values().nth(0) {
let one_time_key = if let Some(k) = key_map.values().next() {
match k {
OneTimeKey::SignedKey(k) => k,
OneTimeKey::Key(_) => {
@ -826,29 +826,29 @@ impl OlmMachine {
let encrytped_sender = decrypted_json
.get("sender")
.cloned()
.ok_or(EventError::MissingField("sender".to_string()))?;
.ok_or_else(|| EventError::MissingField("sender".to_string()))?;
let encrytped_sender: UserId = serde_json::from_value(encrytped_sender)?;
let recipient = decrypted_json
.get("recipient")
.cloned()
.ok_or(EventError::MissingField("recipient".to_string()))?;
.ok_or_else(|| EventError::MissingField("recipient".to_string()))?;
let recipient: UserId = serde_json::from_value(recipient)?;
let recipient_keys: BTreeMap<KeyAlgorithm, String> = serde_json::from_value(
decrypted_json
.get("recipient_keys")
.cloned()
.ok_or(EventError::MissingField("recipient_keys".to_string()))?,
.ok_or_else(|| EventError::MissingField("recipient_keys".to_string()))?,
)?;
let keys: BTreeMap<KeyAlgorithm, String> = serde_json::from_value(
decrypted_json
.get("keys")
.cloned()
.ok_or(EventError::MissingField("keys".to_string()))?,
.ok_or_else(|| EventError::MissingField("keys".to_string()))?,
)?;
if recipient != self.user_id || sender != &encrytped_sender {
return Err(EventError::MissmatchedSender)?;
return Err(EventError::MissmatchedSender.into());
}
if self.account.identity_keys().ed25519()
@ -856,7 +856,7 @@ impl OlmMachine {
.get(&KeyAlgorithm::Ed25519)
.ok_or(EventError::MissingSigningKey)?
{
return Err(EventError::MissmatchedKeys)?;
return Err(EventError::MissmatchedKeys.into());
}
let signing_key = keys
@ -887,7 +887,7 @@ impl OlmMachine {
c
} else {
warn!("Error, unsupported encryption algorithm");
return Err(EventError::UnsupportedAlgorithm)?;
return Err(EventError::UnsupportedAlgorithm.into());
};
let identity_keys = self.account.identity_keys();
@ -925,7 +925,7 @@ impl OlmMachine {
Ok(decrypted_event)
} else {
warn!("Olm event doesn't contain a ciphertext for our key");
Err(EventError::MissingCiphertext)?
Err(EventError::MissingCiphertext.into())
}
}
@ -1228,8 +1228,8 @@ impl OlmMachine {
ToDeviceEvent::RoomKey(mut e) => {
self.add_room_key(sender_key, signing_key, &mut e).await
}
ToDeviceEvent::ForwardedRoomKey(mut e) => {
self.add_forwarded_room_key(sender_key, signing_key, &mut e)
ToDeviceEvent::ForwardedRoomKey(e) => {
self.add_forwarded_room_key(sender_key, signing_key, &e)
}
_ => {
warn!("Received a unexpected encrypted to-device event");
@ -1310,7 +1310,7 @@ impl OlmMachine {
) -> MegolmResult<EventJson<RoomEvent>> {
let content = match &event.content {
EncryptedEventContent::MegolmV1AesSha2(c) => c,
_ => return Err(EventError::UnsupportedAlgorithm)?,
_ => return Err(EventError::UnsupportedAlgorithm.into()),
};
let room_id = event.room_id.as_ref().unwrap();
@ -1893,8 +1893,9 @@ mod test {
alice.account.identity_keys().curve25519(),
alice_session.session_id(),
)
.await
.unwrap();
.await;
assert!(session.unwrap().is_some());
}
#[tokio::test]

View file

@ -192,7 +192,7 @@ impl DeviceStore {
self.entries
.get(user_id)
.and_then(|m| m.remove(device_id))
.and_then(|(_, d)| Some(d))
.map(|(_, d)| d)
}
/// Get a read-only view over all devices of the given user.

View file

@ -58,6 +58,13 @@ impl fmt::Debug for Account {
}
}
#[cfg_attr(tarpaulin, skip)]
impl Default for Account {
fn default() -> Self {
Self::new()
}
}
impl Account {
/// Create a new account.
pub fn new() -> Self {
@ -182,7 +189,7 @@ impl Account {
inner: Arc::new(Mutex::new(session)),
session_id: Arc::new(session_id),
sender_key: Arc::new(their_identity_key.to_owned()),
creation_time: Arc::new(now.clone()),
creation_time: Arc::new(now),
last_use_time: Arc::new(now),
})
}
@ -223,7 +230,7 @@ impl Account {
inner: Arc::new(Mutex::new(session)),
session_id: Arc::new(session_id),
sender_key: Arc::new(their_identity_key.to_owned()),
creation_time: Arc::new(now.clone()),
creation_time: Arc::new(now),
last_use_time: Arc::new(now),
})
}

View file

@ -87,6 +87,7 @@ impl CryptoStore for MemoryStore {
Ok(self.tracked_users.insert(user.clone()))
}
#[allow(clippy::ptr_arg)]
async fn get_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Option<Device>> {
Ok(self.devices.get(user_id, device_id))
}

View file

@ -147,6 +147,7 @@ pub trait CryptoStore: Debug + Send + Sync {
/// * `user_id` - The user that the device belongs to.
///
/// * `device_id` - The unique id of the device.
#[allow(clippy::ptr_arg)]
async fn get_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Option<Device>>;
/// Get all the devices of the given user.

View file

@ -480,12 +480,12 @@ impl CryptoStore for SqliteStore {
let mut group_sessions = self.load_inbound_group_sessions().await?;
let _ = group_sessions
group_sessions
.drain(..)
.map(|s| {
self.inbound_group_sessions.add(s);
})
.collect::<()>();
.for_each(drop);
let devices = self.load_devices().await?;
mem::replace(&mut self.devices, devices);
@ -625,6 +625,7 @@ impl CryptoStore for SqliteStore {
todo!()
}
#[allow(clippy::ptr_arg)]
async fn get_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Option<Device>> {
Ok(self.devices.get(user_id, device_id))
}