From 798433cafd6d3b1e4c411f4e022277a6b8450746 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 21 Sep 2021 10:09:34 +0300 Subject: 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 --- openpgp/src/armor.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'openpgp/src/armor.rs') 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 -- cgit v1.2.3