Address further clippy lints
authorMatt Corallo <git@bluematt.me>
Tue, 7 May 2024 17:28:16 +0000 (17:28 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 7 May 2024 17:30:52 +0000 (17:30 +0000)
src/base32.rs
src/http.rs
src/lib.rs
src/query.rs
src/rr.rs
src/ser.rs
src/validation.rs

index 881b914dc16adb3aa32e0d3e5395c69ed0d2894a..43f40dd374e55c51022c0416e94196bca1965786 100644 (file)
@@ -43,7 +43,7 @@ pub fn decode(data: &str) -> Result<Vec<u8>, ()> {
        // 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.
index 0656c4c7369ad1efadda51314152e87fd5ec9505..eb97c12568ce18693e110d3e072888bfd32a8b86 100644 (file)
@@ -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
index c402a720bd49e3b512d34c1db084b10a32ba743c..91a315678f18f107a1a1e955e5c636121f9bc1db 100644 (file)
@@ -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
index bb724020ea98f792f1631ca20d89616415d9aa75..388b96173c45779a11aa0ccc4a2002acfd1caeb9 100644 (file)
@@ -150,7 +150,7 @@ fn handle_response(resp: &[u8], proof: &mut Vec<u8>, rrsig_key_names: &mut Vec<N
        // Only read the answers and NSEC records in authorities, skipping additional entirely.
        let mut min_ttl = u32::MAX;
        for _ in 0..answers {
-               let (rr, ttl) = parse_wire_packet_rr(&mut read, &resp)?;
+               let (rr, ttl) = parse_wire_packet_rr(&mut read, resp)?;
                write_rr(&rr, ttl, proof);
                min_ttl = cmp::min(min_ttl, ttl);
                if let RR::RRSig(rrsig) = rr { rrsig_key_names.push(rrsig.key_name); }
@@ -159,7 +159,7 @@ fn handle_response(resp: &[u8], proof: &mut Vec<u8>, rrsig_key_names: &mut Vec<N
        for _ in 0..authorities {
                // Only include records from the authority section if they are NSEC/3 (or signatures
                // thereover). We don't care about NS records here.
-               let (rr, ttl) = parse_wire_packet_rr(&mut read, &resp)?;
+               let (rr, ttl) = parse_wire_packet_rr(&mut read, resp)?;
                match &rr {
                        RR::RRSig(rrsig) => {
                                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;
 
index b725b378c6ef7bbbee1d37b788b148faaf494719..0733dd2fa2897d79836a5632e4d5c9d33d7df93b 100644 (file)
--- 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<String> 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<Ordering> {
-               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<Ordering> { 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<Self, ()> {
                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<W: Writer>(&self, out: &mut W) {
@@ -844,7 +847,7 @@ impl StaticRecord for AAAA {
        fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
                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<W: Writer>(&self, out: &mut W) {
index dfd89932e6afd15e46591504dd3f098c7885697a..3cb3c89f2814bddd05566cd9ad88f49bbcf4a62d 100644 (file)
@@ -7,7 +7,7 @@ use crate::rr::*;
 use crate::query::QueryBuf;
 
 pub(crate) fn read_u8(inp: &mut &[u8]) -> Result<u8, ()> {
-       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<u32, ()> {
 }
 
 pub(crate) fn read_u8_len_prefixed_bytes(inp: &mut &[u8]) -> Result<Vec<u8>, ()> {
-       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<Vec<u8>, ()>
 pub(crate) fn write_nsec_types_bitmap<W: Writer>(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<Name, ()> {
        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<W: Writer>(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
index 57ba096d53bd64169eb213fbaed47c7c31925da8..ecca8f89cbba8f12fcdfe551e6c8623936005dc0 100644 (file)
@@ -173,7 +173,7 @@ where RI: IntoIterator<IntoIter = R>, R: Iterator<Item = &'r RRSig>,
                                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<IntoIter = R>, R: Iterator<Item = &'r RRSig>,
 
        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<VerifiedRRStream<'a>, 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<VerifiedRRStream<'a>, 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<VerifiedRRStream<'a>, 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();
                }
        }
 }