Skip to content

Commit 29b6267

Browse files
mkrasnitskiElykDeer
authored andcommitted
Fix clippy warnings and run rustfmt
1 parent 608f261 commit 29b6267

File tree

14 files changed

+59
-99
lines changed

14 files changed

+59
-99
lines changed

rust/examples/decompile/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn decompile_to_c(view: &BinaryView, func: &Function) {
2626
let last = view.get_next_linear_disassembly_lines(&mut cursor.duplicate());
2727
let first = view.get_previous_linear_disassembly_lines(&mut cursor);
2828

29-
let lines = first.into_iter().chain(last.into_iter());
29+
let lines = first.into_iter().chain(&last);
3030

3131
for line in lines {
3232
println!("{}", line.as_ref());

rust/examples/dwarf/dwarf_export/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,11 @@ fn export_data_vars(
522522

523523
for data_variable in &bv.data_variables() {
524524
if let Some(symbol) = data_variable.symbol(bv) {
525-
if symbol.sym_type() == SymbolType::External {
526-
continue;
527-
} else if symbol.sym_type() == SymbolType::Function {
528-
continue;
529-
} else if symbol.sym_type() == SymbolType::ImportedFunction {
530-
continue;
531-
} else if symbol.sym_type() == SymbolType::LibraryFunction {
525+
if let SymbolType::External
526+
| SymbolType::Function
527+
| SymbolType::ImportedFunction
528+
| SymbolType::LibraryFunction = symbol.sym_type()
529+
{
532530
continue;
533531
}
534532
}

rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use gimli::{DebuggingInformationEntry, Dwarf, Reader, Unit};
2828

2929
use log::{error, warn};
3030
use std::{
31+
cmp::Ordering,
3132
collections::{hash_map::Values, HashMap},
3233
hash::Hash,
3334
};
@@ -222,13 +223,7 @@ impl DebugInfoBuilder {
222223
self.types.values()
223224
}
224225

225-
pub(crate) fn add_type(
226-
&mut self,
227-
type_uid: TypeUID,
228-
name: String,
229-
t: Ref<Type>,
230-
commit: bool,
231-
) {
226+
pub(crate) fn add_type(&mut self, type_uid: TypeUID, name: String, t: Ref<Type>, commit: bool) {
232227
if let Some(DebugType {
233228
name: existing_name,
234229
t: existing_type,
@@ -379,19 +374,20 @@ impl DebugInfoBuilder {
379374
if simplify_str_to_fqn(func_full_name, true).len()
380375
< simplify_str_to_fqn(symbol_full_name.clone(), true).len()
381376
{
382-
func.full_name =
383-
Some(symbol_full_name.to_string());
377+
func.full_name = Some(symbol_full_name.to_string());
384378
}
385379
}
386380
}
387381
}
388382

389383
if let Some(address) = func.address {
390384
let existing_functions = bv.functions_at(address);
391-
if existing_functions.len() > 1 {
392-
warn!("Multiple existing functions at address {address:08x}. One or more functions at this address may have the wrong platform information. Please report this binary.");
393-
} else if existing_functions.len() == 1 {
394-
func.platform = Some(existing_functions.get(0).platform());
385+
match existing_functions.len().cmp(&1) {
386+
Ordering::Greater => {
387+
warn!("Multiple existing functions at address {address:08x}. One or more functions at this address may have the wrong platform information. Please report this binary.");
388+
}
389+
Ordering::Equal => func.platform = Some(existing_functions.get(0).platform()),
390+
Ordering::Less => {}
395391
}
396392
}
397393
}

rust/examples/dwarf/dwarf_import/src/lib.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ fn recover_names<R: Reader<Offset = usize>>(
106106
}
107107
}
108108
} else {
109-
namespace_qualifiers
110-
.push((depth, "anonymous_namespace".to_string()));
109+
namespace_qualifiers.push((depth, "anonymous_namespace".to_string()));
111110
}
112111
}
113112

@@ -129,22 +128,24 @@ fn recover_names<R: Reader<Offset = usize>>(
129128
depth,
130129
match entry.tag() {
131130
constants::DW_TAG_class_type => "anonymous_class".to_string(),
132-
constants::DW_TAG_structure_type => "anonymous_structure".to_string(),
131+
constants::DW_TAG_structure_type => {
132+
"anonymous_structure".to_string()
133+
}
133134
constants::DW_TAG_union_type => "anonymous_union".to_string(),
134135
_ => unreachable!(),
135-
}
136+
},
136137
))
137138
}
138139
debug_info_builder_context.set_name(
139140
get_uid(&unit, entry),
140-
simplify_str_to_str(
141-
namespace_qualifiers
142-
.iter()
143-
.map(|(_, namespace)| namespace.to_owned())
144-
.collect::<Vec<String>>()
145-
.join("::"),
146-
)
147-
.to_string(),
141+
simplify_str_to_str(
142+
namespace_qualifiers
143+
.iter()
144+
.map(|(_, namespace)| namespace.to_owned())
145+
.collect::<Vec<String>>()
146+
.join("::"),
147+
)
148+
.to_string(),
148149
);
149150
}
150151
constants::DW_TAG_typedef
@@ -153,17 +154,15 @@ fn recover_names<R: Reader<Offset = usize>>(
153154
if let Some(name) = get_name(&unit, entry, debug_info_builder_context) {
154155
debug_info_builder_context.set_name(
155156
get_uid(&unit, entry),
156-
simplify_str_to_str(
157-
namespace_qualifiers
158-
.iter()
159-
.chain(vec![&(-1, name)].into_iter())
160-
.map(|(_, namespace)| {
161-
namespace.to_owned()
162-
})
163-
.collect::<Vec<String>>()
164-
.join("::"),
165-
)
166-
.to_string(),
157+
simplify_str_to_str(
158+
namespace_qualifiers
159+
.iter()
160+
.chain(vec![&(-1, name)].into_iter())
161+
.map(|(_, namespace)| namespace.to_owned())
162+
.collect::<Vec<String>>()
163+
.join("::"),
164+
)
165+
.to_string(),
167166
);
168167
}
169168
}

rust/examples/dwarf/dwarfdump/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use gimli::{
3333
UnitSectionOffset,
3434
};
3535

36-
static PADDING: [&'static str; 23] = [
36+
static PADDING: [&str; 23] = [
3737
"",
3838
" ",
3939
" ",
@@ -189,7 +189,7 @@ fn get_info_string<R: Reader>(
189189
let value_string = format!("{}", value);
190190
attr_line.push(InstructionTextToken::new(
191191
&value_string,
192-
InstructionTextTokenContents::Integer(value.into()),
192+
InstructionTextTokenContents::Integer(value),
193193
));
194194
} else if let Some(value) = attr.sdata_value() {
195195
let value_string = format!("{}", value);

rust/examples/hlil_visitor/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn print_variable(func: &HighLevelILFunction, var: &Variable) {
2020
fn print_il_expr(instr: &HighLevelILLiftedInstruction, mut indent: usize) {
2121
print_indent(indent);
2222
print_operation(instr);
23-
println!("");
23+
println!();
2424

2525
indent += 1;
2626

rust/examples/minidump/src/command.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ use minidump::{Minidump, MinidumpMemoryInfoList};
55

66
use binaryninja::binaryview::{BinaryView, BinaryViewBase, BinaryViewExt};
77

8-
use crate::view::DataBufferWrapper;
9-
108
pub fn print_memory_information(bv: &BinaryView) {
119
debug!("Printing memory information");
1210
if let Ok(minidump_bv) = bv.parent_view() {
1311
if let Ok(read_buffer) = minidump_bv.read_buffer(0, minidump_bv.len()) {
14-
let read_buffer = DataBufferWrapper::new(read_buffer);
15-
if let Ok(minidump_obj) = Minidump::read(read_buffer) {
12+
if let Ok(minidump_obj) = Minidump::read(read_buffer.get_data()) {
1613
if let Ok(memory_info_list) = minidump_obj.get_stream::<MinidumpMemoryInfoList>() {
1714
let mut memory_info_list_writer = Vec::new();
1815
match memory_info_list.print(&mut memory_info_list_writer) {

rust/examples/minidump/src/view.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::HashMap;
2-
use std::ops::{Deref, Range};
3-
use std::sync::Arc;
2+
use std::ops::Range;
43

54
use binaryninja::section::Section;
65
use binaryninja::segment::Segment;
@@ -16,37 +15,11 @@ use binaryninja::custombinaryview::{
1615
BinaryViewType, BinaryViewTypeBase, CustomBinaryView, CustomBinaryViewType, CustomView,
1716
CustomViewBuilder,
1817
};
19-
use binaryninja::databuffer::DataBuffer;
2018
use binaryninja::platform::Platform;
2119
use binaryninja::Endianness;
2220

2321
type BinaryViewResult<R> = binaryninja::binaryview::Result<R>;
2422

25-
/// A wrapper around a `binaryninja::databuffer::DataBuffer`, from which a `[u8]` buffer can be obtained
26-
/// to pass to `minidump::Minidump::read`.
27-
///
28-
/// This code is taken from [`dwarfdump`](https://github.com/Vector35/binaryninja-api/blob/9d8bc846bd213407fb1a7a19af2a96f17501ac3b/rust/examples/dwarfdump/src/lib.rs#L81)
29-
/// in the Rust API examples.
30-
#[derive(Clone)]
31-
pub struct DataBufferWrapper {
32-
inner: Arc<DataBuffer>,
33-
}
34-
35-
impl DataBufferWrapper {
36-
pub fn new(buf: DataBuffer) -> Self {
37-
DataBufferWrapper {
38-
inner: Arc::new(buf),
39-
}
40-
}
41-
}
42-
43-
impl Deref for DataBufferWrapper {
44-
type Target = [u8];
45-
fn deref(&self) -> &Self::Target {
46-
self.inner.get_data()
47-
}
48-
}
49-
5023
/// The _Minidump_ binary view type, which the Rust plugin registers with the Binary Ninja core
5124
/// (via `binaryninja::custombinaryview::register_view_type`) as a possible binary view
5225
/// that can be applied to opened binaries.
@@ -141,9 +114,8 @@ impl MinidumpBinaryView {
141114
fn init(&self) -> BinaryViewResult<()> {
142115
let parent_view = self.parent_view()?;
143116
let read_buffer = parent_view.read_buffer(0, parent_view.len())?;
144-
let read_buffer = DataBufferWrapper::new(read_buffer);
145117

146-
if let Ok(minidump_obj) = Minidump::read(read_buffer) {
118+
if let Ok(minidump_obj) = Minidump::read(read_buffer.get_data()) {
147119
// Architecture, platform information
148120
if let Ok(minidump_system_info) = minidump_obj.get_stream::<MinidumpSystemInfo>() {
149121
if let Some(platform) = MinidumpBinaryView::translate_minidump_platform(

rust/examples/mlil_visitor/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn print_variable(func: &MediumLevelILFunction, var: &Variable) {
2020
fn print_il_expr(instr: &MediumLevelILLiftedInstruction, mut indent: usize) {
2121
print_indent(indent);
2222
print_operation(instr);
23-
println!("");
23+
println!();
2424

2525
indent += 1;
2626

rust/src/binaryview.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,11 +1584,9 @@ where
15841584
ctx: *mut ::std::os::raw::c_void,
15851585
view: *mut BNBinaryView,
15861586
) {
1587-
ffi_wrap!("EventHandler::on_event", unsafe {
1588-
let mut context = &mut *(ctx as *mut Handler);
1589-
1590-
let handle = BinaryView::from_raw(BNNewViewReference(view));
1591-
Handler::on_event(&mut context, handle.as_ref());
1587+
ffi_wrap!("EventHandler::on_event", {
1588+
let context = unsafe { &*(ctx as *const Handler) };
1589+
context.on_event(&BinaryView::from_raw(BNNewViewReference(view)));
15921590
})
15931591
}
15941592

0 commit comments

Comments
 (0)