From 0cdd9b4fa9e216d966a4446e286369761aa6fd58 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 18 Nov 2024 18:22:14 +0000 Subject: [PATCH] refactor: remove indexing/slicing from `squash_attachment_parts` --- src/mimeparser.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/mimeparser.rs b/src/mimeparser.rs index ac493514bf..4218ea6d74 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -665,11 +665,13 @@ impl MimeMessage { /// Delta Chat sends attachments, such as images, in two-part messages, with the first message /// containing a description. If such a message is detected, text from the first part can be /// moved to the second part, and the first part dropped. - #[allow(clippy::indexing_slicing)] fn squash_attachment_parts(&mut self) { - if let [textpart, filepart] = &self.parts[..] { - let need_drop = textpart.typ == Viewtype::Text - && match filepart.typ { + if self.parts.len() == 2 + && self.parts.first().map(|textpart| textpart.typ) == Some(Viewtype::Text) + && self + .parts + .get(1) + .map_or(false, |filepart| match filepart.typ { Viewtype::Image | Viewtype::Gif | Viewtype::Sticker @@ -680,24 +682,24 @@ impl MimeMessage { | Viewtype::File | Viewtype::Webxdc => true, Viewtype::Unknown | Viewtype::Text | Viewtype::VideochatInvitation => false, - }; - - if need_drop { - let mut filepart = self.parts.swap_remove(1); - - // insert new one - filepart.msg.clone_from(&self.parts[0].msg); - if let Some(quote) = self.parts[0].param.get(Param::Quote) { - filepart.param.set(Param::Quote, quote); - } - - // forget the one we use now - self.parts[0].msg = "".to_string(); + }) + { + let mut parts = std::mem::take(&mut self.parts); + let Some(mut filepart) = parts.pop() else { + // Should never happen. + return; + }; + let Some(textpart) = parts.pop() else { + // Should never happen. + return; + }; - // swap new with old - self.parts.push(filepart); // push to the end - let _ = self.parts.swap_remove(0); // drops first element, replacing it with the last one in O(1) + filepart.msg.clone_from(&textpart.msg); + if let Some(quote) = textpart.param.get(Param::Quote) { + filepart.param.set(Param::Quote, quote); } + + self.parts = vec![filepart]; } }