From 8e041f90dd524867d39455f22b8871d9e592824d Mon Sep 17 00:00:00 2001 From: timokoesters Date: Mon, 11 May 2020 12:33:25 +0200 Subject: [PATCH] refactor: make cargo clippy happier --- rust-toolchain | 2 +- src/client_server.rs | 42 ++++++++++++++++-------------------- src/database/account_data.rs | 3 +-- src/database/rooms.rs | 4 ++-- src/main.rs | 2 +- src/ruma_wrapper.rs | 1 - 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/rust-toolchain b/rust-toolchain index bf867e0a..e40c16e0 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly +nightly-2020-05-09 diff --git a/src/client_server.rs b/src/client_server.rs index 183abbfe..1a407f79 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -158,7 +158,6 @@ pub fn register_route( // Generate new device id if the user didn't specify one let device_id = body .device_id - .clone() .unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH)); // Generate new token for the device @@ -276,7 +275,6 @@ pub fn login_route( // Generate new device id if the user didn't specify one let device_id = body .device_id - .clone() .unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH)); // Generate a new token for the device @@ -347,7 +345,7 @@ pub fn set_pushrule_route( _rule_id: String, ) -> MatrixResult { // TODO - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); db.account_data .update( None, @@ -448,7 +446,7 @@ pub fn set_displayname_route( body: Ruma, _user_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); if let Some(displayname) = &body.displayname { // Some("") will clear the displayname @@ -539,7 +537,7 @@ pub fn set_avatar_url_route( body: Ruma, _user_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); if !body.avatar_url.starts_with("mxc://") { debug!("Request contains an invalid avatar_url."); @@ -624,7 +622,7 @@ pub fn set_presence_route( body: Ruma, _user_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); db.global_edus .update_globallatest( @@ -670,7 +668,7 @@ pub fn set_read_marker_route( body: Ruma, _room_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); db.account_data .update( Some(&body.room_id), @@ -739,7 +737,7 @@ pub fn create_typing_event_route( _room_id: String, _user_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); let edu = EduEvent::Typing(ruma_events::typing::TypingEvent { content: ruma_events::typing::TypingEventContent { user_ids: vec![user_id.clone()], @@ -772,7 +770,7 @@ pub fn create_room_route( ) -> MatrixResult { // TODO: check if room is unique let room_id = RoomId::try_from(db.globals.hostname()).expect("host is valid"); - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); db.rooms .append_pdu( @@ -890,7 +888,7 @@ pub fn join_room_by_id_route( body: Ruma, _room_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); if db .rooms @@ -920,7 +918,7 @@ pub fn join_room_by_id_or_alias_route( body: Ruma, _room_id_or_alias: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) { Ok(room_id) => room_id, @@ -965,7 +963,7 @@ pub fn leave_room_route( body: Ruma, _room_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); db.rooms .leave(&user_id, &body.room_id, &user_id, &db.globals) .unwrap(); @@ -978,7 +976,7 @@ pub fn forget_room_route( body: Ruma, _room_id: String, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); db.rooms.forget(&body.room_id, &user_id).unwrap(); MatrixResult(Ok(forget_room::Response)) } @@ -1112,7 +1110,7 @@ pub fn create_message_event_route( _txn_id: String, body: Ruma, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); let mut unsigned = serde_json::Map::new(); unsigned.insert("transaction_id".to_owned(), body.txn_id.clone().into()); @@ -1146,14 +1144,14 @@ pub fn create_state_event_for_key_route( _state_key: String, body: Ruma, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); // Reponse of with/without key is the same let event_id = db .rooms .append_pdu( body.room_id.clone(), - user_id, + user_id.clone(), body.event_type.clone(), body.json_body.clone(), None, @@ -1177,14 +1175,14 @@ pub fn create_state_event_for_empty_key_route( _event_type: String, body: Ruma, ) -> MatrixResult { - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); // Reponse of with/without key is the same let event_id = db .rooms .append_pdu( body.room_id.clone(), - user_id, + user_id.clone(), body.event_type.clone(), body.json_body.clone(), None, @@ -1204,7 +1202,7 @@ pub fn sync_route( body: Ruma, ) -> MatrixResult { std::thread::sleep(Duration::from_millis(1500)); - let user_id = body.user_id.clone().expect("user is authenticated"); + let user_id = body.user_id.as_ref().expect("user is authenticated"); let next_batch = db.globals.current_count().unwrap().to_string(); let mut joined_rooms = BTreeMap::new(); @@ -1344,10 +1342,7 @@ pub fn sync_route( for room_id in db.rooms.rooms_left(&user_id) { let room_id = room_id.unwrap(); let pdus = db.rooms.pdus_since(&room_id, since).unwrap(); - let room_events = pdus - .into_iter() - .map(|pdu| pdu.unwrap().to_room_event()) - .collect(); + let room_events = pdus.map(|pdu| pdu.unwrap().to_room_event()).collect(); let mut edus = db .rooms @@ -1380,7 +1375,6 @@ pub fn sync_route( .rooms .pdus_since(&room_id, since) .unwrap() - .into_iter() .map(|pdu| pdu.unwrap().to_stripped_state_event()) .collect(); diff --git a/src/database/account_data.rs b/src/database/account_data.rs index 1d48232d..7ade70ce 100644 --- a/src/database/account_data.rs +++ b/src/database/account_data.rs @@ -33,13 +33,12 @@ impl AccountData { .rev() .filter_map(|r| r.ok()) .take_while(|key| key.starts_with(&prefix)) - .filter(|key| { + .find(|key| { key.split(|&b| b == 0xff) .nth(1) .filter(|&user| user == user_id.to_string().as_bytes()) .is_some() }) - .next() { // This is the old room_latest self.roomuserdataid_accountdata.remove(old)?; diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 11306826..f741afa0 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -261,14 +261,14 @@ impl Rooms { self.pduid_pdu.insert(&pdu_id, &*pdu_json.to_string())?; self.eventid_pduid - .insert(pdu.event_id.to_string(), pdu_id.clone())?; + .insert(pdu.event_id.to_string(), pdu_id)?; if let Some(state_key) = pdu.state_key { let mut key = room_id.to_string().as_bytes().to_vec(); key.push(0xff); key.extend_from_slice(pdu.kind.to_string().as_bytes()); key.push(0xff); - key.extend_from_slice(state_key.to_string().as_bytes()); + key.extend_from_slice(state_key.as_bytes()); self.roomstateid_pdu.insert(key, &*pdu_json.to_string())?; } diff --git a/src/main.rs b/src/main.rs index 3452423e..13b0b654 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,7 +84,7 @@ fn setup_rocket() -> rocket::Rocket { fn main() { // Log info by default - if let Err(_) = std::env::var("RUST_LOG") { + if std::env::var("RUST_LOG").is_err() { std::env::set_var("RUST_LOG", "warn"); } diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 7c904dbd..21c59255 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -62,7 +62,6 @@ impl<'a, T: Endpoint> FromData<'a> for Ruma { None => return Failure((Status::Unauthorized, ())), Some((user_id, device_id)) => (Some(user_id), Some(device_id)), } - } else { (None, None) };