From 0f849a1e171a1137d6aeff17ca35d50288144a70 Mon Sep 17 00:00:00 2001 From: Christopher Cole Date: Sun, 13 Nov 2022 13:14:12 -0800 Subject: [PATCH] Run cargo clippy --fix --- src/abi.rs | 4 ++-- src/elf_bytes.rs | 2 +- src/elf_stream.rs | 10 ++++----- src/file.rs | 20 ++++++++--------- src/gnu_symver.rs | 54 ++++++++++++++++++++++----------------------- src/hash.rs | 2 +- src/note.rs | 8 +++---- src/parse.rs | 10 ++++----- src/segment.rs | 2 +- src/string_table.rs | 10 ++------- 10 files changed, 57 insertions(+), 65 deletions(-) diff --git a/src/abi.rs b/src/abi.rs index 0eb0402..628bbd6 100644 --- a/src/abi.rs +++ b/src/abi.rs @@ -1373,7 +1373,7 @@ pub const R_ARM_IRELATIVE: u32 = 160; /// Object file compatibility attributes pub const SHT_AARCH64_ATTRIBUTES: u32 = 0x70000003; -pub const SHT_AARCH64_ATTRIBUTES_SECTION_NAME: &'static str = ".ARM.attributes"; +pub const SHT_AARCH64_ATTRIBUTES_SECTION_NAME: &str = ".ARM.attributes"; /// Architecture compatibility information. /// @@ -2147,7 +2147,7 @@ pub const EF_RISCV_RVE: u32 = 0x0008; pub const EF_RISCV_TSO: u32 = 0x0010; pub const SHT_RISCV_ATTRIBUTES: u32 = 0x70000003; // SHT_LOPROC + 3; -pub const SHT_RISCV_ATTRIBUTES_SECTION_NAME: &'static str = ".riscv.attributes"; +pub const SHT_RISCV_ATTRIBUTES_SECTION_NAME: &str = ".riscv.attributes"; pub const PT_RISCV_ATTRIBUTES: u32 = 0x70000003; diff --git a/src/elf_bytes.rs b/src/elf_bytes.rs index 9d5744f..63912ff 100644 --- a/src/elf_bytes.rs +++ b/src/elf_bytes.rs @@ -576,7 +576,7 @@ impl<'data, E: EndianParse> ElfBytes<'data, E> { /// This is the segment's data as found in the file. pub fn segment_data(&self, phdr: &ProgramHeader) -> Result<&'data [u8], ParseError> { let (start, end) = phdr.get_file_data_range()?; - Ok(self.data.get_bytes(start..end)?) + self.data.get_bytes(start..end) } /// Get the segment's file data for a given [ProgramHeader], and interpret it as an diff --git a/src/elf_stream.rs b/src/elf_stream.rs index efbd32c..2beb07a 100644 --- a/src/elf_stream.rs +++ b/src/elf_stream.rs @@ -174,7 +174,7 @@ impl ElfStream { &mut self, ) -> Result<(&Vec, Option), ParseError> { // It's Ok to have no section headers - if self.shdrs.len() == 0 { + if self.shdrs.is_empty() { return Ok((&self.shdrs, None)); } @@ -331,7 +331,7 @@ impl ElfStream { &mut self, symtab_type: u32, ) -> Result, StringTable)>, ParseError> { - if self.shdrs.len() == 0 { + if self.shdrs.is_empty() { return Ok(None); } @@ -385,7 +385,7 @@ impl ElfStream { /// Get the .dynamic section/segment contents. pub fn dynamic(&mut self) -> Result>, ParseError> { // If we have section headers, then look it up there - if self.shdrs.len() > 0 { + if !self.shdrs.is_empty() { if let Some(shdr) = self .shdrs .iter() @@ -400,7 +400,7 @@ impl ElfStream { ))); } // Otherwise, look up the PT_DYNAMIC segment (if any) - } else if self.phdrs.len() > 0 { + } else if !self.phdrs.is_empty() { if let Some(phdr) = self .phdrs .iter() @@ -427,7 +427,7 @@ impl ElfStream { /// Returns an empty Option if the object does not use symbol versioning. pub fn symbol_version_table(&mut self) -> Result>, ParseError> { // No sections means no GNU symbol versioning sections, which is ok - if self.shdrs.len() == 0 { + if self.shdrs.is_empty() { return Ok(None); } diff --git a/src/file.rs b/src/file.rs index 5957c12..9a94df9 100644 --- a/src/file.rs +++ b/src/file.rs @@ -95,7 +95,7 @@ fn verify_ident(buf: &[u8]) -> Result<(), ParseError> { ))); } - return Ok(()); + Ok(()) } pub fn parse_ident(data: &[u8]) -> Result<(E, Class, u8, u8), ParseError> { @@ -199,7 +199,7 @@ mod parse_tests { 0, 0, ]; - verify_ident(&mut data.as_ref()).expect("Expected Ok result"); + verify_ident(data.as_ref()).expect("Expected Ok result"); } #[test] @@ -222,7 +222,7 @@ mod parse_tests { 0, 0, ]; - let result = verify_ident(&mut data.as_ref()).expect_err("Expected an error"); + let result = verify_ident(data.as_ref()).expect_err("Expected an error"); assert!( matches!(result, ParseError::BadMagic(_)), "Unexpected Error type found: {result}" @@ -249,7 +249,7 @@ mod parse_tests { 0, 0, ]; - let result = verify_ident(&mut data.as_ref()).expect_err("Expected an error"); + let result = verify_ident(data.as_ref()).expect_err("Expected an error"); assert!( matches!(result, ParseError::BadMagic(_)), "Unexpected Error type found: {result}" @@ -276,7 +276,7 @@ mod parse_tests { 0, 0, ]; - let result = verify_ident(&mut data.as_ref()).expect_err("Expected an error"); + let result = verify_ident(data.as_ref()).expect_err("Expected an error"); assert!( matches!(result, ParseError::BadMagic(_)), "Unexpected Error type found: {result}" @@ -303,7 +303,7 @@ mod parse_tests { 0, 0, ]; - let result = verify_ident(&mut data.as_ref()).expect_err("Expected an error"); + let result = verify_ident(data.as_ref()).expect_err("Expected an error"); assert!( matches!(result, ParseError::BadMagic(_)), "Unexpected Error type found: {result}" @@ -331,7 +331,7 @@ mod parse_tests { 0, 0, ]; - let result = verify_ident(&mut data.as_ref()).expect_err("Expected an error"); + let result = verify_ident(data.as_ref()).expect_err("Expected an error"); assert!( matches!(result, ParseError::UnsupportedVersion((42, 1))), "Unexpected Error type found: {result}" @@ -376,8 +376,8 @@ mod parse_tests { let tail = [0u8; ELF32_EHDR_TAILSIZE]; for n in 0..ELF32_EHDR_TAILSIZE { - let buf = tail.split_at(n).0.as_ref(); - let result = FileHeader::parse_tail(ident, &buf).expect_err("Expected an error"); + let buf = tail.split_at(n).0; + let result = FileHeader::parse_tail(ident, buf).expect_err("Expected an error"); assert!( matches!(result, ParseError::SliceReadError(_)), "Unexpected Error type found: {result:?}" @@ -424,7 +424,7 @@ mod parse_tests { for n in 0..ELF64_EHDR_TAILSIZE { let buf = tail.split_at(n).0; - let result = FileHeader::parse_tail(ident, &buf).expect_err("Expected an error"); + let result = FileHeader::parse_tail(ident, buf).expect_err("Expected an error"); assert!( matches!(result, ParseError::SliceReadError(_)), "Unexpected Error type found: {result:?}" diff --git a/src/gnu_symver.rs b/src/gnu_symver.rs index 04df6e2..96d0a62 100644 --- a/src/gnu_symver.rs +++ b/src/gnu_symver.rs @@ -5,7 +5,7 @@ use crate::file::Class; use crate::parse::{ParseAt, ParseError, ParsingTable}; use crate::string_table::StringTable; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct SymbolRequirement<'data> { pub file: &'data str, pub name: &'data str, @@ -73,7 +73,7 @@ impl<'data, E: EndianParse> SymbolVersionTable<'data, E> { }; let ver_ndx = self.version_ids.get(sym_idx)?; - let iter = verneeds.clone(); + let iter = verneeds; for (vn, vna_iter) in iter { for vna in vna_iter { if vna.vna_other != ver_ndx.index() { @@ -113,7 +113,7 @@ impl<'data, E: EndianParse> SymbolVersionTable<'data, E> { }; let ver_ndx = self.version_ids.get(sym_idx)?; - let iter = verdefs.clone(); + let iter = *verdefs; for (vd, vda_iter) in iter { if vd.vd_ndx != ver_ndx.index() { continue; @@ -165,7 +165,7 @@ pub type VersionIndexTable<'data, E> = ParsingTable<'data, E, VersionIndex>; /// structures in the .gnu.version_d and .gnu.version_r sections. These values /// are located in identifiers provided by the the vna_other member of the VerNeedAux /// structure or the vd_ndx member of the VerDef structure. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct VersionIndex(pub u16); impl VersionIndex { @@ -193,9 +193,7 @@ impl ParseAt for VersionIndex { offset: &mut usize, data: &[u8], ) -> Result { - Ok(VersionIndex { - 0: endian.parse_u16_at(offset, data)?, - }) + Ok(VersionIndex(endian.parse_u16_at(offset, data)?)) } #[inline] @@ -222,7 +220,7 @@ impl ParseAt for VersionIndex { /// /// The .gnu.version_d section shall contain an array of VerDef structures /// optionally followed by an array of VerDefAux structures. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct VerDef { /// Version information flag bitmask. pub vd_flags: u16, @@ -303,7 +301,7 @@ impl<'data, E: EndianParse> VerDefIterator<'data, E> { impl<'data, E: EndianParse> Iterator for VerDefIterator<'data, E> { type Item = (VerDef, VerDefAuxIterator<'data, E>); fn next(&mut self) -> Option { - if self.data.len() == 0 || self.count == 0 { + if self.data.is_empty() || self.count == 0 { return None; } @@ -334,7 +332,7 @@ impl<'data, E: EndianParse> Iterator for VerDefIterator<'data, E> { } /// Version Definition Auxiliary Entries from the .gnu.version_d section -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct VerDefAux { /// Offset to the version or dependency name string in the linked string table, in bytes. pub vda_name: u32, @@ -391,7 +389,7 @@ impl<'data, E: EndianParse> VerDefAuxIterator<'data, E> { impl<'data, E: EndianParse> Iterator for VerDefAuxIterator<'data, E> { type Item = VerDefAux; fn next(&mut self) -> Option { - if self.data.len() == 0 || self.count == 0 { + if self.data.is_empty() || self.count == 0 { return None; } @@ -457,7 +455,7 @@ impl<'data, E: EndianParse> Iterator for VerDefAuxIterator<'data, E> { /// /// The section shall contain an array of VerNeed structures optionally /// followed by an array of VerNeedAux structures. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct VerNeed { /// Number of associated verneed array entries. pub vn_cnt: u16, @@ -531,7 +529,7 @@ impl<'data, E: EndianParse> VerNeedIterator<'data, E> { impl<'data, E: EndianParse> Iterator for VerNeedIterator<'data, E> { type Item = (VerNeed, VerNeedAuxIterator<'data, E>); fn next(&mut self) -> Option { - if self.data.len() == 0 || self.count == 0 { + if self.data.is_empty() || self.count == 0 { return None; } @@ -562,7 +560,7 @@ impl<'data, E: EndianParse> Iterator for VerNeedIterator<'data, E> { } /// Version Need Auxiliary Entries from the .gnu.version_r section -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub struct VerNeedAux { /// Dependency name hash value (ELF hash function). pub vna_hash: u32, @@ -584,11 +582,11 @@ impl ParseAt for VerNeedAux { data: &[u8], ) -> Result { Ok(VerNeedAux { - vna_hash: endian.parse_u32_at(offset, &data)?, - vna_flags: endian.parse_u16_at(offset, &data)?, - vna_other: endian.parse_u16_at(offset, &data)?, - vna_name: endian.parse_u32_at(offset, &data)?, - vna_next: endian.parse_u32_at(offset, &data)?, + vna_hash: endian.parse_u32_at(offset, data)?, + vna_flags: endian.parse_u16_at(offset, data)?, + vna_other: endian.parse_u16_at(offset, data)?, + vna_name: endian.parse_u32_at(offset, data)?, + vna_next: endian.parse_u32_at(offset, data)?, }) } @@ -628,7 +626,7 @@ impl<'data, E: EndianParse> VerNeedAuxIterator<'data, E> { impl<'data, E: EndianParse> Iterator for VerNeedAuxIterator<'data, E> { type Item = VerNeedAux; fn next(&mut self) -> Option { - if self.data.len() == 0 || self.count == 0 { + if self.data.is_empty() || self.count == 0 { return None; } @@ -1178,22 +1176,22 @@ mod parse_tests { #[test] fn parse_verndx32_lsb() { - test_parse_for(LittleEndian, Class::ELF32, VersionIndex { 0: 0x0100 }); + test_parse_for(LittleEndian, Class::ELF32, VersionIndex(0x0100)); } #[test] fn parse_verndx32_msb() { - test_parse_for(BigEndian, Class::ELF32, VersionIndex { 0: 0x0001 }); + test_parse_for(BigEndian, Class::ELF32, VersionIndex(0x0001)); } #[test] fn parse_verndx64_lsb() { - test_parse_for(LittleEndian, Class::ELF64, VersionIndex { 0: 0x0100 }); + test_parse_for(LittleEndian, Class::ELF64, VersionIndex(0x0100)); } #[test] fn parse_verndx64_msb() { - test_parse_for(BigEndian, Class::ELF64, VersionIndex { 0: 0x0001 }); + test_parse_for(BigEndian, Class::ELF64, VersionIndex(0x0001)); } #[test] @@ -1251,7 +1249,7 @@ mod parse_tests { let mut data = [0u8; ELFVERDEFSIZE]; data[1] = 1; for n in 0..ELFVERDEFSIZE { - let buf = data.split_at(n).0.as_ref(); + let buf = data.split_at(n).0; let mut offset: usize = 0; let error = VerDef::parse_at(BigEndian, Class::ELF32, &mut offset, buf) .expect_err("Expected an error"); @@ -1293,7 +1291,7 @@ mod parse_tests { let mut data = [0u8; ELFVERDEFSIZE]; data[1] = 1; for n in 0..ELFVERDEFSIZE { - let buf = data.split_at(n).0.as_ref(); + let buf = data.split_at(n).0; let mut offset: usize = 0; let error = VerDef::parse_at(BigEndian, Class::ELF64, &mut offset, buf) .expect_err("Expected an error"); @@ -1422,7 +1420,7 @@ mod parse_tests { let mut data = [0u8; ELFVERNEEDSIZE]; data[1] = 1; for n in 0..ELFVERNEEDSIZE { - let buf = data.split_at(n).0.as_ref(); + let buf = data.split_at(n).0; let mut offset: usize = 0; let error = VerNeed::parse_at(BigEndian, Class::ELF32, &mut offset, buf) .expect_err("Expected an error"); @@ -1462,7 +1460,7 @@ mod parse_tests { let mut data = [0u8; ELFVERNEEDSIZE]; data[1] = 1; for n in 0..ELFVERNEEDSIZE { - let buf = data.split_at(n).0.as_ref(); + let buf = data.split_at(n).0; let mut offset: usize = 0; let error = VerNeed::parse_at(BigEndian, Class::ELF64, &mut offset, buf) .expect_err("Expected an error"); diff --git a/src/hash.rs b/src/hash.rs index 20332e1..9ad66c7 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -163,7 +163,7 @@ impl ParseAt for u64 { offset: &mut usize, data: &[u8], ) -> Result { - Ok(endian.parse_u64_at(offset, data)?) + endian.parse_u64_at(offset, data) } #[inline] diff --git a/src/note.rs b/src/note.rs index 9201dc9..2669034 100644 --- a/src/note.rs +++ b/src/note.rs @@ -113,13 +113,13 @@ impl<'data> Note<'data> { abi::NT_GNU_BUILD_ID => Ok(Note::GnuBuildId(NoteGnuBuildId(raw_desc))), _ => Ok(Note::Unknown(NoteAny { n_type: nhdr.n_type, - name: name, + name, desc: raw_desc, })), }, _ => Ok(Note::Unknown(NoteAny { n_type: nhdr.n_type, - name: name, + name, desc: raw_desc, })), } @@ -201,7 +201,7 @@ impl<'data, E: EndianParse> NoteIterator<'data, E> { impl<'data, E: EndianParse> Iterator for NoteIterator<'data, E> { type Item = Note<'data>; fn next(&mut self) -> Option { - if self.data.len() == 0 { + if self.data.is_empty() { return None; } @@ -210,7 +210,7 @@ impl<'data, E: EndianParse> Iterator for NoteIterator<'data, E> { self.class, self.align, &mut self.offset, - &self.data, + self.data, ) .ok() } diff --git a/src/parse.rs b/src/parse.rs index 57af437..e2284da 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -232,7 +232,7 @@ impl<'data, E: EndianParse, P: ParseAt> ParsingIterator<'data, E, P> { impl<'data, E: EndianParse, P: ParseAt> Iterator for ParsingIterator<'data, E, P> { type Item = P; fn next(&mut self) -> Option { - if self.data.len() == 0 { + if self.data.is_empty() { return None; } @@ -269,7 +269,7 @@ impl<'data, E: EndianParse, P: ParseAt> ParsingTable<'data, E, P> { } pub fn get(&self, index: usize) -> Result { - if self.data.len() == 0 { + if self.data.is_empty() { return Err(ParseError::BadOffset(index as u64)); } @@ -281,7 +281,7 @@ impl<'data, E: EndianParse, P: ParseAt> ParsingTable<'data, E, P> { return Err(ParseError::BadOffset(index as u64)); } - Ok(P::parse_at(self.endian, self.class, &mut start, self.data)?) + P::parse_at(self.endian, self.class, &mut start, self.data) } } @@ -301,7 +301,7 @@ impl ParseAt for u32 { offset: &mut usize, data: &[u8], ) -> Result { - Ok(endian.parse_u32_at(offset, data)?) + endian.parse_u32_at(offset, data) } #[inline] @@ -353,7 +353,7 @@ pub fn test_parse_fuzz_too_short( let size = P::size_for(class); let data = vec![0u8; size]; for n in 0..size { - let buf = data.split_at(n).0.as_ref(); + let buf = data.split_at(n).0; let mut offset: usize = 0; let error = P::parse_at(endian, class, &mut offset, buf).expect_err("Expected an error"); assert!( diff --git a/src/segment.rs b/src/segment.rs index f8cdf01..f35b1d6 100644 --- a/src/segment.rs +++ b/src/segment.rs @@ -65,7 +65,7 @@ impl ParseAt for ProgramHeader { p_paddr, p_filesz, p_memsz, - p_flags: p_flags, + p_flags, p_align, }) } diff --git a/src/string_table.rs b/src/string_table.rs index fca9ea0..0311cb0 100644 --- a/src/string_table.rs +++ b/src/string_table.rs @@ -2,7 +2,7 @@ use crate::parse::ParseError; use core::str::from_utf8; -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Default, Clone, Copy)] pub struct StringTable<'data> { data: &'data [u8], } @@ -13,7 +13,7 @@ impl<'data> StringTable<'data> { } pub fn get_raw(&self, offset: usize) -> Result<&'data [u8], ParseError> { - if self.data.len() == 0 { + if self.data.is_empty() { return Err(ParseError::BadOffset(offset as u64)); }; @@ -35,12 +35,6 @@ impl<'data> StringTable<'data> { } } -impl<'data> Default for StringTable<'data> { - fn default() -> Self { - StringTable { data: &[] } - } -} - #[cfg(test)] mod tests { use super::*;