diff --git a/crates/libs/bindgen/src/io.rs b/crates/libs/bindgen/src/io.rs index 29d2d8f5cd..3f51a455bc 100644 --- a/crates/libs/bindgen/src/io.rs +++ b/crates/libs/bindgen/src/io.rs @@ -1,20 +1,34 @@ use std::io::BufRead; +#[track_caller] pub fn read_file_lines(path: &str) -> Vec { - let file = std::io::BufReader::new( - std::fs::File::open(path).unwrap_or_else(|_| panic!("failed to open file `{path}`")), - ); + let Ok(file) = std::fs::File::open(path) else { + panic!("failed to open file `{path}`") + }; - file.lines() - .map(|line| line.unwrap_or_else(|_| panic!("failed to read file lines `{path}`"))) - .collect() + let file = std::io::BufReader::new(file); + let mut lines = vec![]; + + for line in file.lines() { + let Ok(line) = line else { + panic!("failed to read file lines `{path}`"); + }; + + lines.push(line); + } + + lines } +#[track_caller] pub fn write_to_file>(path: &str, contents: C) { if let Some(parent) = std::path::Path::new(path).parent() { - std::fs::create_dir_all(parent) - .unwrap_or_else(|_| panic!("failed to create directory `{path}`")); + if std::fs::create_dir_all(parent).is_err() { + panic!("failed to create directory `{path}`"); + } } - std::fs::write(path, contents).unwrap_or_else(|_| panic!("failed to write file `{path}`")); + if std::fs::write(path, contents).is_err() { + panic!("failed to write file `{path}`"); + } } diff --git a/crates/libs/bindgen/src/lib.rs b/crates/libs/bindgen/src/lib.rs index c785043bb1..fd2d363d39 100644 --- a/crates/libs/bindgen/src/lib.rs +++ b/crates/libs/bindgen/src/lib.rs @@ -201,16 +201,19 @@ enum ArgKind { Derive, } +#[track_caller] fn expand_args(args: I) -> Vec where I: IntoIterator, S: AsRef, { // This function is needed to avoid a recursion limit in the Rust compiler. + #[track_caller] fn from_string(result: &mut Vec, value: &str) { expand_args(result, value.split_whitespace().map(|arg| arg.to_string())) } + #[track_caller] fn expand_args(result: &mut Vec, args: I) where I: IntoIterator, @@ -241,7 +244,9 @@ where result } +#[track_caller] fn expand_input(input: &[&str]) -> Vec { + #[track_caller] fn expand_input(result: &mut Vec, input: &str) { let path = std::path::Path::new(input); @@ -295,12 +300,17 @@ fn expand_input(input: &[&str]) -> Vec { .collect(); } - input.extend(paths.iter().map(|path| { - let bytes = - std::fs::read(path).unwrap_or_else(|_| panic!("failed to read binary file `{path}`")); + for path in &paths { + let Ok(bytes) = std::fs::read(path) else { + panic!("failed to read binary file `{path}`"); + }; - File::new(bytes).unwrap_or_else(|| panic!("failed to read .winmd format `{path}`")) - })); + let Some(file) = File::new(bytes) else { + panic!("failed to read .winmd format `{path}`"); + }; + + input.push(file); + } input } diff --git a/crates/libs/bindgen/src/writer/mod.rs b/crates/libs/bindgen/src/writer/mod.rs index 3a784af818..cb0de64b03 100644 --- a/crates/libs/bindgen/src/writer/mod.rs +++ b/crates/libs/bindgen/src/writer/mod.rs @@ -20,6 +20,7 @@ impl Writer { clone } + #[track_caller] pub fn write(&self, tree: TypeTree) { if self.config.package { self.write_package(&tree); @@ -28,6 +29,7 @@ impl Writer { } } + #[track_caller] fn write_file(&self, tree: TypeTree) { let tokens = if self.config.flat { self.write_flat(tree)