summaryrefslogtreecommitdiffstats
path: root/openpgp
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 /openpgp
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
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/src/armor.rs8
1 files changed, 4 insertions, 4 deletions
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