diff --git a/build.rs b/build.rs index 2a3da849..370eda27 100644 --- a/build.rs +++ b/build.rs @@ -5,9 +5,8 @@ use std::path::Path; // Must be public so the build script of `std` can call it. pub fn main() { - match env::var("CARGO_CFG_TARGET_OS").unwrap_or_default().as_str() { - "android" => build_android(), - _ => {} + if env::var("CARGO_CFG_TARGET_OS").unwrap_or_default().as_str() == "android" { + build_android() } } diff --git a/src/capture.rs b/src/capture.rs index f2154995..466ab613 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -351,9 +351,10 @@ impl From for BacktraceFrame { } } -// we don't want implementing `impl From for Vec` on purpose, +// we don't want to implement `impl From for Vec` on purpose, // because "... additional directions for Vec can weaken type inference ..." // more information on https://github.com/rust-lang/backtrace-rs/pull/526 +#[allow(clippy::from_over_into)] impl Into> for Backtrace { fn into(self) -> Vec { self.frames @@ -452,7 +453,7 @@ impl BacktraceSymbol { /// This function requires the `std` feature of the `backtrace` crate to be /// enabled, and the `std` feature is enabled by default. pub fn filename(&self) -> Option<&Path> { - self.filename.as_ref().map(|p| &**p) + self.filename.as_deref() } /// Same as `Symbol::lineno` diff --git a/src/lib.rs b/src/lib.rs index 0b9bb56a..ab5e64c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,26 +18,24 @@ //! Next: //! //! ``` -//! fn main() { //! # // Unsafe here so test passes on no_std. //! # #[cfg(feature = "std")] { -//! backtrace::trace(|frame| { -//! let ip = frame.ip(); -//! let symbol_address = frame.symbol_address(); -//! -//! // Resolve this instruction pointer to a symbol name -//! backtrace::resolve_frame(frame, |symbol| { -//! if let Some(name) = symbol.name() { -//! // ... -//! } -//! if let Some(filename) = symbol.filename() { -//! // ... -//! } -//! }); -//! -//! true // keep going to the next frame +//! backtrace::trace(|frame| { +//! let ip = frame.ip(); +//! let symbol_address = frame.symbol_address(); +//! +//! // Resolve this instruction pointer to a symbol name +//! backtrace::resolve_frame(frame, |symbol| { +//! if let Some(name) = symbol.name() { +//! // ... +//! } +//! if let Some(filename) = symbol.filename() { +//! // ... +//! } //! }); -//! } +//! +//! true // keep going to the next frame +//! }); //! # } //! ``` //! diff --git a/src/print.rs b/src/print.rs index 6e38a0f1..a89535de 100644 --- a/src/print.rs +++ b/src/print.rs @@ -289,7 +289,7 @@ impl BacktraceFrameFmt<'_, '_, '_> { write!(self.fmt.fmt, ":{colno}")?; } - write!(self.fmt.fmt, "\n")?; + writeln!(self.fmt.fmt)?; Ok(()) } diff --git a/src/symbolize/gimli.rs b/src/symbolize/gimli.rs index 7465f755..ce37a541 100644 --- a/src/symbolize/gimli.rs +++ b/src/symbolize/gimli.rs @@ -12,7 +12,6 @@ use super::SymbolName; use addr2line::gimli; use core::convert::TryInto; use core::mem; -use core::u32; use libc::c_void; use mystd::ffi::OsString; use mystd::fs::File; @@ -100,7 +99,7 @@ impl Mapping { // only borrow `map` and `stash` and we're preserving them below. cx: unsafe { core::mem::transmute::, Context<'static>>(cx) }, _map: data, - stash: stash, + stash, }) } } @@ -122,13 +121,11 @@ impl<'data> Context<'data> { if cfg!(not(target_os = "aix")) { let data = object.section(stash, id.name()).unwrap_or(&[]); Ok(EndianSlice::new(data, Endian)) + } else if let Some(name) = id.xcoff_name() { + let data = object.section(stash, name).unwrap_or(&[]); + Ok(EndianSlice::new(data, Endian)) } else { - if let Some(name) = id.xcoff_name() { - let data = object.section(stash, name).unwrap_or(&[]); - Ok(EndianSlice::new(data, Endian)) - } else { - Ok(EndianSlice::new(&[], Endian)) - } + Ok(EndianSlice::new(&[], Endian)) } }) .ok()?; @@ -336,7 +333,7 @@ impl Cache { // never happen, and symbolicating backtraces would be ssssllllooooowwww. static mut MAPPINGS_CACHE: Option = None; - f(MAPPINGS_CACHE.get_or_insert_with(|| Cache::new())) + f(MAPPINGS_CACHE.get_or_insert_with(Cache::new)) } fn avma_to_svma(&self, addr: *const u8) -> Option<(usize, *const u8)> { diff --git a/src/symbolize/gimli/elf.rs b/src/symbolize/gimli/elf.rs index 906a3005..b3d04565 100644 --- a/src/symbolize/gimli/elf.rs +++ b/src/symbolize/gimli/elf.rs @@ -21,7 +21,7 @@ impl Mapping { pub fn new(path: &Path) -> Option { let map = super::mmap(path)?; Mapping::mk_or_other(map, |map, stash| { - let object = Object::parse(&map)?; + let object = Object::parse(map)?; // Try to locate an external debug file using the build ID. if let Some(path_debug) = object.build_id().and_then(locate_build_id) { @@ -47,7 +47,7 @@ impl Mapping { fn new_debug(original_path: &Path, path: PathBuf, crc: Option) -> Option { let map = super::mmap(&path)?; Mapping::mk(map, |map, stash| { - let object = Object::parse(&map)?; + let object = Object::parse(map)?; if let Some(_crc) = crc { // TODO: check crc @@ -145,8 +145,8 @@ impl<'a> Object<'a> { // symbolicating with locally defined functions. .filter(|sym| sym.st_shndx(endian) != object::elf::SHN_UNDEF) .map(|sym| { - let address = sym.st_value(endian).into(); - let size = sym.st_size(endian).into(); + let address = sym.st_value(endian); + let size = sym.st_size(endian); let name = sym.st_name(endian); ParsedSym { address, @@ -171,7 +171,7 @@ impl<'a> Object<'a> { // Check for DWARF-standard (gABI) compression, i.e., as generated // by ld's `--compress-debug-sections=zlib-gabi` flag. - let flags: u64 = section.sh_flags(self.endian).into(); + let flags: u64 = section.sh_flags(self.endian); if (flags & u64::from(SHF_COMPRESSED)) == 0 { // Not compressed. return Some(data.0); @@ -224,7 +224,7 @@ impl<'a> Object<'a> { .map(|(_index, section)| section) } - pub fn search_symtab<'b>(&'b self, addr: u64) -> Option<&'b [u8]> { + pub fn search_symtab(&self, addr: u64) -> Option<&[u8]> { // Same sort of binary search as Windows above let i = match self.syms.binary_search_by_key(&addr, |sym| sym.address) { Ok(i) => i, diff --git a/src/symbolize/gimli/libs_dl_iterate_phdr.rs b/src/symbolize/gimli/libs_dl_iterate_phdr.rs index c8558e62..566cf901 100644 --- a/src/symbolize/gimli/libs_dl_iterate_phdr.rs +++ b/src/symbolize/gimli/libs_dl_iterate_phdr.rs @@ -14,7 +14,7 @@ pub(super) fn native_libraries() -> Vec { unsafe { libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret).cast()); } - return ret; + ret } fn infer_current_exe(base_addr: usize) -> OsString { @@ -65,8 +65,8 @@ unsafe extern "C" fn callback( segments: headers .iter() .map(|header| LibrarySegment { - len: (*header).p_memsz as usize, - stated_virtual_memory_address: (*header).p_vaddr as usize, + len: header.p_memsz as usize, + stated_virtual_memory_address: header.p_vaddr as usize, }) .collect(), bias: info.dlpi_addr as usize, diff --git a/src/symbolize/mod.rs b/src/symbolize/mod.rs index 64542bdd..448bc83b 100644 --- a/src/symbolize/mod.rs +++ b/src/symbolize/mod.rs @@ -63,7 +63,7 @@ pub fn resolve(addr: *mut c_void, cb: F) { unsafe { resolve_unsynchronized(addr, cb) } } -/// Resolve a previously capture frame to a symbol, passing the symbol to the +/// Resolve a previously captured frame to a symbol, passing the symbol to the /// specified closure. /// /// This function performs the same function as `resolve` except that it takes a @@ -346,7 +346,7 @@ fn format_symbol_name( mut bytes: &[u8], f: &mut fmt::Formatter<'_>, ) -> fmt::Result { - while bytes.len() > 0 { + while !bytes.is_empty() { match str::from_utf8(bytes) { Ok(name) => { fmt(name, f)?; diff --git a/src/types.rs b/src/types.rs index c7e55105..c419247a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -33,9 +33,9 @@ impl<'a> BytesOrWideString<'a> { pub fn to_str_lossy(&self) -> Cow<'a, str> { use self::BytesOrWideString::*; - match self { - &Bytes(slice) => String::from_utf8_lossy(slice), - &Wide(wide) => Cow::Owned(String::from_utf16_lossy(wide)), + match *self { + Bytes(slice) => String::from_utf8_lossy(slice), + Wide(wide) => Cow::Owned(String::from_utf16_lossy(wide)), } }