Skip to content

Commit

Permalink
cleanup of many warnings issued by clippy in pedantic mode
Browse files Browse the repository at this point in the history
Running clippy in pedantic mode resulted in lots of warnings, many of which made sense.
  • Loading branch information
DanielT committed Nov 22, 2023
1 parent b971785 commit 20d1c60
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 210 deletions.
14 changes: 7 additions & 7 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ pub(crate) fn get_type_limits(
}
}
TypeInfo::Double => (f64::MIN, f64::MAX),
TypeInfo::Float => (f32::MIN as f64, f32::MAX as f64),
TypeInfo::Uint8 => (u8::MIN as f64, u8::MAX as f64),
TypeInfo::Uint16 => (u16::MIN as f64, u16::MAX as f64),
TypeInfo::Uint32 => (u32::MIN as f64, u32::MAX as f64),
TypeInfo::Float => (f64::from(f32::MIN), f64::from(f32::MAX)),
TypeInfo::Uint8 => (f64::from(u8::MIN), f64::from(u8::MAX)),
TypeInfo::Uint16 => (f64::from(u16::MIN), f64::from(u16::MAX)),
TypeInfo::Uint32 => (f64::from(u32::MIN), f64::from(u32::MAX)),
TypeInfo::Uint64 => (u64::MIN as f64, u64::MAX as f64),
TypeInfo::Sint8 => (i8::MIN as f64, i8::MAX as f64),
TypeInfo::Sint16 => (i16::MIN as f64, i16::MAX as f64),
TypeInfo::Sint32 => (i32::MIN as f64, i32::MAX as f64),
TypeInfo::Sint8 => (f64::from(i8::MIN), f64::from(i8::MAX)),
TypeInfo::Sint16 => (f64::from(i16::MIN), f64::from(i16::MAX)),
TypeInfo::Sint32 => (f64::from(i32::MIN), f64::from(i32::MAX)),
TypeInfo::Sint64 => (i64::MIN as f64, i64::MAX as f64),
TypeInfo::Enum { enumerators, .. } => {
let lower = enumerators.iter().map(|val| val.1).min().unwrap_or(0) as f64;
Expand Down
12 changes: 6 additions & 6 deletions src/dwarf/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) fn get_name_attribute(
// could not demangle, but successfully converted the slice to utf8
return Ok(utf8string.to_owned());
}
Err(format!("could not decode {:#?} as a utf-8 string", slice))
Err(format!("could not decode {slice:#?} as a utf-8 string"))
}
gimli::AttributeValue::DebugStrRef(str_offset) => {
match dwarf.debug_str.get_str(str_offset) {
Expand All @@ -34,12 +34,12 @@ pub(crate) fn get_name_attribute(
// could not demangle, but successfully converted the slice to utf8
return Ok(utf8string.to_owned());
}
Err(format!("could not decode {:#?} as a utf-8 string", slice))
Err(format!("could not decode {slice:#?} as a utf-8 string"))
}
Err(err) => Err(err.to_string()),
}
}
_ => Err(format!("invalid name attribute type {:#?}", name_attr)),
_ => Err(format!("invalid name attribute type {name_attr:#?}")),
}
}

Expand All @@ -63,7 +63,7 @@ pub(crate) fn get_typeref_attribute(
// (for example gcc supports this, but support is only enabled if the user requests this explicitly)
Err("unsupported referene to a .debug_types entry (Dwarf 4)".to_string())
}
_ => Err(format!("unsupported type reference: {:#?}", type_attr)),
_ => Err(format!("unsupported type reference: {type_attr:#?}")),
}
}

Expand Down Expand Up @@ -91,8 +91,8 @@ pub(crate) fn get_data_member_location_attribute(
match loc_attr {
gimli::AttributeValue::Exprloc(expression) => evaluate_exprloc(expression, encoding),
gimli::AttributeValue::Udata(val) => Some(val),
_other => {
println!("unexpected data_member_location attribute: {:?}", _other);
other => {
println!("unexpected data_member_location attribute: {other:?}");
None
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/dwarf/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::DebugData;
use super::TypeInfo;
use super::VarInfo;
use std::fmt::Write;

#[cfg(test)]
use std::collections::HashMap;
Expand Down Expand Up @@ -31,7 +32,7 @@ pub(crate) struct VariablesIterator<'a> {

impl<'a> TypeInfoIter<'a> {
pub(crate) fn new(typeinfo: &'a TypeInfo) -> Self {
use TypeInfo::*;
use TypeInfo::{Array, Class, Struct, Union};
match typeinfo {
Class { members, .. } | Union { members, .. } | Struct { members, .. } => {
let mut struct_iter = members.iter();
Expand Down Expand Up @@ -80,12 +81,12 @@ impl<'a> Iterator for TypeInfoIter<'a> {
if let Some((name, (typeinfo, offset))) = current_member {
if member_iter.is_none() {
*member_iter = Some(Box::new(TypeInfoIter::new(typeinfo)));
Some((format!(".{}", name), typeinfo, *offset))
Some((format!(".{name}"), typeinfo, *offset))
} else {
let member = member_iter.as_deref_mut().unwrap().next();
if let Some((member_name, member_typeinfo, member_offset)) = member {
Some((
format!(".{}{}", name, member_name),
format!(".{name}{member_name}"),
member_typeinfo,
offset + member_offset,
))
Expand Down Expand Up @@ -124,9 +125,10 @@ impl<'a> Iterator for TypeInfoIter<'a> {
}
let idxstr = current_indices
.iter()
.map(|val| format!("._{}_", val))
.collect::<Vec<String>>()
.join("");
.fold(String::new(), |mut output, val| {
let _ = write!(output, "._{val}_");
output
});

// calculate the storage offset of this array element. Each element is stride bytes wide.
let offset = *stride * (*position);
Expand All @@ -142,7 +144,7 @@ impl<'a> Iterator for TypeInfoIter<'a> {
let item = item_iter.as_deref_mut().unwrap().next();
if let Some((item_name, item_typeinfo, item_offset)) = item {
Some((
format!("{}{}", idxstr, item_name),
format!("{idxstr}{item_name}"),
item_typeinfo,
offset + item_offset,
))
Expand Down Expand Up @@ -202,7 +204,7 @@ impl<'a> Iterator for VariablesIterator<'a> {
self.type_iter.as_mut().unwrap().next()
{
Some((
format!("{}{}", varname, type_name),
format!("{varname}{type_name}"),
Some(type_info),
*address + offset,
))
Expand Down
14 changes: 7 additions & 7 deletions src/dwarf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use std::{collections::HashMap, fs::File};
type SliceType<'a> = EndianSlice<'a, RunTimeEndian>;

mod attributes;
use attributes::*;
use attributes::{
get_abstract_origin_attribute, get_location_attribute, get_name_attribute,
get_specification_attribute, get_typeref_attribute,
};
mod typereader;
use typereader::load_types;
mod iter;
Expand Down Expand Up @@ -123,10 +126,7 @@ fn load_elf_file<'data>(
) -> Result<object::read::File<'data>, String> {
match object::File::parse(filedata) {
Ok(file) => Ok(file),
Err(err) => Err(format!(
"Error: Failed to parse file '{}': {}",
filename, err
)),
Err(err) => Err(format!("Error: Failed to parse file '{filename}': {err}")),
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@ fn get_endian(elffile: &object::read::File) -> RunTimeEndian {
// read the debug information entries in the DWAF data to get all the global variables and their types
fn read_debug_info_entries(dwarf: &gimli::Dwarf<SliceType>, verbose: bool) -> DebugData {
let (variables, typedefs, units) = load_variables(dwarf, verbose);
let types = load_types(&variables, typedefs, units, dwarf, verbose);
let types = load_types(&variables, &typedefs, &units, dwarf, verbose);
let varname_list: Vec<&String> = variables.keys().collect();
let demangled_names = demangle_cpp_varnames(&varname_list);

Expand Down Expand Up @@ -216,7 +216,7 @@ fn load_variables<'a>(
.to_debug_info_offset(&unit)
.unwrap_or(gimli::DebugInfoOffset(0))
.0;
println!("Error loading variable @{:x}: {}", offset, errmsg);
println!("Error loading variable @{offset:x}: {errmsg}");
}
}
}
Expand Down
31 changes: 14 additions & 17 deletions src/dwarf/typereader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::collections::HashMap;
// load all the types referenced by variables in given HashMap
pub(crate) fn load_types(
variables: &HashMap<String, VarInfo>,
typedefs: HashMap<usize, String>,
units: UnitList,
typedefs: &HashMap<usize, String>,
units: &UnitList,
dwarf: &gimli::Dwarf<EndianSlice<RunTimeEndian>>,
verbose: bool,
) -> HashMap<usize, TypeInfo> {
let mut types = HashMap::<usize, TypeInfo>::new();
// for each variable
for (_name, VarInfo { typeref, .. }) in variables {
for (name, VarInfo { typeref, .. }) in variables {
// check if the type was already loaded
if types.get(typeref).is_none() {
if let Some(unit_idx) = units.get_unit(*typeref) {
Expand All @@ -25,19 +25,19 @@ pub(crate) fn load_types(

// load one type and add it to the collection (always succeeds for correctly structured DWARF debug info)
match get_type(
&units,
units,
unit_idx,
entries_tree.root().unwrap(),
None,
&typedefs,
typedefs,
dwarf,
) {
Ok(vartype) => {
types.insert(*typeref, vartype);
}
Err(errmsg) => {
if verbose {
println!("Error loading type info for variable {}: {}", _name, errmsg);
println!("Error loading type info for variable {name}: {errmsg}");
}
}
}
Expand All @@ -62,7 +62,7 @@ fn get_type(
gimli::constants::DW_TAG_base_type => get_base_type(entry, &unit_list[current_unit].0),
gimli::constants::DW_TAG_pointer_type => {
let (unit, _) = &unit_list[current_unit];
Ok(TypeInfo::Pointer(unit.encoding().address_size as u64))
Ok(TypeInfo::Pointer(u64::from(unit.encoding().address_size)))
}
gimli::constants::DW_TAG_array_type => {
let size = get_byte_size_attribute(entry)
Expand Down Expand Up @@ -127,7 +127,7 @@ fn get_type(
} else {
// a truly anonymous enum. This can happen if someone writes C code that looks like this:
// enum { ... } varname;
format!("anonymous_enum_{}", dioffset)
format!("anonymous_enum_{dioffset}")
};

let mut iter = entries_tree_node.children();
Expand Down Expand Up @@ -246,8 +246,7 @@ fn get_type(
)
}
other_tag => Err(format!(
"unexpected DWARF tag {} in type definition",
other_tag
"unexpected DWARF tag {other_tag} in type definition"
)),
}
}
Expand All @@ -259,7 +258,9 @@ fn get_base_type(
let byte_size = get_byte_size_attribute(entry).unwrap_or(1u64);
let encoding = get_encoding_attribute(entry).unwrap_or(gimli::constants::DW_ATE_unsigned);
Ok(match encoding {
gimli::constants::DW_ATE_address => TypeInfo::Pointer(unit.encoding().address_size as u64),
gimli::constants::DW_ATE_address => {
TypeInfo::Pointer(u64::from(unit.encoding().address_size))
}
gimli::constants::DW_ATE_float => {
if byte_size == 8 {
TypeInfo::Double
Expand All @@ -273,10 +274,7 @@ fn get_base_type(
4 => TypeInfo::Sint32,
8 => TypeInfo::Sint64,
val => {
return Err(format!(
"error loading data type: signed int of size {}",
val
));
return Err(format!("error loading data type: signed int of size {val}"));
}
},
gimli::constants::DW_ATE_boolean
Expand All @@ -288,8 +286,7 @@ fn get_base_type(
8 => TypeInfo::Uint64,
val => {
return Err(format!(
"error loading data type: unsigned int of size {}",
val
"error loading data type: unsigned int of size {val}"
));
}
},
Expand Down
Loading

0 comments on commit 20d1c60

Please sign in to comment.