From d7cb7da07661ce42c36ba2dd4bc0edcad11a7e81 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 27 Sep 2021 18:49:32 +0300 Subject: Join nested if statements with logical and into one statement Instead of this: if foo { if bar { ... } } do this: if foo && bar { ... } Nesting statements implies a more complicated code structure than it really is. Thus it's arguably simpler to write a combined condition by joining the two conditions with a logical and operation. Found by clippy lint collapsible_if: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if --- openpgp/src/cert/amalgamation/key/iter.rs | 48 ++++++++++++------------------- openpgp/src/cert/builder.rs | 36 +++++++++++------------ openpgp/src/cert/parser/mod.rs | 8 ++---- openpgp/src/packet/signature.rs | 12 +++----- openpgp/src/parse.rs | 10 +++---- openpgp/src/parse/stream.rs | 8 ++---- openpgp/src/regex/mod.rs | 12 +++----- openpgp/src/serialize/stream.rs | 8 ++---- 8 files changed, 55 insertions(+), 87 deletions(-) (limited to 'openpgp/src') diff --git a/openpgp/src/cert/amalgamation/key/iter.rs b/openpgp/src/cert/amalgamation/key/iter.rs index 370a4e43..d2ec0fe6 100644 --- a/openpgp/src/cert/amalgamation/key/iter.rs +++ b/openpgp/src/cert/amalgamation/key/iter.rs @@ -175,11 +175,9 @@ impl<'a, P, R> KeyAmalgamationIter<'a, P, R> t!("PK algo is supported... skipping."); continue; } - } else { - if want_supported { - t!("PK algo is not supported... skipping."); - continue; - } + } else if want_supported { + t!("PK algo is not supported... skipping."); + continue; } } @@ -190,11 +188,9 @@ impl<'a, P, R> KeyAmalgamationIter<'a, P, R> t!("Have a secret... skipping."); continue; } - } else { - if want_secret { - t!("No secret... skipping."); - continue; - } + } else if want_secret { + t!("No secret... skipping."); + continue; } } @@ -205,11 +201,9 @@ impl<'a, P, R> KeyAmalgamationIter<'a, P, R> t!("Unencrypted secret... skipping."); continue; } - } else { - if want_unencrypted_secret { - t!("Encrypted secret... skipping."); - continue; - } + } else if want_unencrypted_secret { + t!("Encrypted secret... skipping."); + continue; } } else { // No secret. @@ -797,11 +791,9 @@ impl<'a, P, R> ValidKeyAmalgamationIter<'a, P, R> t!("PK algo is supported... skipping."); continue; } - } else { - if want_supported { - t!("PK algo is not supported... skipping."); - continue; - } + } else if want_supported { + t!("PK algo is not supported... skipping."); + continue; } } @@ -843,11 +835,9 @@ impl<'a, P, R> ValidKeyAmalgamationIter<'a, P, R> t!("Have a secret... skipping."); continue; } - } else { - if want_secret { - t!("No secret... skipping."); - continue; - } + } else if want_secret { + t!("No secret... skipping."); + continue; } } @@ -858,11 +848,9 @@ impl<'a, P, R> ValidKeyAmalgamationIter<'a, P, R> t!("Unencrypted secret... skipping."); continue; } - } else { - if want_unencrypted_secret { - t!("Encrypted secret... skipping."); - continue; - } + } else if want_unencrypted_secret { + t!("Encrypted secret... skipping."); + continue; } } else { // No secret. diff --git a/openpgp/src/cert/builder.rs b/openpgp/src/cert/builder.rs index 0ceb0579..235f2cd9 100644 --- a/openpgp/src/cert/builder.rs +++ b/openpgp/src/cert/builder.rs @@ -1336,17 +1336,15 @@ impl CertBuilder<'_> { a.remove_all(SubpacketTag::PrimaryUserID); Ok(a) })?; + } else if have_primary_user_thing { + // Check if this is the first explicitly selected + // user thing. + emitted_primary_user_thing |= + sig.primary_userid().unwrap_or(false); } else { - if have_primary_user_thing { - // Check if this is the first explicitly selected - // user thing. - emitted_primary_user_thing |= - sig.primary_userid().unwrap_or(false); - } else { - // Implicitly mark the first as primary. - sig = sig.set_primary_userid(true)?; - emitted_primary_user_thing = true; - } + // Implicitly mark the first as primary. + sig = sig.set_primary_userid(true)?; + emitted_primary_user_thing = true; } let signature = uid.bind(&mut signer, &cert, sig)?; @@ -1368,17 +1366,15 @@ impl CertBuilder<'_> { a.remove_all(SubpacketTag::PrimaryUserID); Ok(a) })?; + } else if have_primary_user_thing { + // Check if this is the first explicitly selected + // user thing. + emitted_primary_user_thing |= + sig.primary_userid().unwrap_or(false); } else { - if have_primary_user_thing { - // Check if this is the first explicitly selected - // user thing. - emitted_primary_user_thing |= - sig.primary_userid().unwrap_or(false); - } else { - // Implicitly mark the first as primary. - sig = sig.set_primary_userid(true)?; - emitted_primary_user_thing = true; - } + // Implicitly mark the first as primary. + sig = sig.set_primary_userid(true)?; + emitted_primary_user_thing = true; } let signature = ua.bind(&mut signer, &cert, sig)?; diff --git a/openpgp/src/cert/parser/mod.rs b/openpgp/src/cert/parser/mod.rs index 02516428..15afbdd9 100644 --- a/openpgp/src/cert/parser/mod.rs +++ b/openpgp/src/cert/parser/mod.rs @@ -904,12 +904,10 @@ pub(crate) fn split_sigs(primary: &KeyHandle, primary_keyid: &KeyHandle, } else { other_revs.push(sig); } + } else if is_selfsig { + self_signatures.push(sig); } else { - if is_selfsig { - self_signatures.push(sig); - } else { - certifications.push(sig); - } + certifications.push(sig); } } diff --git a/openpgp/src/packet/signature.rs b/openpgp/src/packet/signature.rs index 2db99a39..359ea7a5 100644 --- a/openpgp/src/packet/signature.rs +++ b/openpgp/src/packet/signature.rs @@ -2316,10 +2316,8 @@ impl crate::packet::Signature { }; let l = p.serialized_len(); - if size + l <= std::u16::MAX as usize { - if acc.insert(p.clone()) { - size += l; - } + if size + l <= std::u16::MAX as usize && acc.insert(p.clone()) { + size += l; } } @@ -2343,10 +2341,8 @@ impl crate::packet::Signature { .filter(|p| eligible(p) && ! p.authenticated() && ! prefer(p))) { let l = p.serialized_len(); - if size + l <= std::u16::MAX as usize { - if acc.insert(p.clone()) { - size += l; - } + if size + l <= std::u16::MAX as usize && acc.insert(p.clone()) { + size += l; } } assert!(size <= std::u16::MAX as usize); diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 8a323b89..5cf7c5f5 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -2229,12 +2229,10 @@ impl Key4 format!("Unexpected secret key found in {:?} packet", tag) ).into()); } - } else { - if tag == Tag::SecretKey || tag == Tag::SecretSubkey { - return php.error(Error::MalformedPacket( - format!("Expected secret key in {:?} packet", tag) - ).into()); - } + } else if tag == Tag::SecretKey || tag == Tag::SecretSubkey { + return php.error(Error::MalformedPacket( + format!("Expected secret key in {:?} packet", tag) + ).into()); } fn k(creation_time: u32, diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs index efb5755e..8d92dab5 100644 --- a/openpgp/src/parse/stream.rs +++ b/openpgp/src/parse/stream.rs @@ -2325,11 +2325,9 @@ impl<'a, H: VerificationHelper + DecryptionHelper> Decryptor<'a, H> { format!("Expected signature, got {}", pp.packet.tag())) .into()); } - } else { - if let Err(err) = pp.possible_message() { - t!("Malformed message: {}", err); - return Err(err.context("Malformed OpenPGP message")); - } + } else if let Err(err) = pp.possible_message() { + t!("Malformed message: {}", err); + return Err(err.context("Malformed OpenPGP message")); } let sym_algo_hint = if let Packet::AED(ref aed) = pp.packet { diff --git a/openpgp/src/regex/mod.rs b/openpgp/src/regex/mod.rs index a7e911de..0584bcbe 100644 --- a/openpgp/src/regex/mod.rs +++ b/openpgp/src/regex/mod.rs @@ -453,10 +453,8 @@ impl Regex { /// /// [`char::is_control`]: https://doc.rust-lang.org/std/primitive.char.html#method.is_control pub fn is_match(&self, s: &str) -> bool { - if ! self.disable_sanitizations { - if s.chars().any(char::is_control) { - return false; - } + if ! self.disable_sanitizations && s.chars().any(char::is_control) { + return false; } self.is_match_clean(s) @@ -975,10 +973,8 @@ impl RegexSet { /// # Ok(()) } /// ``` pub fn is_match(&self, s: &str) -> bool { - if ! self.disable_sanitizations { - if s.chars().any(char::is_control) { - return false; - } + if ! self.disable_sanitizations && s.chars().any(char::is_control) { + return false; } match self.re_set { diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs index 53068489..cbfa1117 100644 --- a/openpgp/src/serialize/stream.rs +++ b/openpgp/src/serialize/stream.rs @@ -1409,12 +1409,10 @@ impl<'a> Write for Signer<'a> { // later, we will hash it then. Otherwise, it is // implicitly omitted when the signer is finalized. self.hash_stash.extend_from_slice(&data[l..]); + } else if self.template.typ() == SignatureType::Text { + crate::parse::hash_update_text(&mut self.hash, data); } else { - if self.template.typ() == SignatureType::Text { - crate::parse::hash_update_text(&mut self.hash, data); - } else { - self.hash.update(data); - } + self.hash.update(data); } self.position += amount as u64; } -- cgit v1.2.3