Skip to content

Commit c18a476

Browse files
committed
refactor: forbid clippy::string_slice
1 parent 3235c8b commit c18a476

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

deltachat-contact-tools/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
clippy::cloned_instead_of_copied
1717
)]
1818
#![cfg_attr(not(test), forbid(clippy::indexing_slicing))]
19+
#![cfg_attr(not(test), forbid(clippy::string_slice))]
1920
#![allow(
2021
clippy::match_bool,
2122
clippy::mixed_read_write_in_expression,

deltachat-jsonrpc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![recursion_limit = "256"]
22
#![cfg_attr(not(test), forbid(clippy::indexing_slicing))]
3+
#![cfg_attr(not(test), forbid(clippy::string_slice))]
34
pub mod api;
45
pub use yerpc;
56

format-flowed/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//!
1010
//! For received messages, DelSp parameter is honoured.
1111
#![cfg_attr(not(test), forbid(clippy::indexing_slicing))]
12+
#![cfg_attr(not(test), forbid(clippy::string_slice))]
1213

1314
/// Wraps line to 72 characters using format=flowed soft breaks.
1415
///

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
clippy::cloned_instead_of_copied
1818
)]
1919
#![cfg_attr(not(test), forbid(clippy::indexing_slicing))]
20+
#![cfg_attr(not(test), forbid(clippy::string_slice))]
2021
#![allow(
2122
clippy::match_bool,
2223
clippy::mixed_read_write_in_expression,

src/qr.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -807,11 +807,7 @@ async fn decode_mailto(context: &Context, qr: &str) -> Result<Qr> {
807807
.get(MAILTO_SCHEME.len()..)
808808
.context("Invalid mailto: scheme")?;
809809

810-
let (addr, query) = if let Some(query_index) = payload.find('?') {
811-
(&payload[..query_index], &payload[query_index + 1..])
812-
} else {
813-
(payload, "")
814-
};
810+
let (addr, query) = payload.split_once('?').unwrap_or((payload, ""));
815811

816812
let param: BTreeMap<&str, &str> = query
817813
.split('&')
@@ -861,12 +857,9 @@ async fn decode_mailto(context: &Context, qr: &str) -> Result<Qr> {
861857
async fn decode_smtp(context: &Context, qr: &str) -> Result<Qr> {
862858
let payload = qr.get(SMTP_SCHEME.len()..).context("Invalid SMTP scheme")?;
863859

864-
let addr = if let Some(query_index) = payload.find(':') {
865-
&payload[..query_index]
866-
} else {
867-
bail!("Invalid SMTP found");
868-
};
869-
860+
let (addr, _rest) = payload
861+
.split_once(':')
862+
.context("Invalid SMTP scheme payload")?;
870863
let addr = normalize_address(addr)?;
871864
let name = "";
872865
Qr::from_address(context, name, &addr, None).await

src/securejoin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub(crate) async fn handle_securejoin_handshake(
347347
send_alice_handshake_msg(
348348
context,
349349
contact_id,
350-
&format!("{}-auth-required", &step[..2]),
350+
&format!("{}-auth-required", &step.get(..2).unwrap_or_default()),
351351
)
352352
.await
353353
.context("failed sending auth-required handshake message")?;

src/tools.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,27 @@ use crate::stock_str;
4747
/// end of the shortened string.
4848
pub(crate) fn truncate(buf: &str, approx_chars: usize) -> Cow<str> {
4949
let count = buf.chars().count();
50-
if count > approx_chars + DC_ELLIPSIS.len() {
51-
let end_pos = buf
52-
.char_indices()
53-
.nth(approx_chars)
54-
.map(|(n, _)| n)
55-
.unwrap_or_default();
56-
57-
if let Some(index) = buf[..end_pos].rfind([' ', '\n']) {
58-
Cow::Owned(format!("{}{}", &buf[..=index], DC_ELLIPSIS))
59-
} else {
60-
Cow::Owned(format!("{}{}", &buf[..end_pos], DC_ELLIPSIS))
61-
}
50+
if count <= approx_chars + DC_ELLIPSIS.len() {
51+
return Cow::Borrowed(buf);
52+
}
53+
let end_pos = buf
54+
.char_indices()
55+
.nth(approx_chars)
56+
.map(|(n, _)| n)
57+
.unwrap_or_default();
58+
59+
if let Some(index) = buf.get(..end_pos).and_then(|s| s.rfind([' ', '\n'])) {
60+
Cow::Owned(format!(
61+
"{}{}",
62+
&buf.get(..=index).unwrap_or_default(),
63+
DC_ELLIPSIS
64+
))
6265
} else {
63-
Cow::Borrowed(buf)
66+
Cow::Owned(format!(
67+
"{}{}",
68+
&buf.get(..end_pos).unwrap_or_default(),
69+
DC_ELLIPSIS
70+
))
6471
}
6572
}
6673

0 commit comments

Comments
 (0)