diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-09-21 10:09:34 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@sequoia-pgp.org> | 2021-09-30 08:31:19 +0300 |
commit | 798433cafd6d3b1e4c411f4e022277a6b8450746 (patch) | |
tree | c2fe4dac6b69eff7867ab8ba9756fc6941c3c2c2 /autocrypt | |
parent | 248e2f64897d67fab25f84a70b7af1af50d6174a (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
Diffstat (limited to 'autocrypt')
-rw-r--r-- | autocrypt/src/lib.rs | 5 |
1 files changed, 2 insertions, 3 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)); } |