Skip to content

Commit 77ab8ac

Browse files
committed
Apply some of the cargo clippy suggestions
1 parent fd0aed5 commit 77ab8ac

File tree

9 files changed

+41
-46
lines changed

9 files changed

+41
-46
lines changed

build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use std::path::Path;
55

66
// Must be public so the build script of `std` can call it.
77
pub fn main() {
8-
match env::var("CARGO_CFG_TARGET_OS").unwrap_or_default().as_str() {
9-
"android" => build_android(),
10-
_ => {}
8+
if env::var("CARGO_CFG_TARGET_OS").unwrap_or_default().as_str() == "android" {
9+
build_android()
1110
}
1211
}
1312

src/capture.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,10 @@ impl From<crate::Frame> for BacktraceFrame {
351351
}
352352
}
353353

354-
// we don't want implementing `impl From<Backtrace> for Vec<BacktraceFrame>` on purpose,
354+
// we don't want to implement `impl From<Backtrace> for Vec<BacktraceFrame>` on purpose,
355355
// because "... additional directions for Vec<T> can weaken type inference ..."
356356
// more information on https://github.com/rust-lang/backtrace-rs/pull/526
357+
#[allow(clippy::from_over_into)]
357358
impl Into<Vec<BacktraceFrame>> for Backtrace {
358359
fn into(self) -> Vec<BacktraceFrame> {
359360
self.frames
@@ -452,7 +453,7 @@ impl BacktraceSymbol {
452453
/// This function requires the `std` feature of the `backtrace` crate to be
453454
/// enabled, and the `std` feature is enabled by default.
454455
pub fn filename(&self) -> Option<&Path> {
455-
self.filename.as_ref().map(|p| &**p)
456+
self.filename.as_deref()
456457
}
457458

458459
/// Same as `Symbol::lineno`

src/lib.rs

Lines changed: 15 additions & 17 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl BacktraceFrameFmt<'_, '_, '_> {
289289
write!(self.fmt.fmt, ":{colno}")?;
290290
}
291291

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

src/symbolize/gimli.rs

Lines changed: 6 additions & 9 deletions
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;
@@ -100,7 +99,7 @@ impl Mapping {
10099
// only borrow `map` and `stash` and we're preserving them below.
101100
cx: unsafe { core::mem::transmute::<Context<'_>, Context<'static>>(cx) },
102101
_map: data,
103-
stash: stash,
102+
stash,
104103
})
105104
}
106105
}
@@ -122,13 +121,11 @@ impl<'data> Context<'data> {
122121
if cfg!(not(target_os = "aix")) {
123122
let data = object.section(stash, id.name()).unwrap_or(&[]);
124123
Ok(EndianSlice::new(data, Endian))
124+
} else if let Some(name) = id.xcoff_name() {
125+
let data = object.section(stash, name).unwrap_or(&[]);
126+
Ok(EndianSlice::new(data, Endian))
125127
} else {
126-
if let Some(name) = id.xcoff_name() {
127-
let data = object.section(stash, name).unwrap_or(&[]);
128-
Ok(EndianSlice::new(data, Endian))
129-
} else {
130-
Ok(EndianSlice::new(&[], Endian))
131-
}
128+
Ok(EndianSlice::new(&[], Endian))
132129
}
133130
})
134131
.ok()?;
@@ -336,7 +333,7 @@ impl Cache {
336333
// never happen, and symbolicating backtraces would be ssssllllooooowwww.
337334
static mut MAPPINGS_CACHE: Option<Cache> = None;
338335

339-
f(MAPPINGS_CACHE.get_or_insert_with(|| Cache::new()))
336+
f(MAPPINGS_CACHE.get_or_insert_with(Cache::new))
340337
}
341338

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

src/symbolize/gimli/elf.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Mapping {
2121
pub fn new(path: &Path) -> Option<Mapping> {
2222
let map = super::mmap(path)?;
2323
Mapping::mk_or_other(map, |map, stash| {
24-
let object = Object::parse(&map)?;
24+
let object = Object::parse(map)?;
2525

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

5252
if let Some(_crc) = crc {
5353
// TODO: check crc
@@ -145,8 +145,8 @@ impl<'a> Object<'a> {
145145
// symbolicating with locally defined functions.
146146
.filter(|sym| sym.st_shndx(endian) != object::elf::SHN_UNDEF)
147147
.map(|sym| {
148-
let address = sym.st_value(endian).into();
149-
let size = sym.st_size(endian).into();
148+
let address = sym.st_value(endian);
149+
let size = sym.st_size(endian);
150150
let name = sym.st_name(endian);
151151
ParsedSym {
152152
address,
@@ -171,7 +171,7 @@ impl<'a> Object<'a> {
171171

172172
// Check for DWARF-standard (gABI) compression, i.e., as generated
173173
// by ld's `--compress-debug-sections=zlib-gabi` flag.
174-
let flags: u64 = section.sh_flags(self.endian).into();
174+
let flags: u64 = section.sh_flags(self.endian);
175175
if (flags & u64::from(SHF_COMPRESSED)) == 0 {
176176
// Not compressed.
177177
return Some(data.0);
@@ -224,7 +224,7 @@ impl<'a> Object<'a> {
224224
.map(|(_index, section)| section)
225225
}
226226

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

src/symbolize/gimli/libs_dl_iterate_phdr.rs

Lines changed: 3 additions & 3 deletions
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 {
@@ -65,8 +65,8 @@ unsafe extern "C" fn callback(
6565
segments: headers
6666
.iter()
6767
.map(|header| LibrarySegment {
68-
len: (*header).p_memsz as usize,
69-
stated_virtual_memory_address: (*header).p_vaddr as usize,
68+
len: header.p_memsz as usize,
69+
stated_virtual_memory_address: header.p_vaddr as usize,
7070
})
7171
.collect(),
7272
bias: info.dlpi_addr as usize,

src/symbolize/mod.rs

Lines changed: 2 additions & 2 deletions
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
@@ -346,7 +346,7 @@ fn format_symbol_name(
346346
mut bytes: &[u8],
347347
f: &mut fmt::Formatter<'_>,
348348
) -> fmt::Result {
349-
while bytes.len() > 0 {
349+
while !bytes.is_empty() {
350350
match str::from_utf8(bytes) {
351351
Ok(name) => {
352352
fmt(name, f)?;

src/types.rs

Lines changed: 3 additions & 3 deletions
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

0 commit comments

Comments
 (0)