Skip to content

Commit 91b023c

Browse files
committed
[BNTL Utils] Fix misc clippy lints
1 parent 1ad3b79 commit 91b023c

9 files changed

Lines changed: 38 additions & 47 deletions

File tree

plugins/bntl_utils/src/command/create.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use binaryninja::command::{Command, ProjectCommand};
77
use binaryninja::interaction::{Form, FormInputField, MessageBoxButtonSet, MessageBoxIcon};
88
use binaryninja::platform::Platform;
99
use binaryninja::project::Project;
10-
use binaryninja::types::TypeLibrary;
1110
use std::thread;
1211

1312
pub struct CreateFromCurrentView;
@@ -226,10 +225,10 @@ impl CreateFromProject {
226225

227226
let background_task = BackgroundTask::new("Processing started...", true);
228227
new_processing_state_background_thread(background_task.clone(), processor.state());
229-
let data = processor.process_project(&project);
228+
let data = processor.process_project(project);
230229
background_task.finish();
231230

232-
let mut finalized_data = match data {
231+
let finalized_data = match data {
233232
// Prune off empty type libraries, no need to save them.
234233
Ok(data) => data.finalized(&default_name),
235234
Err(err) => {

plugins/bntl_utils/src/dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl TILDump {
5858

5959
let platform_name_str = platform_name.to_string();
6060
let platform = Platform::by_name(&platform_name_str)
61-
.ok_or_else(|| TILDumpError::PlatformNotFound(platform_name_str))?;
61+
.ok_or(TILDumpError::PlatformNotFound(platform_name_str))?;
6262

6363
empty_bv.set_default_platform(&platform);
6464

plugins/bntl_utils/src/helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn path_to_type_libraries(path: &Path) -> Vec<Ref<TypeLibrary>> {
88
.into_iter()
99
.filter_map(|e| e.ok())
1010
.filter(|e| e.file_type().is_file())
11-
.filter(|e| e.path().extension().map_or(false, |ext| ext == "bntl"))
11+
.filter(|e| e.path().extension().is_some_and(|ext| ext == "bntl"))
1212
.filter_map(|e| TypeLibrary::load_from_file(e.path()))
1313
.collect::<Vec<_>>()
1414
}

plugins/bntl_utils/src/merge.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ fn merge_structures(s1: &Structure, s2: &Structure) -> Option<Ref<Structure>> {
101101
for m in &s.members() {
102102
members
103103
.entry(m.offset)
104-
.and_modify(|(existing_name, existing_ty)| {
104+
.and_modify(|(_existing_name, existing_ty)| {
105105
// Update type if merge succeeds
106106
if let Some(merged) = merge_recursive(existing_ty, &m.ty.contents) {
107107
*existing_ty = merged;
108108
}
109-
// Name collision: Keep existing (s1/first wins), ignoring m.name
110109
})
111110
.or_insert_with(|| (m.name.clone(), m.ty.contents.to_owned()));
112111
}

plugins/bntl_utils/src/process.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl ProcessedData {
144144
}
145145

146146
pub fn finalized(mut self, default_name: &str) -> Self {
147-
self.deduplicate_types(&default_name);
147+
self.deduplicate_types(default_name);
148148
// TODO: Run remap.
149149
self.prune()
150150
}
@@ -209,7 +209,7 @@ impl ProcessedData {
209209
merged_type_library.add_alternate_name(alt_name);
210210
}
211211
for platform_name in &tl.platform_names() {
212-
if let Some(platform) = Platform::by_name(&platform_name) {
212+
if let Some(platform) = Platform::by_name(platform_name) {
213213
merged_type_library.add_platform(&platform);
214214
} else {
215215
// TODO: Upgrade this to an error?
@@ -375,14 +375,13 @@ impl ProcessedData {
375375
for type_library in type_libraries {
376376
// If the default type library does not have the platform, it will not be pulled in.
377377
for platform_name in &type_library.platform_names() {
378-
if let Some(platform) = Platform::by_name(&platform_name) {
378+
if let Some(platform) = Platform::by_name(platform_name) {
379379
default_type_library.add_platform(&platform);
380380
}
381381
}
382382

383383
type_library.remove_named_type(qualified_name.clone());
384-
type_library
385-
.add_type_source(qualified_name.clone(), &default_type_library_name);
384+
type_library.add_type_source(qualified_name.clone(), default_type_library_name);
386385
}
387386
} else {
388387
// TODO: Probably demote this to debug, since they might just be disparate types.
@@ -480,7 +479,7 @@ impl TypeLibProcessor {
480479

481480
pub fn process(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
482481
match path.extension() {
483-
Some(ext) if ext == "bntl" => self.process_type_library(&path),
482+
Some(ext) if ext == "bntl" => self.process_type_library(path),
484483
Some(ext) if ext == "h" || ext == "hpp" => self.process_source(path),
485484
// NOTE: A typical processor will not go down this path where we only provide a single
486485
// winmd file to be processed. You almost always want to process multiple winmd files,
@@ -603,18 +602,18 @@ impl TypeLibProcessor {
603602
project_file: &ProjectFile,
604603
) -> Result<ProcessedData, ProcessingError> {
605604
let file_name = project_file.name();
606-
let extension = file_name.split('.').last();
605+
let extension = file_name.split('.').next_back();
607606
let path = project_file
608607
.path_on_disk()
609608
.ok_or_else(|| ProcessingError::NoPathToProjectFile(project_file.to_owned()))?;
610609
match extension {
611-
Some(ext) if ext == "bntl" => self.process_type_library(&path),
610+
Some("bntl") => self.process_type_library(&path),
612611
Some(ext) if ext == "h" || ext == "hpp" => self.process_source(&path),
613612
// NOTE: A typical processor will not go down this path where we only provide a single
614613
// winmd file to be processed. You almost always want to process multiple winmd files,
615614
// which can be done by passing a directory with the relevant winmd files.
616-
Some(ext) if ext == "winmd" => self.process_winmd(&[path]),
617-
Some(ext) if ext == "tbd" => self.process_tbd(&path),
615+
Some("winmd") => self.process_winmd(&[path]),
616+
Some("tbd") => self.process_tbd(&path),
618617
_ => {
619618
// If the file cannot be parsed, it should be skipped to avoid a load error.
620619
if !is_parsable(&path) {
@@ -623,7 +622,7 @@ impl TypeLibProcessor {
623622

624623
let settings_str = self.analysis_settings.to_string();
625624
let file = binaryninja::load_project_file_with_progress(
626-
&project_file,
625+
project_file,
627626
false,
628627
Some(settings_str),
629628
|_pos, _total| {
@@ -654,7 +653,7 @@ impl TypeLibProcessor {
654653

655654
let settings_str = self.analysis_settings.to_string();
656655
let file = binaryninja::load_with_options_and_progress(
657-
&path,
656+
path,
658657
false,
659658
Some(settings_str),
660659
|_pos, _total| {
@@ -800,10 +799,10 @@ impl TypeLibProcessor {
800799
type_library.store_metadata("ordinals_10_0", &map_md);
801800
}
802801

803-
let mut processed_data = self.process_external_libraries(&view)?;
802+
let mut processed_data = self.process_external_libraries(view)?;
804803
processed_data.type_libraries.insert(type_library);
805804
if let Some(api_set_section) = view.section_by_name(".apiset") {
806-
let processed_api_set = self.process_api_set(&view, &api_set_section)?;
805+
let processed_api_set = self.process_api_set(view, &api_set_section)?;
807806
tracing::info!(
808807
"Found {} api set libraries in '{}', adding alternative names...",
809808
processed_api_set.type_libraries.len(),
@@ -905,7 +904,7 @@ impl TypeLibProcessor {
905904
let section_bytes = view
906905
.read_buffer(section.start(), section.len())
907906
.ok_or_else(|| ProcessingError::BinaryViewRead(section.start(), section.len()))?;
908-
let api_set_map = ApiSetMap::try_from_apiset_section_bytes(&section_bytes.get_data())?;
907+
let api_set_map = ApiSetMap::try_from_apiset_section_bytes(section_bytes.get_data())?;
909908

910909
let mut target_map: HashMap<String, HashSet<String>> = HashMap::new();
911910
for entry in api_set_map.namespace_entries()? {
@@ -944,7 +943,7 @@ impl TypeLibProcessor {
944943
/// during the [`ProcessedData::merge`] step. This lets us add overrides like extra platforms.
945944
pub fn process_type_library(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
946945
self.state.set_file_state(path.to_owned(), false);
947-
let finalized_type_library = TypeLibrary::load_from_file(&path)
946+
let finalized_type_library = TypeLibrary::load_from_file(path)
948947
.ok_or_else(|| ProcessingError::InvalidTypeLibrary(path.to_owned()))?;
949948
self.state.set_file_state(path.to_owned(), true);
950949
Ok(ProcessedData::new(vec![finalized_type_library]))
@@ -957,8 +956,7 @@ impl TypeLibProcessor {
957956
CoreTypeParser::parser_by_name("ClangTypeParser").expect("Failed to get clang parser");
958957
let platform_type_container = platform.type_container();
959958

960-
let header_contents =
961-
std::fs::read_to_string(path).map_err(|e| ProcessingError::FileRead(e))?;
959+
let header_contents = std::fs::read_to_string(path).map_err(ProcessingError::FileRead)?;
962960

963961
let file_name = path
964962
.file_name()
@@ -982,7 +980,7 @@ impl TypeLibProcessor {
982980
&include_dirs,
983981
"",
984982
)
985-
.map_err(|e| ProcessingError::TypeParsingFailed(e))?;
983+
.map_err(ProcessingError::TypeParsingFailed)?;
986984

987985
let type_library = TypeLibrary::new(platform.arch(), &self.default_dependency_name);
988986
type_library.add_platform(&platform);
@@ -1000,7 +998,7 @@ impl TypeLibProcessor {
1000998
/// most important for us is the list of exported symbols, which we can use to relocate objects
1001999
/// in the default type library (specified by `default_dependency_name`) to the correct type library.
10021000
pub fn process_tbd(&self, path: &Path) -> Result<ProcessedData, ProcessingError> {
1003-
let mut file = File::open(path).map_err(|e| ProcessingError::FileRead(e))?;
1001+
let mut file = File::open(path).map_err(ProcessingError::FileRead)?;
10041002
let mut type_libraries = Vec::new();
10051003
for tbd_info in parse_tbd_info(&mut file).unwrap() {
10061004
let install_path = PathBuf::from(tbd_info.install_name);
@@ -1066,7 +1064,7 @@ impl TypeLibProcessor {
10661064
}
10671065
let platform = self.default_platform()?;
10681066
let type_libraries = WindowsMetadataImporter::new()
1069-
.with_files(&paths)
1067+
.with_files(paths)
10701068
.map_err(ProcessingError::WinMdFailedImport)?
10711069
.import(&platform)
10721070
.map_err(ProcessingError::WinMdFailedImport)?;
@@ -1090,8 +1088,8 @@ pub fn is_parsable(path: &Path) -> bool {
10901088
if path.extension() == Some(OsStr::new("pdb")) {
10911089
return false;
10921090
}
1093-
let mut metadata = FileMetadata::with_file_path(&path);
1094-
let Ok(view) = BinaryView::from_path(&mut metadata, path) else {
1091+
let mut metadata = FileMetadata::with_file_path(path);
1092+
let Ok(view) = BinaryView::from_path(&metadata, path) else {
10951093
return false;
10961094
};
10971095
// If any view type parses this file, consider it for this source.

plugins/bntl_utils/src/tbd.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use std::str::FromStr;
88
pub fn parse_tbd_info(data: &mut impl Read) -> Result<Vec<TbdInfo>, serde_saphyr::Error> {
99
let mut documents = Vec::new();
1010
for file in serde_saphyr::read::<_, TbdFile>(data) {
11-
if let Some(info) = TbdInfo::try_from(file?).ok() {
11+
// TODO: Float errors to caller
12+
if let Ok(info) = TbdInfo::try_from(file?) {
1213
documents.push(info);
1314
}
1415
}
@@ -301,8 +302,8 @@ impl TbdTarget {
301302
.iter()
302303
.map(|a| {
303304
Ok(TbdTarget {
304-
arch: a.clone(),
305-
platform: platform.clone(),
305+
arch: *a,
306+
platform: *platform,
306307
})
307308
})
308309
.collect()

plugins/bntl_utils/src/url.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl BnParsedUrl {
202202
.get_file_by_id(&item_guid_str)
203203
.ok()
204204
.flatten()
205-
.ok_or_else(|| BnResourceError::ItemNotFound(item_guid_str))?;
205+
.ok_or(BnResourceError::ItemNotFound(item_guid_str))?;
206206

207207
Ok(BnResource::RemoteProjectFile(file))
208208
}

plugins/bntl_utils/src/winmd.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl WindowsMetadataImporter {
7070
Ok(self)
7171
}
7272

73+
#[allow(dead_code)]
7374
pub fn with_platform(mut self, platform: &Platform) -> Self {
7475
// TODO: platform.address_size()
7576
self.address_size = platform.arch().address_size();
@@ -115,10 +116,10 @@ impl WindowsMetadataImporter {
115116
til.add_platform(platform);
116117
til.set_dependency_name(&type_lib_name);
117118
for ty in &info.metadata.types {
118-
self.import_type(&til, &ty)?;
119+
self.import_type(&til, ty)?;
119120
}
120121
for func in &info.metadata.functions {
121-
self.import_function(&til, &func)?;
122+
self.import_function(&til, func)?;
122123
}
123124
for (name, library_name) in &info.external_references {
124125
let qualified_name = QualifiedName::from(name.clone());
@@ -314,9 +315,7 @@ impl WindowsMetadataImporter {
314315
union.structure_type(StructureType::UnionStructureType);
315316

316317
let mut max_alignment = 0usize;
317-
// We need to look ahead to figure out when bitfields end and adjust current_byte_offset accordingly.
318-
let mut field_iter = fields.iter().peekable();
319-
while let Some(field) = field_iter.next() {
318+
for field in fields {
320319
let field_ty = self.convert_type_kind(&field.ty)?;
321320
let field_alignment = self.type_kind_alignment(&field.ty)?;
322321
max_alignment = max_alignment.max(field_alignment);

plugins/bntl_utils/src/winmd/translate.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl WindowsMetadataTranslator {
283283

284284
let nested: Result<HashMap<String, _>, _> = structure
285285
.index()
286-
.nested(structure.clone())
286+
.nested(*structure)
287287
.map(|n| {
288288
// TODO: Are all nested fields a struct?
289289
let nested_ty = self.translate_struct(&n)?;
@@ -372,11 +372,7 @@ impl WindowsMetadataTranslator {
372372

373373
let mut constants = Vec::new();
374374
for field in class.fields() {
375-
if let Some(constant) = field
376-
.constant()
377-
.map(|c| self.value_to_u64(&c.value()))
378-
.flatten()
379-
{
375+
if let Some(constant) = field.constant().and_then(|c| self.value_to_u64(&c.value())) {
380376
constants.push(MetadataConstantInfo {
381377
name: field.name().to_string(),
382378
namespace: namespace.clone(),
@@ -525,8 +521,7 @@ impl WindowsMetadataTranslator {
525521
}
526522
let variant_constant = variant
527523
.constant()
528-
.map(|c| self.value_to_u64(&c.value()))
529-
.flatten()
524+
.and_then(|c| self.value_to_u64(&c.value()))
530525
.unwrap_or(last_constant);
531526
let variant_name = variant.name().to_string();
532527
variants.push((variant_name, variant_constant));

0 commit comments

Comments
 (0)