Skip to content

Commit 386a42a

Browse files
authored
Apply some of the cargo clippy suggestions (#649)
1 parent df56954 commit 386a42a

11 files changed

+46
-41
lines changed

src/capture.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::from_over_into)]
2+
13
#[cfg(feature = "serde")]
24
use crate::resolve;
35
use crate::PrintFmt;
@@ -351,7 +353,7 @@ impl From<crate::Frame> for BacktraceFrame {
351353
}
352354
}
353355

354-
// we don't want implementing `impl From<Backtrace> for Vec<BacktraceFrame>` on purpose,
356+
// we don't want to implement `impl From<Backtrace> for Vec<BacktraceFrame>` on purpose,
355357
// because "... additional directions for Vec<T> can weaken type inference ..."
356358
// more information on https://github.com/rust-lang/backtrace-rs/pull/526
357359
impl Into<Vec<BacktraceFrame>> for Backtrace {
@@ -452,7 +454,7 @@ impl BacktraceSymbol {
452454
/// This function requires the `std` feature of the `backtrace` crate to be
453455
/// enabled, and the `std` feature is enabled by default.
454456
pub fn filename(&self) -> Option<&Path> {
455-
self.filename.as_ref().map(|p| &**p)
457+
self.filename.as_deref()
456458
}
457459

458460
/// Same as `Symbol::lineno`

src/lib.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,24 @@
1818
//! Next:
1919
//!
2020
//! ```
21-
//! fn main() {
2221
//! # // Unsafe here so test passes on no_std.
2322
//! # #[cfg(feature = "std")] {
24-
//! backtrace::trace(|frame| {
25-
//! let ip = frame.ip();
26-
//! let symbol_address = frame.symbol_address();
27-
//!
28-
//! // Resolve this instruction pointer to a symbol name
29-
//! backtrace::resolve_frame(frame, |symbol| {
30-
//! if let Some(name) = symbol.name() {
31-
//! // ...
32-
//! }
33-
//! if let Some(filename) = symbol.filename() {
34-
//! // ...
35-
//! }
36-
//! });
37-
//!
38-
//! true // keep going to the next frame
23+
//! backtrace::trace(|frame| {
24+
//! let ip = frame.ip();
25+
//! let symbol_address = frame.symbol_address();
26+
//!
27+
//! // Resolve this instruction pointer to a symbol name
28+
//! backtrace::resolve_frame(frame, |symbol| {
29+
//! if let Some(name) = symbol.name() {
30+
//! // ...
31+
//! }
32+
//! if let Some(filename) = symbol.filename() {
33+
//! // ...
34+
//! }
3935
//! });
40-
//! }
36+
//!
37+
//! true // keep going to the next frame
38+
//! });
4139
//! # }
4240
//! ```
4341
//!

src/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl BacktraceFrameFmt<'_, '_, '_> {
288288
write!(self.fmt.fmt, ":{colno}")?;
289289
}
290290

291-
write!(self.fmt.fmt, "\n")?;
291+
writeln!(self.fmt.fmt)?;
292292
Ok(())
293293
}
294294

src/print/fuchsia.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl<'a> Iterator for NoteIter<'a> {
196196
type Item = Note<'a>;
197197
fn next(&mut self) -> Option<Self::Item> {
198198
// Check if we've reached the end.
199-
if self.base.len() == 0 || self.error {
199+
if self.base.is_empty() || self.error {
200200
return None;
201201
}
202202
// We transmute out an nhdr but we carefully consider the resulting

src/symbolize/gimli.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use super::SymbolName;
1212
use addr2line::gimli;
1313
use core::convert::TryInto;
1414
use core::mem;
15-
use core::u32;
1615
use libc::c_void;
1716
use mystd::ffi::OsString;
1817
use mystd::fs::File;
@@ -335,7 +334,7 @@ impl Cache {
335334
// never happen, and symbolicating backtraces would be ssssllllooooowwww.
336335
static mut MAPPINGS_CACHE: Option<Cache> = None;
337336

338-
f(MAPPINGS_CACHE.get_or_insert_with(|| Cache::new()))
337+
f(MAPPINGS_CACHE.get_or_insert_with(Cache::new))
339338
}
340339

341340
fn avma_to_svma(&self, addr: *const u8) -> Option<(usize, *const u8)> {

src/symbolize/gimli/elf.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::useless_conversion)]
2+
13
use super::mystd::ffi::{OsStr, OsString};
24
use super::mystd::fs;
35
use super::mystd::os::unix::ffi::{OsStrExt, OsStringExt};
@@ -21,7 +23,7 @@ impl Mapping {
2123
pub fn new(path: &Path) -> Option<Mapping> {
2224
let map = super::mmap(path)?;
2325
Mapping::mk_or_other(map, |map, stash| {
24-
let object = Object::parse(&map)?;
26+
let object = Object::parse(map)?;
2527

2628
// Try to locate an external debug file using the build ID.
2729
if let Some(path_debug) = object.build_id().and_then(locate_build_id) {
@@ -47,7 +49,7 @@ impl Mapping {
4749
fn new_debug(original_path: &Path, path: PathBuf, crc: Option<u32>) -> Option<Mapping> {
4850
let map = super::mmap(&path)?;
4951
Mapping::mk(map, |map, stash| {
50-
let object = Object::parse(&map)?;
52+
let object = Object::parse(map)?;
5153

5254
if let Some(_crc) = crc {
5355
// TODO: check crc
@@ -224,7 +226,7 @@ impl<'a> Object<'a> {
224226
.map(|(_index, section)| section)
225227
}
226228

227-
pub fn search_symtab<'b>(&'b self, addr: u64) -> Option<&'b [u8]> {
229+
pub fn search_symtab(&self, addr: u64) -> Option<&[u8]> {
228230
// Same sort of binary search as Windows above
229231
let i = match self.syms.binary_search_by_key(&addr, |sym| sym.address) {
230232
Ok(i) => i,

src/symbolize/gimli/libs_dl_iterate_phdr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn native_libraries() -> Vec<Library> {
1414
unsafe {
1515
libc::dl_iterate_phdr(Some(callback), core::ptr::addr_of_mut!(ret).cast());
1616
}
17-
return ret;
17+
ret
1818
}
1919

2020
fn infer_current_exe(base_addr: usize) -> OsString {
@@ -84,8 +84,8 @@ unsafe extern "C" fn callback(
8484
segments: headers
8585
.iter()
8686
.map(|header| LibrarySegment {
87-
len: (*header).p_memsz as usize,
88-
stated_virtual_memory_address: (*header).p_vaddr as usize,
87+
len: header.p_memsz as usize,
88+
stated_virtual_memory_address: header.p_vaddr as usize,
8989
})
9090
.collect(),
9191
bias: dlpi_addr as usize,

src/symbolize/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn resolve<F: FnMut(&Symbol)>(addr: *mut c_void, cb: F) {
6363
unsafe { resolve_unsynchronized(addr, cb) }
6464
}
6565

66-
/// Resolve a previously capture frame to a symbol, passing the symbol to the
66+
/// Resolve a previously captured frame to a symbol, passing the symbol to the
6767
/// specified closure.
6868
///
6969
/// This function performs the same function as `resolve` except that it takes a

src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ impl<'a> BytesOrWideString<'a> {
3333
pub fn to_str_lossy(&self) -> Cow<'a, str> {
3434
use self::BytesOrWideString::*;
3535

36-
match self {
37-
&Bytes(slice) => String::from_utf8_lossy(slice),
38-
&Wide(wide) => Cow::Owned(String::from_utf16_lossy(wide)),
36+
match *self {
37+
Bytes(slice) => String::from_utf8_lossy(slice),
38+
Wide(wide) => Cow::Owned(String::from_utf16_lossy(wide)),
3939
}
4040
}
4141

tests/skip_inner_frames.rs

+4
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ fn backtrace_new_should_start_with_call_site_trace() {
4444
let this_ip = backtrace_new_should_start_with_call_site_trace as *mut c_void;
4545
let frame_ip = b.frames().first().unwrap().symbol_address();
4646
assert_eq!(this_ip, frame_ip);
47+
48+
let trace = format!("{b:?}");
49+
// FIXME: need more stacktrace content tests
50+
assert!(trace.ends_with("\n"));
4751
}

tests/smoke.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,20 @@ fn smoke_test_frames() {
190190
);
191191
}
192192
if expected_line != 0 {
193-
assert!(
194-
line == expected_line,
195-
"bad line number on frame for `{expected_name}`: {line} != {expected_line}"
196-
);
193+
assert_eq!(
194+
line,
195+
expected_line,
196+
"bad line number on frame for `{expected_name}`: {line} != {expected_line}");
197197
}
198198

199199
// dbghelp on MSVC doesn't support column numbers
200200
if !cfg!(target_env = "msvc") {
201201
let col = col.expect("didn't find a column number");
202202
if expected_col != 0 {
203-
assert!(
204-
col == expected_col,
205-
"bad column number on frame for `{expected_name}`: {col} != {expected_col}",
206-
);
203+
assert_eq!(
204+
col,
205+
expected_col,
206+
"bad column number on frame for `{expected_name}`: {col} != {expected_col}");
207207
}
208208
}
209209
}

0 commit comments

Comments
 (0)