From 0ee4c614fd9a28113d13935066a6d5266b84c4b7 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 23 Sep 2025 13:50:00 -0700 Subject: [PATCH 1/6] cleanup: let-chain in style selection --- src/capture.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/capture.rs b/src/capture.rs index b22e0a3c..e408106b 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -5,9 +5,9 @@ use crate::PrintFmt; use crate::resolve; use crate::{BacktraceFmt, Symbol, SymbolName, resolve_frame, trace}; use core::ffi::c_void; -use std::fmt; use std::path::{Path, PathBuf}; use std::prelude::v1::*; +use std::{env, fmt}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -490,22 +490,21 @@ impl fmt::Debug for Backtrace { PrintFmt::Short }; - // When printing paths we try to strip the cwd if it exists, otherwise - // we just print the path as-is. Note that we also only do this for the // short format, because if it's full we presumably want to print // everything. - let cwd = std::env::current_dir(); let mut print_path = move |fmt: &mut fmt::Formatter<'_>, path: crate::BytesOrWideString<'_>| { let path = path.into_path_buf(); - if style != PrintFmt::Full { - if let Ok(cwd) = &cwd { - if let Ok(suffix) = path.strip_prefix(cwd) { - return fmt::Display::fmt(&suffix.display(), fmt); - } - } + // When printing paths we try to strip the cwd if it exists, otherwise + // we just print the path as-is + if style == PrintFmt::Short + && let Ok(cwd) = &env::current_dir() + && let Ok(suffix) = path.strip_prefix(cwd) + { + fmt::Display::fmt(&suffix.display(), fmt) + } else { + fmt::Display::fmt(&path.display(), fmt) } - fmt::Display::fmt(&path.display(), fmt) }; let mut f = BacktraceFmt::new(fmt, style, &mut print_path); From 5dcce86cc7f9c4d0f25d683c265fe12c7e1d1c6f Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 23 Sep 2025 14:02:25 -0700 Subject: [PATCH 2/6] more let chains! --- src/symbolize/gimli/macho.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/symbolize/gimli/macho.rs b/src/symbolize/gimli/macho.rs index 7112694d..06f6a1cc 100644 --- a/src/symbolize/gimli/macho.rs +++ b/src/symbolize/gimli/macho.rs @@ -34,12 +34,11 @@ impl Mapping { // contains and try to find a macho file which has a matching UUID as // the one of our own file. If we find a match that's the dwarf file we // want to return. - if let Some(uuid) = uuid { - if let Some(parent) = path.parent() { - if let Some(mapping) = Mapping::load_dsym(parent, uuid) { - return Some(mapping); - } - } + if let Some(uuid) = uuid + && let Some(parent) = path.parent() + && let Some(mapping) = Mapping::load_dsym(parent, uuid) + { + return Some(mapping); } // Looks like nothing matched our UUID, so let's at least return our own From a1a50d927258f8334e1908b0fea0494d9adb1cb7 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 23 Sep 2025 14:06:12 -0700 Subject: [PATCH 3/6] more let-chains! --- src/types.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/types.rs b/src/types.rs index c419247a..c30bd08e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -66,12 +66,15 @@ impl<'a> BytesOrWideString<'a> { } } - if let BytesOrWideString::Bytes(b) = self { - if let Ok(s) = str::from_utf8(b) { - return PathBuf::from(s); - } + // cross-platform fallback is to pray we are dealing with utf-8 + if let BytesOrWideString::Bytes(b) = self + && let Ok(s) = str::from_utf8(b) + { + PathBuf::from(s); + } else { + // or die + unreachable!() } - unreachable!() } } From 16c98516f2d81c5e277bac697d068b992c42c7a2 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 23 Sep 2025 14:09:32 -0700 Subject: [PATCH 4/6] more let-chains! --- src/symbolize/gimli/elf.rs | 40 +++++++++++++++++++------------------- src/types.rs | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/symbolize/gimli/elf.rs b/src/symbolize/gimli/elf.rs index fd9903d1..646b8d2b 100644 --- a/src/symbolize/gimli/elf.rs +++ b/src/symbolize/gimli/elf.rs @@ -30,17 +30,17 @@ impl Mapping { 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) { - if let Some(mapping) = Mapping::new_debug(path, path_debug, None) { - return Some(Either::A(mapping)); - } + if let Some(path_debug) = object.build_id().and_then(locate_build_id) + && let Some(mapping) = Mapping::new_debug(path, path_debug, None) + { + return Some(Either::A(mapping)); } // Try to locate an external debug file using the GNU debug link section. - if let Some((path_debug, crc)) = object.gnu_debuglink_path(path) { - if let Some(mapping) = Mapping::new_debug(path, path_debug, Some(crc)) { - return Some(Either::A(mapping)); - } + if let Some((path_debug, crc)) = object.gnu_debuglink_path(path) + && let Some(mapping) = Mapping::new_debug(path, path_debug, Some(crc)) + { + return Some(Either::A(mapping)); } let dwp = Mapping::load_dwarf_package(path, stash); @@ -102,14 +102,14 @@ impl Mapping { // Try to locate a supplementary object file. let mut sup = None; - if let Some((path_sup, build_id_sup)) = object.gnu_debugaltlink_path(&path) { - if let Some(map_sup) = super::mmap(&path_sup) { - let map_sup = stash.cache_mmap(map_sup); - if let Some(sup_) = Object::parse(map_sup) { - if sup_.build_id() == Some(build_id_sup) { - sup = Some(sup_); - } - } + if let Some((path_sup, build_id_sup)) = object.gnu_debugaltlink_path(&path) + && let Some(map_sup) = super::mmap(&path_sup) + { + let map_sup = stash.cache_mmap(map_sup); + if let Some(sup_) = Object::parse(map_sup) + && sup_.build_id() == Some(build_id_sup) + { + sup = Some(sup_); } } @@ -532,10 +532,10 @@ pub(super) fn handle_split_dwarf<'data>( stash: &'data Stash, load: addr2line::SplitDwarfLoad>, ) -> Option>>> { - if let Some(dwp) = package.as_ref() { - if let Ok(Some(cu)) = dwp.find_cu(load.dwo_id, &load.parent) { - return Some(Arc::new(cu)); - } + if let Some(dwp) = package.as_ref() + && let Ok(Some(cu)) = dwp.find_cu(load.dwo_id, &load.parent) + { + return Some(Arc::new(cu)); } let mut path = PathBuf::new(); diff --git a/src/types.rs b/src/types.rs index c30bd08e..61f66c4a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -70,7 +70,7 @@ impl<'a> BytesOrWideString<'a> { if let BytesOrWideString::Bytes(b) = self && let Ok(s) = str::from_utf8(b) { - PathBuf::from(s); + PathBuf::from(s) } else { // or die unreachable!() From e9188a5ef8d21e7d84b6694c91f1b6cf0adc2117 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 23 Sep 2025 14:12:17 -0700 Subject: [PATCH 5/6] let chain harder! --- src/symbolize/gimli.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/symbolize/gimli.rs b/src/symbolize/gimli.rs index cbc0b768..b691bf2c 100644 --- a/src/symbolize/gimli.rs +++ b/src/symbolize/gimli.rs @@ -478,16 +478,16 @@ pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) } } if !any_frames { - if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) { - if let Ok(mut frames) = object_cx.find_frames(stash, object_addr) { - while let Ok(Some(frame)) = frames.next() { - any_frames = true; - call(Symbol::Frame { - addr: addr as *mut c_void, - location: frame.location, - name: frame.function.map(|f| f.name.slice()), - }); - } + if let Some((object_cx, object_addr)) = cx.object.search_object_map(addr as u64) + && let Ok(mut frames) = object_cx.find_frames(stash, object_addr) + { + while let Ok(Some(frame)) = frames.next() { + any_frames = true; + call(Symbol::Frame { + addr: addr as *mut c_void, + location: frame.location, + name: frame.function.map(|f| f.name.slice()), + }); } } } From 47a42798e5dc596d9524f7b72de4fedfd396111e Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 26 Sep 2025 11:47:22 -0700 Subject: [PATCH 6/6] let-chain for cpp_demangle! --- src/symbolize/mod.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/symbolize/mod.rs b/src/symbolize/mod.rs index d221ce80..06604fbf 100644 --- a/src/symbolize/mod.rs +++ b/src/symbolize/mod.rs @@ -376,10 +376,10 @@ impl<'a> fmt::Display for SymbolName<'a> { // This may fail to print if the demangled symbol isn't actually // valid, so handle the error here gracefully by not propagating // it outwards. - if let Some(ref cpp) = self.cpp_demangled.0 { - if let Ok(s) = cpp.demangle() { - return s.fmt(f); - } + if let Some(ref cpp) = self.cpp_demangled.0 + && let Ok(s) = cpp.demangle() + { + return s.fmt(f); } } @@ -398,10 +398,10 @@ impl<'a> fmt::Debug for SymbolName<'a> { // This may fail to print if the demangled symbol isn't actually // valid, so handle the error here gracefully by not propagating // it outwards. - if let Some(ref cpp) = self.cpp_demangled.0 { - if let Ok(s) = cpp.demangle() { - return s.fmt(f); - } + if let Some(ref cpp) = self.cpp_demangled.0 + && let Ok(s) = cpp.demangle() + { + return s.fmt(f); } }