From 4e28e636a1cb4086f606cb4caf97accd27daeb96 Mon Sep 17 00:00:00 2001 From: Malte Meiboom Date: Mon, 27 Apr 2026 13:42:47 +0200 Subject: [PATCH] Make clippy happy - Applied many of the `clippy` suggestions. --- src/crypto.rs | 57 ++++++++++++++++++--------------------- src/daemon/mod.rs | 13 +++++---- src/types/husk_context.rs | 2 +- src/types/introducer.rs | 2 +- src/types/mail_context.rs | 7 +++++ src/types/recipient.rs | 6 ++--- 6 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index d6d0b23..120f964 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -46,10 +46,7 @@ pub fn get_local_trust_root(cert_store: &CertStore) -> anyhow::Result { Ok(mut parser) => { match parser.next() { Some(Ok(cert)) => { - match Cert::from_bytes(cert.as_bytes()) { - Ok(c) => Some(c), - Err(_) => None, - } + Cert::from_bytes(cert.as_bytes()).ok() }, Some(Err(_)) | None => None @@ -89,7 +86,7 @@ pub fn get_local_certificates<'hc>(context: &HuskContext<'hc>, email: &str) for (fpr, some_userid) in bindings { if let Some(userid) = some_userid { let paths = n.authenticate(userid, &fpr, wot::FULLY_TRUSTED); - if paths.len() > 0 { + if !paths.is_empty() { log::debug!("{} authenticated!!!", email); for (path, _) in paths.iter() { if let Ok(cert) = cert_store.lookup_by_cert_fpr(&path.target().fingerprint()) { @@ -125,34 +122,32 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str) let collect_certs = |certs: Vec>, email: String, policy: &StandardPolicy| { let mut result = Vec::new(); - for cert in certs { - if let Ok(c) = cert { - if let Ok(vc) = c.with_policy(policy, None) { + for cert in certs.into_iter().flatten() { + if let Ok(vc) = cert.with_policy(policy, None) { - // check if the returned certificate contains a userid with the - // email address in question. - let mut userid_found = false; - for userid in vc.userids() { - if let Ok(Some(u)) = userid.userid().email() { - if u == email { - userid_found = true; - } + // check if the returned certificate contains a userid with the + // email address in question. + let mut userid_found = false; + for userid in vc.userids() { + if let Ok(Some(u)) = userid.userid().email() { + if u == email { + userid_found = true; } } + } - if userid_found { - // check if the certificate can be used (for encryption). - vc.keys() - .supported() - .alive() - .revoked(false) - .for_transport_encryption() - .for_each(|_| { - // XXX: cert gets returned for each transport key - // once would be enough - result.push(c.clone()); - }); - } + if userid_found { + // check if the certificate can be used (for encryption). + vc.keys() + .supported() + .alive() + .revoked(false) + .for_transport_encryption() + .for_each(|_| { + // XXX: cert gets returned for each transport key + // once would be enough + result.push(cert.clone()); + }); } } } @@ -239,7 +234,7 @@ pub async fn get_certificates<'hc>(context: &HuskContext<'hc>, email: &str) log::debug!("get certificate for {}", email); let locals = get_local_certificates(context, email); - if locals.len() > 0 { + if !locals.is_empty() { return locals; } @@ -363,7 +358,7 @@ pub fn encrypt(context: &HuskContext<'_>, body: &Bytes, recipients: &Vec 0 { + if !keys.is_empty() { let mut sink = Vec::new(); let message = Message::new(&mut sink); let message = Armorer::new(message).build()?; diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index c007d77..22266f1 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -94,7 +94,7 @@ impl Daemon { match HuskContext::new(&config) { Ok(context) => { log::debug!("found introducers: {:?}", context.introducers); - cx.data = Some(context).take(); + cx.data = Some(context); Status::Continue }, Err(e) => { @@ -138,13 +138,12 @@ impl Daemon { break; } } - if rcpt.is_none() { - Status::Tempfail - } else { - context.add_recipient(rcpt.unwrap()).await; + if let Some(rcpt) = rcpt { + context.add_recipient(rcpt).await; Status::Continue + } else { + Status::Tempfail } - } else { Status::Tempfail } @@ -213,7 +212,7 @@ impl Daemon { if protection == ProtectionPossibility::Full { // if an error occures the body is not exchanged - match crypto::encrypt(&context, &body, &context.mail.recipients) { + match crypto::encrypt(context, &body, &context.mail.recipients) { Ok(encrypted) => { if cx.actions.replace_body(&encrypted).await.is_err() { log::error!("Cannot exchange body"); diff --git a/src/types/husk_context.rs b/src/types/husk_context.rs index eb3a264..e93229d 100644 --- a/src/types/husk_context.rs +++ b/src/types/husk_context.rs @@ -59,7 +59,7 @@ impl<'hc> HuskContext<'hc> { pub async fn add_recipient(&mut self, rcpt: String) { - let certs = crypto::get_certificates(&self, rcpt.as_str()).await; + let certs = crypto::get_certificates(self, rcpt.as_str()).await; self.mail.add_recipient(rcpt, certs) } diff --git a/src/types/introducer.rs b/src/types/introducer.rs index 7bbfdee..8ede07c 100644 --- a/src/types/introducer.rs +++ b/src/types/introducer.rs @@ -64,7 +64,7 @@ impl Display for Introducer { .map(|u| String::from_utf8_lossy(u.userid().value()).to_string()) .collect::>() .join(", "); - write!(f, "Introducer: {}\n", self.cert.fingerprint())?; + writeln!(f, "Introducer: {}", self.cert.fingerprint())?; write!(f, " UserIDs: {}", uids)?; Ok(()) } diff --git a/src/types/mail_context.rs b/src/types/mail_context.rs index 2e7556a..bd126b4 100644 --- a/src/types/mail_context.rs +++ b/src/types/mail_context.rs @@ -66,6 +66,13 @@ impl<'mc> MailContext<'mc> { } +/// Default Mailcontext +impl<'mc> Default for MailContext<'mc> { + fn default() -> Self { + Self::new() + } +} + impl From<&MailContext<'_>> for ProtectionPossibility { fn from(mail_context: &MailContext<'_>) -> ProtectionPossibility { let mut can_encrypt = false; diff --git a/src/types/recipient.rs b/src/types/recipient.rs index 4288b27..2dddddb 100644 --- a/src/types/recipient.rs +++ b/src/types/recipient.rs @@ -17,8 +17,8 @@ pub struct Recipient<'r> { impl From for Recipient<'_> { fn from(email: String) -> Self { - Recipient { - email: email, + Recipient { + email, certs: Vec::new() } } @@ -40,7 +40,7 @@ impl Recipient<'_> { /// check if this Recipient can encrypt. // XXX: currently this check is naive pub fn can_encrypt(&self) -> bool { - if self.certs.len() == 0 { + if self.certs.is_empty() { return false; }