From 78af60e0fbddbf73c44a7a46952c42b0a57be2cb Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 7 May 2024 17:28:16 +0000 Subject: [PATCH] Address further clippy lints --- src/base32.rs | 2 +- src/http.rs | 3 +++ src/lib.rs | 3 +++ src/query.rs | 6 +++--- src/rr.rs | 21 ++++++++++++--------- src/ser.rs | 14 +++++++------- src/validation.rs | 24 ++++++++++++------------ 7 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/base32.rs b/src/base32.rs index 881b914..43f40dd 100644 --- a/src/base32.rs +++ b/src/base32.rs @@ -43,7 +43,7 @@ pub fn decode(data: &str) -> Result, ()> { // If the string has more characters than are required to alphabet_encode the number of bytes // decodable, treat the string as invalid. match data.len() % 8 { 1|3|6 => return Err(()), _ => {} } - Ok(decode_data(data, RFC4648_INV_ALPHABET)?) + decode_data(data, RFC4648_INV_ALPHABET) } /// Encode a byte slice into a base32 string. diff --git a/src/http.rs b/src/http.rs index 0656c4c..eb97c12 100644 --- a/src/http.rs +++ b/src/http.rs @@ -6,6 +6,9 @@ // Luckily, it seems to work fine in 1.63 with the feature flag (and RUSTC_BOOTSTRAP=1) enabled. #![cfg_attr(all(feature = "validation", rust_1_63), feature(const_slice_from_raw_parts))] +#![allow(clippy::new_without_default)] // why is this even a lint +#![allow(clippy::result_unit_err)] // Why in the hell is this a lint? +#![allow(clippy::get_first)] // Sometimes this improves readability #![allow(clippy::needless_lifetimes)] // lifetimes improve readability #![allow(clippy::needless_borrow)] // borrows indicate read-only/non-move #![allow(clippy::too_many_arguments)] // sometimes we don't have an option diff --git a/src/lib.rs b/src/lib.rs index c402a72..91a3156 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,9 @@ // Luckily, it seems to work fine in 1.63 with the feature flag (and RUSTC_BOOTSTRAP=1) enabled. #![cfg_attr(rust_1_63, feature(const_slice_from_raw_parts))] +#![allow(clippy::new_without_default)] // why is this even a lint +#![allow(clippy::result_unit_err)] // Why in the hell is this a lint? +#![allow(clippy::get_first)] // Sometimes this improves readability #![allow(clippy::needless_lifetimes)] // lifetimes improve readability #![allow(clippy::needless_borrow)] // borrows indicate read-only/non-move #![allow(clippy::too_many_arguments)] // sometimes we don't have an option diff --git a/src/query.rs b/src/query.rs index bb72402..388b961 100644 --- a/src/query.rs +++ b/src/query.rs @@ -150,7 +150,7 @@ fn handle_response(resp: &[u8], proof: &mut Vec, rrsig_key_names: &mut Vec, rrsig_key_names: &mut Vec { if rrsig.ty != NSec::TYPE && rrsig.ty != NSec3::TYPE { @@ -251,7 +251,7 @@ impl ProofBuilder { if self.pending_queries == 0 { return Err(()); } let mut rrsig_key_names = Vec::new(); - let min_ttl = handle_response(&resp, &mut self.proof, &mut rrsig_key_names)?; + let min_ttl = handle_response(resp, &mut self.proof, &mut rrsig_key_names)?; self.min_ttl = cmp::min(self.min_ttl, min_ttl); self.pending_queries -= 1; diff --git a/src/rr.rs b/src/rr.rs index b725b37..0733dd2 100644 --- a/src/rr.rs +++ b/src/rr.rs @@ -41,7 +41,7 @@ impl Name { } else if n == 0 { Some(".") } else { - self.as_str().splitn(labels as usize - n as usize + 1, ".").last() + self.as_str().splitn(labels as usize - n as usize + 1, '.').last() } } } @@ -61,7 +61,7 @@ impl TryFrom for Name { if *s.as_bytes().last().unwrap_or(&0) != b"."[0] { return Err(()); } if s.len() > 255 { return Err(()); } if s.chars().any(|c| !c.is_ascii_graphic() || c == '"') { return Err(()); } - for label in s.split(".") { + for label in s.split('.') { if label.len() > 63 { return Err(()); } } @@ -259,7 +259,7 @@ impl Record for RR { } } -#[derive(Debug, Clone, PartialEq, Eq, Ord)] +#[derive(Debug, Clone, PartialEq, Eq)] /// A text resource record, containing arbitrary text data pub struct Txt { /// The name this record is at. @@ -272,9 +272,9 @@ pub struct Txt { } /// The wire type for TXT records pub const TXT_TYPE: u16 = 16; -impl PartialOrd for Txt { - fn partial_cmp(&self, o: &Txt) -> Option { - Some(self.name.cmp(&o.name) +impl Ord for Txt { + fn cmp(&self, o: &Txt) -> Ordering { + self.name.cmp(&o.name) .then_with(|| { // Compare in wire encoding form, i.e. compare in 255-byte chunks for i in 1..(self.data.len() / 255) + 2 { @@ -286,9 +286,12 @@ impl PartialOrd for Txt { if !slice_cmp.is_eq() { return slice_cmp; } } Ordering::Equal - })) + }) } } +impl PartialOrd for Txt { + fn partial_cmp(&self, o: &Txt) -> Option { Some(self.cmp(o)) } +} impl StaticRecord for Txt { const TYPE: u16 = TXT_TYPE; fn name(&self) -> &Name { &self.name } @@ -816,7 +819,7 @@ impl StaticRecord for A { fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result { if data.len() != 4 { return Err(()); } let mut address = [0; 4]; - address.copy_from_slice(&data); + address.copy_from_slice(data); Ok(A { name, address }) } fn write_u16_len_prefixed_data(&self, out: &mut W) { @@ -844,7 +847,7 @@ impl StaticRecord for AAAA { fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result { if data.len() != 16 { return Err(()); } let mut address = [0; 16]; - address.copy_from_slice(&data); + address.copy_from_slice(data); Ok(AAAA { name, address }) } fn write_u16_len_prefixed_data(&self, out: &mut W) { diff --git a/src/ser.rs b/src/ser.rs index dfd8993..3cb3c89 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -7,7 +7,7 @@ use crate::rr::*; use crate::query::QueryBuf; pub(crate) fn read_u8(inp: &mut &[u8]) -> Result { - let res = *inp.get(0).ok_or(())?; + let res = *inp.first().ok_or(())?; *inp = &inp[1..]; Ok(res) } @@ -27,7 +27,7 @@ pub(crate) fn read_u32(inp: &mut &[u8]) -> Result { } pub(crate) fn read_u8_len_prefixed_bytes(inp: &mut &[u8]) -> Result, ()> { - let len = *inp.get(0).ok_or(())?; + let len = *inp.first().ok_or(())?; *inp = &inp[1..]; if inp.len() < len.into() { return Err(()); } let mut res = Vec::with_capacity(len.into()); @@ -39,7 +39,7 @@ pub(crate) fn read_u8_len_prefixed_bytes(inp: &mut &[u8]) -> Result, ()> pub(crate) fn write_nsec_types_bitmap(out: &mut W, types: &[u8; 8192]) { for (idx, flags) in types.chunks(32).enumerate() { debug_assert_eq!(flags.len(), 32); - if flags != &[0; 32] { + if flags != [0; 32] { let last_nonzero_idx = flags.iter().rposition(|flag| *flag != 0) .unwrap_or_else(|| { debug_assert!(false); 0 }); out.write(&(idx as u8).to_be_bytes()); @@ -52,7 +52,7 @@ pub(crate) fn nsec_types_bitmap_len(types: &[u8; 8192]) -> u16 { let mut total_len = 0; for flags in types.chunks(32) { debug_assert_eq!(flags.len(), 32); - if flags != &[0; 32] { + if flags != [0; 32] { total_len += 3 + flags.iter().rposition(|flag| *flag != 0) .unwrap_or_else(|| { debug_assert!(false); 0 }) as u16; } @@ -102,7 +102,7 @@ fn read_wire_packet_labels(inp: &mut &[u8], wire_packet: &[u8], name: &mut Strin pub(crate) fn read_wire_packet_name(inp: &mut &[u8], wire_packet: &[u8]) -> Result { let mut name = String::with_capacity(1024); read_wire_packet_labels(inp, wire_packet, &mut name)?; - Ok(name.try_into()?) + name.try_into() } pub(crate) trait Writer { fn write(&mut self, buf: &[u8]); } @@ -115,7 +115,7 @@ pub(crate) fn write_name(out: &mut W, name: &str) { if canonical_name == "." { out.write(&[0]); } else { - for label in canonical_name.split(".") { + for label in canonical_name.split('.') { out.write(&(label.len() as u8).to_be_bytes()); out.write(label.as_bytes()); } @@ -126,7 +126,7 @@ pub(crate) fn name_len(name: &Name) -> u16 { 1 } else { let mut res = 0; - for label in name.split(".") { + for label in name.split('.') { res += 1 + label.len(); } res as u16 diff --git a/src/validation.rs b/src/validation.rs index 57ba096..ecca8f8 100644 --- a/src/validation.rs +++ b/src/validation.rs @@ -173,7 +173,7 @@ where RI: IntoIterator, R: Iterator, ctx.update(&dnskey.alg.to_be_bytes()); ctx.update(&dnskey.pubkey); let hash = ctx.finish(); - if hash.as_ref() == &ds.digest { + if hash.as_ref() == ds.digest { validated_dnskeys.push(*dnskey); break; } @@ -183,7 +183,7 @@ where RI: IntoIterator, R: Iterator, let mut found_unsupported_alg = false; for sig in sigs { - match verify_rrsig(sig, validated_dnskeys.iter().map(|k| *k), records.clone()) { + match verify_rrsig(sig, validated_dnskeys.iter().copied(), records.clone()) { Ok(()) => return Ok(sig), Err(ValidationError::UnsupportedAlgorithm) => { // There may be redundant signatures by different keys, where one we don't @@ -253,8 +253,8 @@ fn resolve_time(time: u32) -> u64 { } fn nsec_ord(a: &str, b: &str) -> Ordering { - let mut a_label_iter = a.rsplit("."); - let mut b_label_iter = b.rsplit("."); + let mut a_label_iter = a.rsplit('.'); + let mut b_label_iter = b.rsplit('.'); loop { match (a_label_iter.next(), b_label_iter.next()) { (Some(_), None) => return Ordering::Greater, @@ -267,11 +267,11 @@ fn nsec_ord(a: &str, b: &str) -> Ordering { (Some(_), None) => return Ordering::Greater, (None, Some(_)) => return Ordering::Less, (Some(mut a), Some(mut b)) => { - if a >= 'A' as u8 && a <= 'Z' as u8 { - a += 'a' as u8 - 'A' as u8; + if a.is_ascii_uppercase() { + a += b'a' - b'A'; } - if b >= 'A' as u8 && b <= 'Z' as u8 { - b += 'a' as u8 - 'A' as u8; + if b.is_ascii_uppercase() { + b += b'a' - b'A'; } if a != b { return a.cmp(&b); } }, @@ -411,7 +411,7 @@ pub fn verify_rr_stream<'a>(inp: &'a [RR]) -> Result, Valid .filter(|nsec| nsec.name.ends_with(zone.as_str())); for nsec in nsec_search { let name_matches = nsec.name.as_str() == name; - let name_contained = nsec_ord(&nsec.name, &name) != Ordering::Greater && + let name_contained = nsec_ord(&nsec.name, name) != Ordering::Greater && nsec_ord(&nsec.next_name, name) == Ordering::Greater; if (name_matches && !nsec.types.contains_type(ty)) || name_contained { rrs_needing_non_existence_proofs @@ -437,7 +437,7 @@ pub fn verify_rr_stream<'a>(inp: &'a [RR]) -> Result, Valid { continue; } let mut hasher = crypto::hash::Hasher::sha1(); - write_name(&mut hasher, &name); + write_name(&mut hasher, name); hasher.update(&nsec3.salt); for _ in 0..nsec3.hash_iterations { let res = hasher.finish(); @@ -467,7 +467,7 @@ pub fn verify_rr_stream<'a>(inp: &'a [RR]) -> Result, Valid hash } else { continue }; - let (start_hash_base32, _) = nsec3.name.split_once(".") + let (start_hash_base32, _) = nsec3.name.split_once('.') .unwrap_or_else(|| { debug_assert!(false); ("", "")}); let start_hash = if let Ok(start_hash) = base32::decode(start_hash_base32) { start_hash @@ -532,7 +532,7 @@ impl<'a> VerifiedRRStream<'a> { continue; } - return self.verified_rrs.iter().filter(|rr| rr.name() == name).map(|rr| *rr).collect(); + return self.verified_rrs.iter().filter(|rr| rr.name() == name).copied().collect(); } } } -- 2.30.2