diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 00000000..f97e544b --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +msrv = "1.42.0" diff --git a/src/lib.rs b/src/lib.rs index ed1af9cb..db30375a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,14 +26,17 @@ #![warn(ellipsis_inclusive_range_patterns)] //#![warn(elided_lifetimes_in_paths)] #![warn(explicit_outlives_requirements)] -// Allow clippy warnings when we aren't building with clippy. -#![allow(unknown_lints)] +// Style. +#![allow(clippy::bool_to_int_with_if)] +#![allow(clippy::collapsible_else_if)] +#![allow(clippy::comparison_chain)] +#![allow(clippy::manual_range_contains)] +#![allow(clippy::needless_late_init)] +#![allow(clippy::too_many_arguments)] // False positives with `fallible_iterator`. #![allow(clippy::should_implement_trait)] -// Many false positives involving `continue`. -#![allow(clippy::never_loop)] -// False positives when block expressions are used inside an assertion. -#![allow(clippy::panic_params)] +// False positives. +#![allow(clippy::derive_partial_eq_without_eq)] #![no_std] #[allow(unused_imports)] diff --git a/src/read/abbrev.rs b/src/read/abbrev.rs index 0763ae53..54f5cf8e 100644 --- a/src/read/abbrev.rs +++ b/src/read/abbrev.rs @@ -344,7 +344,7 @@ impl Attributes { /// Pushes a new value onto this list of attributes. fn push(&mut self, attr: AttributeSpecification) { match self { - Attributes::Heap(list) => return list.push(attr), + Attributes::Heap(list) => list.push(attr), Attributes::Inline { buf, len: MAX_ATTRIBUTES_INLINE, @@ -363,13 +363,13 @@ impl Attributes { impl Debug for Attributes { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - (&**self).fmt(f) + (**self).fmt(f) } } impl PartialEq for Attributes { fn eq(&self, other: &Attributes) -> bool { - &**self == &**other + **self == **other } } @@ -394,7 +394,7 @@ impl FromIterator for Attributes { for item in iter { list.push(item); } - return list; + list } } @@ -504,8 +504,7 @@ pub(crate) fn get_attribute_size(form: constants::DwForm, encoding: Encoding) -> match form { constants::DW_FORM_addr => Some(encoding.address_size), - constants::DW_FORM_implicit_const | - constants::DW_FORM_flag_present => Some(0), + constants::DW_FORM_implicit_const | constants::DW_FORM_flag_present => Some(0), constants::DW_FORM_data1 | constants::DW_FORM_flag @@ -531,7 +530,7 @@ pub(crate) fn get_attribute_size(form: constants::DwForm, encoding: Encoding) -> | constants::DW_FORM_ref_sig8 | constants::DW_FORM_ref_sup8 => Some(8), - constants::DW_FORM_data16 => Some(16), + constants::DW_FORM_data16 => Some(16), constants::DW_FORM_sec_offset | constants::DW_FORM_GNU_ref_alt @@ -552,16 +551,16 @@ pub(crate) fn get_attribute_size(form: constants::DwForm, encoding: Encoding) -> } // Variably sized forms. - constants::DW_FORM_block | - constants::DW_FORM_block1 | - constants::DW_FORM_block2 | - constants::DW_FORM_block4 | - constants::DW_FORM_exprloc | - constants::DW_FORM_ref_udata | - constants::DW_FORM_string | - constants::DW_FORM_sdata | - constants::DW_FORM_udata | - constants::DW_FORM_indirect | + constants::DW_FORM_block + | constants::DW_FORM_block1 + | constants::DW_FORM_block2 + | constants::DW_FORM_block4 + | constants::DW_FORM_exprloc + | constants::DW_FORM_ref_udata + | constants::DW_FORM_string + | constants::DW_FORM_sdata + | constants::DW_FORM_udata + | constants::DW_FORM_indirect => None, // We don't know the size of unknown forms. _ => None, diff --git a/src/read/cfi.rs b/src/read/cfi.rs index 2e516734..5e9befac 100644 --- a/src/read/cfi.rs +++ b/src/read/cfi.rs @@ -1058,7 +1058,6 @@ where Fde(PartialFrameDescriptionEntry<'bases, Section, R>), } -#[allow(clippy::type_complexity)] fn parse_cfi_entry<'bases, Section, R>( bases: &'bases BaseAddresses, section: &Section, @@ -1617,7 +1616,6 @@ where } impl FrameDescriptionEntry { - #[allow(clippy::too_many_arguments)] fn parse_rest( offset: R::Offset, length: R::Offset, @@ -1982,7 +1980,7 @@ impl> UnwindContext { } let mut table = UnwindTable::new_for_cie(section, bases, self, cie); - while let Some(_) = table.next_row()? {} + while table.next_row()?.is_some() {} self.save_initial_rules()?; Ok(()) @@ -2005,7 +2003,7 @@ impl> UnwindContext { } fn save_initial_rules(&mut self) -> Result<()> { - assert_eq!(self.is_initialized, false); + debug_assert!(!self.is_initialized); self.initial_rule = match *self.stack.last().unwrap().registers.rules { // All rules are default (undefined). In this case just synthesize // an undefined rule. @@ -2821,10 +2819,7 @@ pub enum RegisterRule { impl RegisterRule { fn is_defined(&self) -> bool { - match *self { - RegisterRule::Undefined => false, - _ => true, - } + !matches!(*self, RegisterRule::Undefined) } } @@ -3394,10 +3389,10 @@ impl Default for Pointer { } } -impl Into for Pointer { +impl From for u64 { #[inline] - fn into(self) -> u64 { - match self { + fn from(p: Pointer) -> u64 { + match p { Pointer::Direct(p) | Pointer::Indirect(p) => p, } } @@ -3762,8 +3757,6 @@ mod tests { } } - #[allow(clippy::type_complexity)] - #[allow(clippy::needless_pass_by_value)] fn assert_parse_cie<'input, E>( kind: SectionKind>>, section: Section, @@ -5118,7 +5111,6 @@ mod tests { assert_eq!(iter.next(), Ok(None)); } - #[allow(clippy::needless_pass_by_value)] fn assert_eval<'a, I>( mut initial_ctx: UnwindContext>, expected_ctx: UnwindContext>, @@ -5598,7 +5590,6 @@ mod tests { #[test] fn test_unwind_table_cie_no_rule() { - #[allow(clippy::identity_op)] let initial_instructions = Section::with_endian(Endian::Little) // The CFA is -12 from register 4. .D8(constants::DW_CFA_def_cfa_sf.0) @@ -5671,7 +5662,6 @@ mod tests { #[test] fn test_unwind_table_cie_single_rule() { - #[allow(clippy::identity_op)] let initial_instructions = Section::with_endian(Endian::Little) // The CFA is -12 from register 4. .D8(constants::DW_CFA_def_cfa_sf.0) @@ -5747,7 +5737,6 @@ mod tests { #[test] fn test_unwind_table_next_row() { - #[allow(clippy::identity_op)] let initial_instructions = Section::with_endian(Endian::Little) // The CFA is -12 from register 4. .D8(constants::DW_CFA_def_cfa_sf.0) diff --git a/src/read/endian_slice.rs b/src/read/endian_slice.rs index 05262cde..d0fd67c0 100644 --- a/src/read/endian_slice.rs +++ b/src/read/endian_slice.rs @@ -197,12 +197,12 @@ where } } -impl<'input, Endian> Into<&'input [u8]> for EndianSlice<'input, Endian> +impl<'input, Endian> From> for &'input [u8] where Endian: Endianity, { - fn into(self) -> &'input [u8] { - self.slice + fn from(endian_slice: EndianSlice<'input, Endian>) -> &'input [u8] { + endian_slice.slice } } diff --git a/src/read/line.rs b/src/read/line.rs index 0e7380bb..f7f44b2b 100644 --- a/src/read/line.rs +++ b/src/read/line.rs @@ -198,7 +198,6 @@ where R: Reader, Offset: ReaderOffset, { - #[allow(clippy::new_ret_no_self)] fn new(program: IncompleteLineProgram) -> OneShotLineRows { let row = LineRow::new(program.header()); let instructions = LineInstructions { @@ -606,7 +605,6 @@ impl LineInstructions { /// /// Unfortunately, the `header` parameter means that this cannot be a /// `FallibleIterator`. - #[allow(clippy::inline_always)] #[inline(always)] pub fn next_instruction( &mut self, diff --git a/src/read/loclists.rs b/src/read/loclists.rs index 57f0e487..5cba675d 100644 --- a/src/read/loclists.rs +++ b/src/read/loclists.rs @@ -233,7 +233,7 @@ impl LocationLists { let (mut input, format) = if unit_encoding.version <= 4 { (self.debug_loc.section.clone(), LocListsFormat::Bare) } else { - (self.debug_loclists.section.clone(), LocListsFormat::LLE) + (self.debug_loclists.section.clone(), LocListsFormat::Lle) }; input.skip(offset.0)?; Ok(RawLocListIter::new(input, unit_encoding, format)) @@ -259,7 +259,7 @@ impl LocationLists { Ok(RawLocListIter::new( input, unit_encoding, - LocListsFormat::LLE, + LocListsFormat::Lle, )) } @@ -300,7 +300,7 @@ enum LocListsFormat { Bare, /// The DW_LLE encoded range list format used in DWARF 5 and the non-standard GNU /// split dwarf extension. - LLE, + Lle, } /// A raw iterator over a location list. @@ -402,10 +402,10 @@ fn parse_data(input: &mut R, encoding: Encoding) -> Result RawLocListEntry { /// Parse a location list entry from `.debug_loclists` fn parse(input: &mut R, encoding: Encoding, format: LocListsFormat) -> Result> { - match format { + Ok(match format { LocListsFormat::Bare => { let range = RawRange::parse(input, encoding.address_size)?; - return Ok(if range.is_end() { + if range.is_end() { None } else if range.is_base_address(encoding.address_size) { Some(RawLocListEntry::BaseAddress { addr: range.end }) @@ -417,9 +417,9 @@ impl RawLocListEntry { end: range.end, data, }) - }); + } } - LocListsFormat::LLE => Ok(match constants::DwLle(input.read_u8()?) { + LocListsFormat::Lle => match constants::DwLle(input.read_u8()?) { constants::DW_LLE_end_of_list => None, constants::DW_LLE_base_addressx => Some(RawLocListEntry::BaseAddressx { addr: DebugAddrIndex(input.read_uleb128().and_then(R::Offset::from_u64)?), @@ -463,8 +463,8 @@ impl RawLocListEntry { _ => { return Err(Error::InvalidAddressRange); } - }), - } + }, + }) } } diff --git a/src/read/op.rs b/src/read/op.rs index 88ea2029..670d1ad2 100644 --- a/src/read/op.rs +++ b/src/read/op.rs @@ -346,10 +346,7 @@ where { /// Return true if the piece is empty. pub fn is_empty(&self) -> bool { - match *self { - Location::Empty => true, - _ => false, - } + matches!(*self, Location::Empty) } } @@ -1225,7 +1222,6 @@ impl> Evaluation { self.stack.try_push(value).map_err(|_| Error::StackFull) } - #[allow(clippy::cyclomatic_complexity)] fn evaluate_one_operation(&mut self) -> Result> { let operation = Operation::parse(&mut self.pc, self.encoding)?; @@ -2889,7 +2885,6 @@ mod tests { result } - #[allow(clippy::too_many_arguments)] fn check_eval_with_args( program: &[AssemblerEntry], expect: Result<&[Piece>]>, diff --git a/src/read/rnglists.rs b/src/read/rnglists.rs index 98840be7..12e3e04e 100644 --- a/src/read/rnglists.rs +++ b/src/read/rnglists.rs @@ -232,7 +232,7 @@ impl RangeLists { let (mut input, format) = if unit_encoding.version <= 4 { (self.debug_ranges.section.clone(), RangeListsFormat::Bare) } else { - (self.debug_rnglists.section.clone(), RangeListsFormat::RLE) + (self.debug_rnglists.section.clone(), RangeListsFormat::Rle) }; input.skip(offset.0)?; Ok(RawRngListIter::new(input, unit_encoding, format)) @@ -277,7 +277,7 @@ enum RangeListsFormat { /// The bare range list format used before DWARF 5. Bare, /// The DW_RLE encoded range list format used in DWARF 5. - RLE, + Rle, } /// A raw iterator over an address range list. @@ -355,10 +355,10 @@ impl RawRngListEntry { encoding: Encoding, format: RangeListsFormat, ) -> Result> { - match format { + Ok(match format { RangeListsFormat::Bare => { let range = RawRange::parse(input, encoding.address_size)?; - return Ok(if range.is_end() { + if range.is_end() { None } else if range.is_base_address(encoding.address_size) { Some(RawRngListEntry::BaseAddress { addr: range.end }) @@ -367,9 +367,9 @@ impl RawRngListEntry { begin: range.begin, end: range.end, }) - }); + } } - RangeListsFormat::RLE => Ok(match constants::DwRle(input.read_u8()?) { + RangeListsFormat::Rle => match constants::DwRle(input.read_u8()?) { constants::DW_RLE_end_of_list => None, constants::DW_RLE_base_addressx => Some(RawRngListEntry::BaseAddressx { addr: DebugAddrIndex(input.read_uleb128().and_then(R::Offset::from_u64)?), @@ -400,8 +400,8 @@ impl RawRngListEntry { _ => { return Err(Error::InvalidAddressRange); } - }), - } + }, + }) } } diff --git a/src/read/unit.rs b/src/read/unit.rs index 670e55ef..67243533 100644 --- a/src/read/unit.rs +++ b/src/read/unit.rs @@ -883,7 +883,6 @@ where } /// Return the input buffer after the last attribute. - #[allow(clippy::inline_always)] #[inline(always)] fn after_attrs(&self) -> Result { if let Some(attrs_len) = self.attrs_len.get() { @@ -892,7 +891,7 @@ where Ok(input) } else { let mut attrs = self.attrs(); - while let Some(_) = attrs.next()? {} + while attrs.next()?.is_some() {} Ok(attrs.input) } } @@ -912,7 +911,6 @@ where } /// Parse an entry. Returns `Ok(None)` for null entries. - #[allow(clippy::inline_always)] #[inline(always)] fn parse( input: &mut R, @@ -1143,8 +1141,6 @@ impl Attribute { /// name. /// /// See "Table 7.5: Attribute encodings" and "Table 7.6: Attribute form encodings". - #[allow(clippy::cyclomatic_complexity)] - #[allow(clippy::match_same_arms)] pub fn value(&self) -> AttributeValue { // Table 7.5 shows the possible attribute classes for each name. // Table 7.6 shows the possible attribute classes for each form. @@ -1980,7 +1976,7 @@ fn allow_section_offset(name: constants::DwAt, version: u16) -> bool { } } -pub(crate) fn parse_attribute<'unit, R: Reader>( +pub(crate) fn parse_attribute( input: &mut R, encoding: Encoding, spec: AttributeSpecification, @@ -2205,7 +2201,7 @@ pub(crate) fn parse_attribute<'unit, R: Reader>( } } -pub(crate) fn skip_attributes<'unit, R: Reader>( +pub(crate) fn skip_attributes( input: &mut R, encoding: Encoding, specs: &[AttributeSpecification], @@ -2294,7 +2290,6 @@ impl<'abbrev, 'entry, 'unit, R: Reader> AttrsIter<'abbrev, 'entry, 'unit, R> { /// Returns `None` when iteration is finished. If an error /// occurs while parsing the next attribute, then this error /// is returned, and all subsequent calls return `None`. - #[allow(clippy::inline_always)] #[inline(always)] pub fn next(&mut self) -> Result>> { if self.attributes.is_empty() { @@ -2647,7 +2642,6 @@ impl<'abbrev, 'unit, R: Reader> EntriesCursor<'abbrev, 'unit, R> { /// println!("The first entry with no children is {:?}", /// first_entry_with_no_children.unwrap()); /// ``` - #[allow(clippy::type_complexity)] pub fn next_dfs( &mut self, ) -> Result)>> { @@ -4213,7 +4207,6 @@ mod tests { #[test] fn test_attribute_udata_sdata_value() { - #[allow(clippy::type_complexity)] let tests: &[( AttributeValue>, Option, diff --git a/src/read/util.rs b/src/read/util.rs index 16eafdde..747418ba 100644 --- a/src/read/util.rs +++ b/src/read/util.rs @@ -9,7 +9,8 @@ use core::ptr; use core::slice; mod sealed { - // SAFETY: Implementer must not modify the content in storage. + /// # Safety + /// Implementer must not modify the content in storage. pub unsafe trait Sealed { type Storage; @@ -161,7 +162,7 @@ impl ArrayVec { } else { self.len -= 1; // SAFETY: this element is valid and we "forget" it by setting the length. - Some(unsafe { A::as_slice(&mut self.storage)[self.len].as_ptr().read() }) + Some(unsafe { A::as_slice(&self.storage)[self.len].as_ptr().read() }) } } diff --git a/src/write/line.rs b/src/write/line.rs index 310170d9..c88b735b 100644 --- a/src/write/line.rs +++ b/src/write/line.rs @@ -94,8 +94,6 @@ impl LineProgram { /// Panics if `comp_dir` is empty or contains a null byte. /// /// Panics if `comp_file` is empty or contains a null byte. - #[allow(clippy::too_many_arguments)] - #[allow(clippy::new_ret_no_self)] pub fn new( encoding: Encoding, line_encoding: LineEncoding, @@ -261,7 +259,7 @@ impl LineProgram { } else { let entry = self.files.entry(key); let index = entry.index(); - entry.or_insert(FileInfo::default()); + entry.or_default(); index }; FileId::new(index) @@ -1723,7 +1721,6 @@ mod tests { // Test that the address/line advance is correct. We don't test for optimality. #[test] - #[allow(clippy::useless_vec)] fn test_advance() { let encoding = Encoding { format: Format::Dwarf32, diff --git a/src/write/op.rs b/src/write/op.rs index 1880afaf..287083b3 100644 --- a/src/write/op.rs +++ b/src/write/op.rs @@ -279,12 +279,8 @@ impl Expression { } offsets.push(offset); for (operation, offset) in self.operations.iter().zip(offsets.iter().copied()) { - let refs = match refs { - Some(ref mut refs) => Some(&mut **refs), - None => None, - }; debug_assert_eq!(w.len(), offset); - operation.write(w, refs, encoding, unit_offsets, &offsets)?; + operation.write(w, refs.as_deref_mut(), encoding, unit_offsets, &offsets)?; } Ok(()) } @@ -630,7 +626,7 @@ impl Operation { } w.write_uleb128(entry_offset(base)?)?; w.write_udata(value.len() as u64, 1)?; - w.write(&value)?; + w.write(value)?; } Operation::FrameOffset(offset) => { w.write_u8(constants::DW_OP_fbreg.0)?; @@ -770,7 +766,7 @@ impl Operation { Operation::ImplicitValue(ref data) => { w.write_u8(constants::DW_OP_implicit_value.0)?; w.write_uleb128(data.len() as u64)?; - w.write(&data)?; + w.write(data)?; } Operation::ImplicitPointer { entry, byte_offset } => { if encoding.version >= 5 { @@ -872,7 +868,7 @@ pub(crate) mod convert { let mut offsets = Vec::new(); let mut offset = 0; let mut from_operations = from_expression.clone().operations(encoding); - while let Some(_) = from_operations.next()? { + while from_operations.next()?.is_some() { offsets.push(offset); offset = from_operations.offset_from(&from_expression); } diff --git a/src/write/section.rs b/src/write/section.rs index e8f3378c..db5eb9a2 100644 --- a/src/write/section.rs +++ b/src/write/section.rs @@ -111,7 +111,7 @@ impl Sections { debug_loclists: DebugLocLists(section.clone()), debug_str: DebugStr(section.clone()), debug_frame: DebugFrame(section.clone()), - eh_frame: EhFrame(section.clone()), + eh_frame: EhFrame(section), debug_info_refs: Vec::new(), debug_loc_refs: Vec::new(), debug_loclists_refs: Vec::new(), diff --git a/src/write/unit.rs b/src/write/unit.rs index 2f1fad1b..23027bc2 100644 --- a/src/write/unit.rs +++ b/src/write/unit.rs @@ -365,7 +365,6 @@ impl Unit { &mut unit_refs, self, &mut offsets, - abbrevs, line_program, line_strings, strings, @@ -605,7 +604,6 @@ impl DebuggingInformationEntry { } /// Write the entry to the given sections. - #[allow(clippy::too_many_arguments)] fn write( &self, w: &mut DebugInfo, @@ -613,7 +611,6 @@ impl DebuggingInformationEntry { unit_refs: &mut Vec<(DebugInfoOffset, UnitEntryId)>, unit: &Unit, offsets: &mut UnitOffsets, - abbrevs: &mut AbbreviationTable, line_program: Option, line_strings: &DebugLineStrOffsets, strings: &DebugStrOffsets, @@ -654,7 +651,6 @@ impl DebuggingInformationEntry { unit_refs, unit, offsets, - abbrevs, line_program, line_strings, strings, @@ -1128,7 +1124,6 @@ impl AttributeValue { } /// Write the attribute value to the given sections. - #[allow(clippy::cyclomatic_complexity, clippy::too_many_arguments)] fn write( &self, w: &mut DebugInfo, @@ -1155,7 +1150,7 @@ impl AttributeValue { AttributeValue::Block(ref val) => { debug_assert_form!(constants::DW_FORM_block); w.write_uleb128(val.len() as u64)?; - w.write(&val)?; + w.write(val)?; } AttributeValue::Data1(val) => { debug_assert_form!(constants::DW_FORM_data1); @@ -1308,7 +1303,7 @@ impl AttributeValue { } AttributeValue::String(ref val) => { debug_assert_form!(constants::DW_FORM_string); - w.write(&val)?; + w.write(val)?; w.write_u8(0)?; } AttributeValue::Encoding(val) => { @@ -1558,7 +1553,6 @@ pub(crate) mod convert { /// Create a unit by reading the data in the input sections. /// /// Does not add entry attributes. - #[allow(clippy::too_many_arguments)] pub(crate) fn convert_entries>( from_header: read::UnitHeader, unit_id: UnitId, @@ -1934,7 +1928,6 @@ mod tests { use std::sync::Arc; #[test] - #[allow(clippy::cyclomatic_complexity)] fn test_unit_table() { let mut strings = StringTable::default(); @@ -2579,7 +2572,6 @@ mod tests { } #[test] - #[allow(clippy::cyclomatic_complexity)] fn test_unit_ref() { let mut units = UnitTable::default(); let unit_id1 = units.add(Unit::new( diff --git a/src/write/writer.rs b/src/write/writer.rs index 0785d168..1ce3641f 100644 --- a/src/write/writer.rs +++ b/src/write/writer.rs @@ -93,9 +93,7 @@ pub trait Writer { constants::DW_EH_PE_sdata2 => self.write_sdata(val as i64, 2), constants::DW_EH_PE_sdata4 => self.write_sdata(val as i64, 4), constants::DW_EH_PE_sdata8 => self.write_sdata(val as i64, 8), - _ => { - return Err(Error::UnsupportedPointerEncoding(format)); - } + _ => Err(Error::UnsupportedPointerEncoding(format)), } } @@ -334,7 +332,6 @@ mod tests { use std::{i64, u64}; #[test] - #[allow(clippy::cyclomatic_complexity)] fn test_writer() { let mut w = write::EndianVec::new(LittleEndian); w.write_address(Address::Constant(0x1122_3344), 4).unwrap();