From 1fd8c2052e29950678e7357e15754e42e288b075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 18 Dec 2020 13:50:02 +0100 Subject: [PATCH] crypto: Fix a bunch of clippy warnings. --- matrix_sdk/src/client.rs | 2 +- matrix_sdk_crypto/src/machine.rs | 30 +++++++++---------- matrix_sdk_crypto/src/verification/machine.rs | 26 ++++++++-------- .../src/verification/requests.rs | 19 +++++------- .../src/verification/sas/event_enums.rs | 2 +- .../src/verification/sas/helpers.rs | 2 +- matrix_sdk_crypto/src/verification/sas/mod.rs | 1 + .../src/verification/sas/sas_state.rs | 23 +++++--------- 8 files changed, 48 insertions(+), 57 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 114275d9..a0c3e9d2 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -1165,7 +1165,7 @@ impl Client { request: &RoomMessageRequest, ) -> Result { let content = request.content.clone(); - let txn_id = request.txn_id.into(); + let txn_id = request.txn_id; let room_id = &request.room_id; self.room_send(&room_id, content, Some(txn_id)).await diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index fcb00c86..2a731a5b 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -743,8 +743,8 @@ impl OlmMachine { } } - async fn handle_verification_event(&self, mut event: &mut AnyToDeviceEvent) { - if let Err(e) = self.verification_machine.receive_event(&mut event).await { + async fn handle_verification_event(&self, event: &AnyToDeviceEvent) { + if let Err(e) = self.verification_machine.receive_event(&event).await { error!("Error handling a verification event: {:?}", e); } } @@ -879,7 +879,7 @@ impl OlmMachine { | AnyToDeviceEvent::KeyVerificationMac(..) | AnyToDeviceEvent::KeyVerificationRequest(..) | AnyToDeviceEvent::KeyVerificationStart(..) => { - self.handle_verification_event(&mut event).await; + self.handle_verification_event(&event).await; } _ => continue, } @@ -1826,34 +1826,34 @@ pub(crate) mod test { let (alice_sas, request) = bob_device.start_verification().await.unwrap(); - let mut event = request_to_event(alice.user_id(), &request.into()); - bob.handle_verification_event(&mut event).await; + let event = request_to_event(alice.user_id(), &request.into()); + bob.handle_verification_event(&event).await; let bob_sas = bob.get_verification(alice_sas.flow_id().as_str()).unwrap(); assert!(alice_sas.emoji().is_none()); assert!(bob_sas.emoji().is_none()); - let mut event = bob_sas + let event = bob_sas .accept() .map(|r| request_to_event(bob.user_id(), &r)) .unwrap(); - alice.handle_verification_event(&mut event).await; + alice.handle_verification_event(&event).await; - let mut event = alice + let event = alice .outgoing_to_device_requests() .first() .map(|r| outgoing_request_to_event(alice.user_id(), r)) .unwrap(); - bob.handle_verification_event(&mut event).await; + bob.handle_verification_event(&event).await; - let mut event = bob + let event = bob .outgoing_to_device_requests() .first() .map(|r| outgoing_request_to_event(bob.user_id(), r)) .unwrap(); - alice.handle_verification_event(&mut event).await; + alice.handle_verification_event(&event).await; assert!(alice_sas.emoji().is_some()); assert!(bob_sas.emoji().is_some()); @@ -1861,19 +1861,19 @@ pub(crate) mod test { assert_eq!(alice_sas.emoji(), bob_sas.emoji()); assert_eq!(alice_sas.decimals(), bob_sas.decimals()); - let mut event = bob_sas + let event = bob_sas .confirm() .await .unwrap() .0 .map(|r| request_to_event(bob.user_id(), &r)) .unwrap(); - alice.handle_verification_event(&mut event).await; + alice.handle_verification_event(&event).await; assert!(!alice_sas.is_done()); assert!(!bob_sas.is_done()); - let mut event = alice_sas + let event = alice_sas .confirm() .await .unwrap() @@ -1891,7 +1891,7 @@ pub(crate) mod test { .unwrap(); assert!(!alice_device.is_trusted()); - bob.handle_verification_event(&mut event).await; + bob.handle_verification_event(&event).await; assert!(bob_sas.is_done()); assert!(alice_device.is_trusted()); } diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 64b52ab7..b59fd64a 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -96,7 +96,7 @@ impl VerificationMachine { let request = content_to_request(device.user_id(), device.device_id(), c); self.verifications - .insert(sas.flow_id().to_string(), sas.clone()); + .insert(sas.flow_id().as_str().to_owned(), sas.clone()); request.into() } @@ -152,7 +152,7 @@ impl VerificationMachine { request: Arc::new( RoomMessageRequest { room_id: r, - txn_id: request_id.clone(), + txn_id: request_id, content: c, } .into(), @@ -512,7 +512,7 @@ mod test { ); machine - .receive_event(&mut wrap_any_to_device_content( + .receive_event(&wrap_any_to_device_content( bob_sas.user_id(), start_content.into(), )) @@ -536,18 +536,18 @@ mod test { let alice = alice_machine.get_sas(bob.flow_id().as_str()).unwrap(); - let mut event = alice + let event = alice .accept() .map(|c| wrap_any_to_device_content(alice.user_id(), get_content_from_request(&c))) .unwrap(); - let mut event = bob - .receive_event(&mut event) + let event = bob + .receive_event(&event) .map(|c| wrap_any_to_device_content(bob.user_id(), c)) .unwrap(); assert!(alice_machine.outgoing_to_device_messages.is_empty()); - alice_machine.receive_event(&mut event).await.unwrap(); + alice_machine.receive_event(&event).await.unwrap(); assert!(!alice_machine.outgoing_to_device_messages.is_empty()); let request = alice_machine @@ -564,29 +564,29 @@ mod test { panic!("Invalid request type"); }; - let mut event = + let event = wrap_any_to_device_content(alice.user_id(), get_content_from_request(&r.into())); drop(request); alice_machine.mark_request_as_sent(&txn_id); - assert!(bob.receive_event(&mut event).is_none()); + assert!(bob.receive_event(&event).is_none()); assert!(alice.emoji().is_some()); assert!(bob.emoji().is_some()); assert_eq!(alice.emoji(), bob.emoji()); - let mut event = wrap_any_to_device_content( + let event = wrap_any_to_device_content( alice.user_id(), get_content_from_request(&alice.confirm().await.unwrap().0.unwrap()), ); - bob.receive_event(&mut event); + bob.receive_event(&event); - let mut event = wrap_any_to_device_content( + let event = wrap_any_to_device_content( bob.user_id(), get_content_from_request(&bob.confirm().await.unwrap().0.unwrap()), ); - alice.receive_event(&mut event); + alice.receive_event(&event); assert!(alice.is_done()); assert!(bob.is_done()); diff --git a/matrix_sdk_crypto/src/verification/requests.rs b/matrix_sdk_crypto/src/verification/requests.rs index d7f55446..ae96cf90 100644 --- a/matrix_sdk_crypto/src/verification/requests.rs +++ b/matrix_sdk_crypto/src/verification/requests.rs @@ -86,13 +86,13 @@ impl VerificationRequest { } pub(crate) fn into_started_sas( - &self, + self, event: &SyncMessageEvent, device: ReadOnlyDevice, user_identity: Option, ) -> Result { match &*self.inner.lock().unwrap() { - InnerRequest::Ready(s) => s.into_started_sas( + InnerRequest::Ready(s) => s.clone().into_started_sas( &event.clone().into_full_event(self.room_id().clone()), self.store.clone(), self.account.clone(), @@ -129,7 +129,7 @@ impl InnerRequest { } fn into_started_sas( - &mut self, + self, event: &MessageEvent, store: Arc>, account: ReadOnlyAccount, @@ -193,8 +193,7 @@ impl RequestState { state: Sent { methods: SUPPORTED_METHODS.to_vec(), flow_id: response.event_id.clone(), - } - .into(), + }, } } } @@ -219,9 +218,8 @@ impl RequestState { state: Ready { methods: content.methods.to_owned(), other_device_id: content.from_device.clone(), - flow_id: self.state.flow_id.clone(), - } - .into(), + flow_id: self.state.flow_id, + }, } } } @@ -256,8 +254,7 @@ impl RequestState { methods: content.methods.clone(), flow_id: event_id.clone(), other_device_id: content.from_device.clone(), - } - .into(), + }, } } @@ -300,7 +297,7 @@ struct Ready { impl RequestState { fn into_started_sas( - &self, + self, event: &MessageEvent, store: Arc>, account: ReadOnlyAccount, diff --git a/matrix_sdk_crypto/src/verification/sas/event_enums.rs b/matrix_sdk_crypto/src/verification/sas/event_enums.rs index c21b0e8b..ce122eac 100644 --- a/matrix_sdk_crypto/src/verification/sas/event_enums.rs +++ b/matrix_sdk_crypto/src/verification/sas/event_enums.rs @@ -55,7 +55,7 @@ impl StartContent { } } - pub fn to_canonical_json(self) -> CanonicalJsonValue { + pub fn canonical_json(self) -> CanonicalJsonValue { let content = match self { StartContent::ToDevice(c) => serde_json::to_value(c), StartContent::Room(_, c) => serde_json::to_value(c), diff --git a/matrix_sdk_crypto/src/verification/sas/helpers.rs b/matrix_sdk_crypto/src/verification/sas/helpers.rs index 587982f6..76d29729 100644 --- a/matrix_sdk_crypto/src/verification/sas/helpers.rs +++ b/matrix_sdk_crypto/src/verification/sas/helpers.rs @@ -62,7 +62,7 @@ pub struct SasIds { /// * `content` - The `m.key.verification.start` event content that started the /// interactive verification process. pub fn calculate_commitment(public_key: &str, content: impl Into) -> String { - let content = content.into().to_canonical_json(); + let content = content.into().canonical_json(); encode( Sha256::new() diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index de23dc64..ede3a46a 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -53,6 +53,7 @@ use self::event_enums::CancelContent; #[derive(Debug)] /// A result of a verification flow. +#[allow(clippy::large_enum_variant)] pub enum VerificationResult { /// The verification succeeded, nothing needs to be done. Ok, diff --git a/matrix_sdk_crypto/src/verification/sas/sas_state.rs b/matrix_sdk_crypto/src/verification/sas/sas_state.rs index d3eec749..ae65d7e5 100644 --- a/matrix_sdk_crypto/src/verification/sas/sas_state.rs +++ b/matrix_sdk_crypto/src/verification/sas/sas_state.rs @@ -86,13 +86,6 @@ impl FlowId { } } - pub fn to_string(&self) -> String { - match self { - FlowId::InRoom(_, r) => r.to_string(), - FlowId::ToDevice(t) => t.to_string(), - } - } - pub fn as_str(&self) -> &str { match self { FlowId::InRoom(_, r) => r.as_str(), @@ -403,8 +396,8 @@ impl SasState { pub fn as_content(&self) -> StartContent { match self.verification_flow_id.as_ref() { - FlowId::ToDevice(_) => StartContent::ToDevice(StartToDeviceEventContent { - transaction_id: self.verification_flow_id.to_string(), + FlowId::ToDevice(s) => StartContent::ToDevice(StartToDeviceEventContent { + transaction_id: s.to_string(), from_device: self.device_id().into(), method: StartMethod::MSasV1( MSasV1Content::new(self.state.protocol_definitions.clone()) @@ -590,8 +583,8 @@ impl SasState { ); match self.verification_flow_id.as_ref() { - FlowId::ToDevice(_) => AcceptToDeviceEventContent { - transaction_id: self.verification_flow_id.to_string(), + FlowId::ToDevice(s) => AcceptToDeviceEventContent { + transaction_id: s.to_string(), method, } .into(), @@ -732,12 +725,12 @@ impl SasState { /// if we_started is false. pub fn as_content(&self) -> KeyContent { match &*self.verification_flow_id { - FlowId::ToDevice(s) => KeyContent::ToDevice(KeyToDeviceEventContent { + FlowId::ToDevice(s) => KeyToDeviceEventContent { transaction_id: s.to_string(), key: self.inner.lock().unwrap().public_key(), - }) + } .into(), - FlowId::InRoom(r, e) => KeyContent::Room( + FlowId::InRoom(r, e) => ( r.clone(), KeyEventContent { key: self.inner.lock().unwrap().public_key(), @@ -746,7 +739,7 @@ impl SasState { }, }, ) - .into(), + .into(), } }