summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-09-21 10:09:34 +0300
committerLars Wirzenius <liw@sequoia-pgp.org>2021-09-30 08:31:19 +0300
commit798433cafd6d3b1e4c411f4e022277a6b8450746 (patch)
treec2fe4dac6b69eff7867ab8ba9756fc6941c3c2c2
parent248e2f64897d67fab25f84a70b7af1af50d6174a (diff)
Use strip_{prefix,suffix} for code that's easier to follow
Instead of: if text.starts_with(prefix) { &text[prefix.len()..] } else { &text } use this: if let Some(rest) = text.strip_prefix(prefix) { rest } else { &text } The strip_prefix is easier to understand, and also removes the dependency between the if condition and the computation to get the slice after the prefix. The dependency is reasonably clear, but the compiler does not understand it so it's plausible that a future change might change the condition but not the slice. The approach using strip_prefix avoids that. This was found by the clippy lint manual_strip: https://rust-lang.github.io/rust-clippy/master/index.html#manual_strip
-rw-r--r--autocrypt/src/lib.rs5
-rw-r--r--openpgp/src/armor.rs8
2 files changed, 6 insertions, 7 deletions
diff --git a/autocrypt/src/lib.rs b/autocrypt/src/lib.rs
index 112f14f4..78f73486 100644
--- a/autocrypt/src/lib.rs
+++ b/autocrypt/src/lib.rs
@@ -265,9 +265,8 @@ impl AutocryptHeaders {
const AUTOCRYPT_GOSSIP : &str = "Autocrypt-Gossip: ";
const FROM : &str = "From: ";
- if line.starts_with(FROM) {
- headers.from
- = Some(line[FROM.len()..].trim_matches(' ').into());
+ if let Some(rest) = line.strip_prefix(FROM) {
+ headers.from = Some(rest.trim_matches(' ').into());
} else if line.starts_with(AUTOCRYPT) || line.starts_with(AUTOCRYPT_GOSSIP) {
headers.headers.push(Self::decode_autocrypt_like_header(&line));
}
diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs
index d009dacc..9534cf65 100644
--- a/openpgp/src/armor.rs
+++ b/openpgp/src/armor.rs
@@ -1072,12 +1072,12 @@ impl<'a> Reader<'a> {
// it couldn't is if we encountered EOF. We need to strip
// it. But, if it ends with \r\n, then we also want to
// strip the \r too.
- let line = if line.ends_with(&"\r\n"[..]) {
+ let line = if let Some(rest) = line.strip_suffix(&"\r\n"[..]) {
// \r\n.
- &line[..line.len() - 2]
- } else if line.ends_with('\n') {
+ rest
+ } else if let Some(rest) = line.strip_suffix('\n') {
// \n.
- &line[..line.len() - 1]
+ rest
} else {
// EOF.
line