From c18a476806c44d3d4d5e3509585d76d83c06ff92 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 18 Nov 2024 22:13:14 +0000 Subject: [PATCH] refactor: forbid clippy::string_slice --- deltachat-contact-tools/src/lib.rs | 1 + deltachat-jsonrpc/src/lib.rs | 1 + format-flowed/src/lib.rs | 1 + src/lib.rs | 1 + src/qr.rs | 15 ++++---------- src/securejoin.rs | 2 +- src/tools.rs | 33 ++++++++++++++++++------------ 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/deltachat-contact-tools/src/lib.rs b/deltachat-contact-tools/src/lib.rs index 4a6e5b037d..17c2ad0d4c 100644 --- a/deltachat-contact-tools/src/lib.rs +++ b/deltachat-contact-tools/src/lib.rs @@ -16,6 +16,7 @@ clippy::cloned_instead_of_copied )] #![cfg_attr(not(test), forbid(clippy::indexing_slicing))] +#![cfg_attr(not(test), forbid(clippy::string_slice))] #![allow( clippy::match_bool, clippy::mixed_read_write_in_expression, diff --git a/deltachat-jsonrpc/src/lib.rs b/deltachat-jsonrpc/src/lib.rs index 912a6883a7..389206f467 100644 --- a/deltachat-jsonrpc/src/lib.rs +++ b/deltachat-jsonrpc/src/lib.rs @@ -1,5 +1,6 @@ #![recursion_limit = "256"] #![cfg_attr(not(test), forbid(clippy::indexing_slicing))] +#![cfg_attr(not(test), forbid(clippy::string_slice))] pub mod api; pub use yerpc; diff --git a/format-flowed/src/lib.rs b/format-flowed/src/lib.rs index 055dd66778..4d246b9c07 100644 --- a/format-flowed/src/lib.rs +++ b/format-flowed/src/lib.rs @@ -9,6 +9,7 @@ //! //! For received messages, DelSp parameter is honoured. #![cfg_attr(not(test), forbid(clippy::indexing_slicing))] +#![cfg_attr(not(test), forbid(clippy::string_slice))] /// Wraps line to 72 characters using format=flowed soft breaks. /// diff --git a/src/lib.rs b/src/lib.rs index 7124bd5eb2..86c79ebdaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ clippy::cloned_instead_of_copied )] #![cfg_attr(not(test), forbid(clippy::indexing_slicing))] +#![cfg_attr(not(test), forbid(clippy::string_slice))] #![allow( clippy::match_bool, clippy::mixed_read_write_in_expression, diff --git a/src/qr.rs b/src/qr.rs index 30f0eb4e69..ed76947b0f 100644 --- a/src/qr.rs +++ b/src/qr.rs @@ -807,11 +807,7 @@ async fn decode_mailto(context: &Context, qr: &str) -> Result { .get(MAILTO_SCHEME.len()..) .context("Invalid mailto: scheme")?; - let (addr, query) = if let Some(query_index) = payload.find('?') { - (&payload[..query_index], &payload[query_index + 1..]) - } else { - (payload, "") - }; + let (addr, query) = payload.split_once('?').unwrap_or((payload, "")); let param: BTreeMap<&str, &str> = query .split('&') @@ -861,12 +857,9 @@ async fn decode_mailto(context: &Context, qr: &str) -> Result { async fn decode_smtp(context: &Context, qr: &str) -> Result { let payload = qr.get(SMTP_SCHEME.len()..).context("Invalid SMTP scheme")?; - let addr = if let Some(query_index) = payload.find(':') { - &payload[..query_index] - } else { - bail!("Invalid SMTP found"); - }; - + let (addr, _rest) = payload + .split_once(':') + .context("Invalid SMTP scheme payload")?; let addr = normalize_address(addr)?; let name = ""; Qr::from_address(context, name, &addr, None).await diff --git a/src/securejoin.rs b/src/securejoin.rs index 863d2b4f2f..96631fe01a 100644 --- a/src/securejoin.rs +++ b/src/securejoin.rs @@ -347,7 +347,7 @@ pub(crate) async fn handle_securejoin_handshake( send_alice_handshake_msg( context, contact_id, - &format!("{}-auth-required", &step[..2]), + &format!("{}-auth-required", &step.get(..2).unwrap_or_default()), ) .await .context("failed sending auth-required handshake message")?; diff --git a/src/tools.rs b/src/tools.rs index ef460316c7..bad4cb2529 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -47,20 +47,27 @@ use crate::stock_str; /// end of the shortened string. pub(crate) fn truncate(buf: &str, approx_chars: usize) -> Cow { let count = buf.chars().count(); - if count > approx_chars + DC_ELLIPSIS.len() { - let end_pos = buf - .char_indices() - .nth(approx_chars) - .map(|(n, _)| n) - .unwrap_or_default(); - - if let Some(index) = buf[..end_pos].rfind([' ', '\n']) { - Cow::Owned(format!("{}{}", &buf[..=index], DC_ELLIPSIS)) - } else { - Cow::Owned(format!("{}{}", &buf[..end_pos], DC_ELLIPSIS)) - } + if count <= approx_chars + DC_ELLIPSIS.len() { + return Cow::Borrowed(buf); + } + let end_pos = buf + .char_indices() + .nth(approx_chars) + .map(|(n, _)| n) + .unwrap_or_default(); + + if let Some(index) = buf.get(..end_pos).and_then(|s| s.rfind([' ', '\n'])) { + Cow::Owned(format!( + "{}{}", + &buf.get(..=index).unwrap_or_default(), + DC_ELLIPSIS + )) } else { - Cow::Borrowed(buf) + Cow::Owned(format!( + "{}{}", + &buf.get(..end_pos).unwrap_or_default(), + DC_ELLIPSIS + )) } }