diff options
author | Lars Wirzenius <liw@sequoia-pgp.org> | 2021-09-29 13:06:57 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@sequoia-pgp.org> | 2021-09-30 08:31:19 +0300 |
commit | 248e2f64897d67fab25f84a70b7af1af50d6174a (patch) | |
tree | e04da2998c47bedada1293a758a451af47a964ee | |
parent | 730ac1e5cba491456c899d07e3813aedb61cc2b5 (diff) |
Use match for tri-state condition
A chain of if statements is not guaranteed to test every possible
case, the way match does. Thus it seems better to use match here, when
there are three possible results from a comparison.
Found by clippy lint comparison_chain:
https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
-rw-r--r-- | openpgp/src/crypto/mod.rs | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs index b54f156c..5bcf7692 100644 --- a/openpgp/src/crypto/mod.rs +++ b/openpgp/src/crypto/mod.rs @@ -18,6 +18,7 @@ //! [`SessionKey::new`]: SessionKey::new() //! [`KeyPair` example]: KeyPair#examples +use std::cmp::Ordering; use std::ops::{Deref, DerefMut}; use std::fmt; use std::borrow::Cow; @@ -249,17 +250,19 @@ impl Password { /// returned. pub(crate) fn pad(value: &[u8], to: usize) -> Result<Cow<[u8]>> { - if value.len() == to { - Ok(Cow::Borrowed(value)) - } else if value.len() < to { - let missing = to - value.len(); - let mut v = vec![0; to]; - v[missing..].copy_from_slice(value); - Ok(Cow::Owned(v)) - } else { - Err(Error::InvalidOperation( - format!("Input value is longer than expected: {} > {}", - value.len(), to)).into()) + match value.len().cmp(&to) { + Ordering::Equal => Ok(Cow::Borrowed(value)), + Ordering::Less => { + let missing = to - value.len(); + let mut v = vec![0; to]; + v[missing..].copy_from_slice(value); + Ok(Cow::Owned(v)) + } + Ordering::Greater => { + Err(Error::InvalidOperation( + format!("Input value is longer than expected: {} > {}", + value.len(), to)).into()) + } } } |