From 08ef0cec39fb2a64d25dcd86c61fac14c5198ed1 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Wed, 25 Oct 2023 01:14:49 -0400 Subject: [PATCH] Use `cargo fmt` --- .github/workflows/rust-bindings.yml | 5 +- rust/prism-sys/build.rs | 19 ++------ rust/prism-sys/rustfmt.toml | 2 + rust/prism-sys/tests/node_tests.rs | 11 ++--- rust/prism-sys/tests/pack_tests.rs | 5 +- rust/prism-sys/tests/parser_tests.rs | 25 ++-------- rust/prism-sys/tests/utils_tests.rs | 11 +---- rust/prism/build.rs | 52 ++++++++++++-------- rust/prism/rustfmt.toml | 2 + rust/prism/src/lib.rs | 72 +++++++++++----------------- 10 files changed, 82 insertions(+), 122 deletions(-) create mode 100644 rust/prism-sys/rustfmt.toml create mode 100644 rust/prism/rustfmt.toml diff --git a/.github/workflows/rust-bindings.yml b/.github/workflows/rust-bindings.yml index 610f1909975..89677e94be4 100644 --- a/.github/workflows/rust-bindings.yml +++ b/.github/workflows/rust-bindings.yml @@ -50,7 +50,7 @@ jobs: working-directory: rust/${{ matrix.crate }} run: cargo test - cargo-clippy: + lint: strategy: fail-fast: false matrix: @@ -83,6 +83,9 @@ jobs: - name: cargo clippy working-directory: rust/${{ matrix.crate }} run: cargo clippy --tests -- -W "clippy::pedantic" + - name: cargo fmt + working-directory: rust/${{ matrix.crate }} + run: cargo fmt --check sanitizer-test: name: Test with -Zsanitizer=${{ matrix.sanitizer }} diff --git a/rust/prism-sys/build.rs b/rust/prism-sys/build.rs index beb8b36d418..1a4e9394062 100644 --- a/rust/prism-sys/build.rs +++ b/rust/prism-sys/build.rs @@ -8,10 +8,7 @@ fn main() { println!("cargo:rustc-link-lib=static=rubyparser"); // Add `[root]/build/` to the search paths, so it can find `librubyparser.a`. - println!( - "cargo:rustc-link-search=native={}", - ruby_build_path.to_str().unwrap() - ); + println!("cargo:rustc-link-search=native={}", ruby_build_path.to_str().unwrap()); // This is where the magic happens. let bindings = generate_bindings(&ruby_include_path); @@ -23,20 +20,14 @@ fn main() { /// Gets the path to project files (`librubyparser*`) at `[root]/build/`. /// fn ruby_build_path() -> PathBuf { - cargo_manifest_path() - .join("../../build/") - .canonicalize() - .unwrap() + cargo_manifest_path().join("../../build/").canonicalize().unwrap() } /// Gets the path to the header files that `bindgen` needs for doing code /// generation. /// fn ruby_include_path() -> PathBuf { - cargo_manifest_path() - .join("../../include/") - .canonicalize() - .unwrap() + cargo_manifest_path().join("../../include/").canonicalize().unwrap() } fn cargo_manifest_path() -> PathBuf { @@ -112,7 +103,5 @@ fn generate_bindings(ruby_include_path: &Path) -> bindgen::Bindings { fn write_bindings(bindings: &bindgen::Bindings) { let out_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); - bindings - .write_to_file(out_path.join("bindings.rs")) - .expect("Couldn't write bindings!"); + bindings.write_to_file(out_path.join("bindings.rs")).expect("Couldn't write bindings!"); } diff --git a/rust/prism-sys/rustfmt.toml b/rust/prism-sys/rustfmt.toml new file mode 100644 index 00000000000..9b630818e21 --- /dev/null +++ b/rust/prism-sys/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 300 +match_block_trailing_comma = true diff --git a/rust/prism-sys/tests/node_tests.rs b/rust/prism-sys/tests/node_tests.rs index ad679fc55c3..8aa59214ee8 100644 --- a/rust/prism-sys/tests/node_tests.rs +++ b/rust/prism-sys/tests/node_tests.rs @@ -1,7 +1,7 @@ use std::{ffi::CString, mem::MaybeUninit}; -use prism_sys::{pm_parser_t, pm_parser_init, pm_parse, pm_parser_free}; -use prism_sys::{pm_node_type, pm_node_destroy}; +use prism_sys::{pm_node_destroy, pm_node_type}; +use prism_sys::{pm_parse, pm_parser_free, pm_parser_init, pm_parser_t}; #[test] fn node_test() { @@ -9,12 +9,7 @@ fn node_test() { let code = CString::new("class Foo; end").unwrap(); unsafe { - pm_parser_init( - parser.as_mut_ptr(), - code.as_ptr().cast::(), - code.as_bytes().len(), - std::ptr::null(), - ); + pm_parser_init(parser.as_mut_ptr(), code.as_ptr().cast::(), code.as_bytes().len(), std::ptr::null()); let parser = parser.assume_init_mut(); let parsed_node = pm_parse(parser); diff --git a/rust/prism-sys/tests/pack_tests.rs b/rust/prism-sys/tests/pack_tests.rs index 25936e66a82..f063f8e2552 100644 --- a/rust/prism-sys/tests/pack_tests.rs +++ b/rust/prism-sys/tests/pack_tests.rs @@ -1,9 +1,6 @@ use std::{ffi::CString, mem::MaybeUninit}; -use prism_sys::{ - pm_pack_encoding, pm_pack_endian, pm_pack_length_type, pm_pack_parse, pm_pack_result, - pm_pack_signed, pm_pack_size, pm_pack_type, pm_pack_variant, pm_size_to_native, -}; +use prism_sys::{pm_pack_encoding, pm_pack_endian, pm_pack_length_type, pm_pack_parse, pm_pack_result, pm_pack_signed, pm_pack_size, pm_pack_type, pm_pack_variant, pm_size_to_native}; #[test] fn pack_parse_test() { diff --git a/rust/prism-sys/tests/parser_tests.rs b/rust/prism-sys/tests/parser_tests.rs index 1b81a1bdd02..354ccdf9d79 100644 --- a/rust/prism-sys/tests/parser_tests.rs +++ b/rust/prism-sys/tests/parser_tests.rs @@ -4,11 +4,7 @@ use std::{ path::Path, }; -use prism_sys::{ - pm_comment_t, pm_comment_type_t, pm_diagnostic_t, - pm_node_destroy, pm_parse, pm_parser_free, pm_parser_init, - pm_parser_t, -}; +use prism_sys::{pm_comment_t, pm_comment_type_t, pm_diagnostic_t, pm_node_destroy, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t}; fn ruby_file_contents() -> (CString, usize) { let rust_path = Path::new(env!("CARGO_MANIFEST_DIR")); @@ -39,12 +35,7 @@ fn comments_test() { let mut parser = MaybeUninit::::uninit(); unsafe { - pm_parser_init( - parser.as_mut_ptr(), - source.as_ptr().cast::(), - source.as_bytes().len(), - std::ptr::null(), - ); + pm_parser_init(parser.as_mut_ptr(), source.as_ptr().cast::(), source.as_bytes().len(), std::ptr::null()); let parser = parser.assume_init_mut(); let node = pm_parse(parser); @@ -70,12 +61,7 @@ fn diagnostics_test() { let mut parser = MaybeUninit::::uninit(); unsafe { - pm_parser_init( - parser.as_mut_ptr(), - source.as_ptr().cast::(), - source.as_bytes().len(), - std::ptr::null(), - ); + pm_parser_init(parser.as_mut_ptr(), source.as_ptr().cast::(), source.as_bytes().len(), std::ptr::null()); let parser = parser.assume_init_mut(); let node = pm_parse(parser); @@ -84,10 +70,7 @@ fn diagnostics_test() { let error = error_list.head as *const pm_diagnostic_t; let message = CStr::from_ptr((*error).message); - assert_eq!( - message.to_string_lossy(), - "Cannot parse the expression" - ); + assert_eq!(message.to_string_lossy(), "Cannot parse the expression"); let location = { let start = (*error).start.offset_from(parser.start); diff --git a/rust/prism-sys/tests/utils_tests.rs b/rust/prism-sys/tests/utils_tests.rs index 79d89cad9c5..bf2e0c863d7 100644 --- a/rust/prism-sys/tests/utils_tests.rs +++ b/rust/prism-sys/tests/utils_tests.rs @@ -31,11 +31,7 @@ fn list_test() { } mod string { - use prism_sys::{ - pm_string_free, pm_string_length, pm_string_source, pm_string_t, - pm_string_t__bindgen_ty_1, PM_STRING_SHARED, PM_STRING_OWNED, - PM_STRING_CONSTANT, PM_STRING_MAPPED - }; + use prism_sys::{pm_string_free, pm_string_length, pm_string_source, pm_string_t, pm_string_t__bindgen_ty_1, PM_STRING_CONSTANT, PM_STRING_MAPPED, PM_STRING_OWNED, PM_STRING_SHARED}; use super::*; @@ -59,10 +55,7 @@ mod string { length: c_string.as_bytes().len(), }; - S { - c_string, - pm_string, - } + S { c_string, pm_string } } #[test] diff --git a/rust/prism/build.rs b/rust/prism/build.rs index 977adbac695..3295007bdeb 100644 --- a/rust/prism/build.rs +++ b/rust/prism/build.rs @@ -1,7 +1,4 @@ -#![allow( - clippy::too_many_lines, - clippy::uninlined_format_args -)] +#![allow(clippy::too_many_lines, clippy::uninlined_format_args)] use serde::Deserialize; use std::fs::File; @@ -41,7 +38,7 @@ enum NodeFieldType { UInt32, #[serde(rename = "flags")] - Flags + Flags, } #[derive(Debug, Deserialize)] @@ -51,13 +48,13 @@ struct NodeField { #[serde(rename = "type")] field_type: NodeFieldType, - kind: Option + kind: Option, } #[derive(Debug, Deserialize)] struct FlagValue { name: String, - comment: String + comment: String, } #[derive(Debug, Deserialize)] @@ -75,7 +72,7 @@ struct Node { #[serde(default)] fields: Vec, - comment: String + comment: String, } #[derive(Debug, Deserialize)] @@ -100,10 +97,7 @@ fn main() -> Result<(), Box> { /// Gets the path to the config.yml file at `[root]/config.yml`. /// fn config_path() -> PathBuf { - cargo_manifest_path() - .join("../../config.yml") - .canonicalize() - .unwrap() + cargo_manifest_path().join("../../config.yml").canonicalize().unwrap() } /// Gets the path to the root directory at `[root]`. @@ -346,7 +340,7 @@ fn write_node(file: &mut File, flags: &[Flags], node: &Node) -> Result<(), Box Result<(), Box Result<(), Box Result<(), Box self.visit{}(&{} {{ parser: *parser, pointer: *pointer, marker: *marker }}),", node.name, struct_name(&node.name), node.name)?; + writeln!( + file, + " Node::{} {{ parser, pointer, marker }} => self.visit{}(&{} {{ parser: *parser, pointer: *pointer, marker: *marker }}),", + node.name, + struct_name(&node.name), + node.name + )?; } writeln!(file, " }}")?; @@ -445,7 +449,7 @@ fn write_visit(file: &mut File, config: &Config) -> Result<(), Box {} + _ => {}, } } @@ -467,7 +471,9 @@ fn write_bindings(config: &Config) -> Result<(), Box> { let out_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("bindings.rs"); let mut file = std::fs::File::create(&out_path).expect("Unable to create file"); - writeln!(file, r#" + writeln!( + file, + r#" use std::marker::PhantomData; use std::ptr::NonNull; @@ -679,7 +685,8 @@ impl std::fmt::Debug for ConstantList<'_> {{ write!(f, "{{:?}}", self.iter().collect::>()) }} }} -"#)?; +"# + )?; for node in &config.nodes { writeln!(file, "const {}: u16 = pm_node_type::{} as u16;", type_name(&node.name), type_name(&node.name))?; @@ -713,7 +720,9 @@ impl std::fmt::Debug for ConstantList<'_> {{ writeln!(file, "}}")?; writeln!(file)?; - writeln!(file, r#" + writeln!( + file, + r#" impl<'pr> Node<'pr> {{ /// Creates a new node from the given pointer. /// @@ -724,7 +733,8 @@ impl<'pr> Node<'pr> {{ #[allow(clippy::not_unsafe_ptr_arg_deref)] pub(crate) fn new(parser: NonNull, node: *mut pm_node_t) -> Self {{ match unsafe {{ (*node).type_ }} {{ -"#)?; +"# + )?; for node in &config.nodes { writeln!(file, " {} => Self::{} {{ parser, pointer: node.cast::(), marker: PhantomData }},", type_name(&node.name), node.name, struct_name(&node.name))?; diff --git a/rust/prism/rustfmt.toml b/rust/prism/rustfmt.toml new file mode 100644 index 00000000000..e1b84c33426 --- /dev/null +++ b/rust/prism/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 120 +match_block_trailing_comma = true diff --git a/rust/prism/src/lib.rs b/rust/prism/src/lib.rs index 60ee08688f7..2ffc20488d3 100644 --- a/rust/prism/src/lib.rs +++ b/rust/prism/src/lib.rs @@ -2,19 +2,7 @@ //! //! Rustified version of Ruby's prism parser. //! -#![warn( - clippy::all, - clippy::nursery, - clippy::pedantic, - future_incompatible, - missing_docs, - nonstandard_style, - rust_2018_idioms, - trivial_casts, - trivial_numeric_casts, - unreachable_pub, - unused_qualifications -)] +#![warn(clippy::all, clippy::nursery, clippy::pedantic, future_incompatible, missing_docs, nonstandard_style, rust_2018_idioms, trivial_casts, trivial_numeric_casts, unreachable_pub, unused_qualifications)] // Most of the code in this file is generated, so sometimes it generates code // that doesn't follow the clippy rules. We don't want to see those warnings. @@ -25,24 +13,24 @@ mod bindings { include!(concat!(env!("OUT_DIR"), "/bindings.rs")); } -use std::ffi::{CStr, c_char}; +use std::ffi::{c_char, CStr}; use std::marker::PhantomData; use std::mem::MaybeUninit; use std::ptr::NonNull; -use prism_sys::{pm_parser_t, pm_parser_init, pm_parse, pm_parser_free, pm_node_destroy, pm_node_t, pm_comment_t, pm_diagnostic_t}; pub use self::bindings::*; +use prism_sys::{pm_comment_t, pm_diagnostic_t, pm_node_destroy, pm_node_t, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t}; /// A diagnostic message that came back from the parser. #[derive(Debug)] pub struct Diagnostic<'pr> { diagnostic: NonNull, - marker: PhantomData<&'pr pm_diagnostic_t> + marker: PhantomData<&'pr pm_diagnostic_t>, } impl<'pr> Diagnostic<'pr> { /// Returns the message associated with the diagnostic. - /// + /// /// # Panics /// /// Panics if the message is not able to be converted into a `CStr`. @@ -60,7 +48,7 @@ impl<'pr> Diagnostic<'pr> { #[derive(Debug)] pub struct Comment<'pr> { comment: NonNull, - marker: PhantomData<&'pr pm_comment_t> + marker: PhantomData<&'pr pm_comment_t>, } impl<'pr> Comment<'pr> { @@ -81,7 +69,7 @@ impl<'pr> Comment<'pr> { /// can be used to iterate over the diagnostics in the parse result. pub struct Diagnostics<'pr> { diagnostic: *mut pm_diagnostic_t, - marker: PhantomData<&'pr pm_diagnostic_t> + marker: PhantomData<&'pr pm_diagnostic_t>, } impl<'pr> Iterator for Diagnostics<'pr> { @@ -102,7 +90,7 @@ impl<'pr> Iterator for Diagnostics<'pr> { /// to iterate over the comments in the parse result. pub struct Comments<'pr> { comment: *mut pm_comment_t, - marker: PhantomData<&'pr pm_comment_t> + marker: PhantomData<&'pr pm_comment_t>, } impl<'pr> Iterator for Comments<'pr> { @@ -124,7 +112,7 @@ impl<'pr> Iterator for Comments<'pr> { pub struct ParseResult<'pr> { source: &'pr [u8], parser: NonNull, - node: NonNull + node: NonNull, } impl<'pr> ParseResult<'pr> { @@ -137,9 +125,7 @@ impl<'pr> ParseResult<'pr> { /// Returns whether we found a `frozen_string_literal` magic comment with a true value. #[must_use] pub fn frozen_string_literals(&self) -> bool { - unsafe { - (*self.parser.as_ptr()).frozen_string_literal - } + unsafe { (*self.parser.as_ptr()).frozen_string_literal } } /// Returns a slice of the source string that was parsed using the given @@ -160,7 +146,10 @@ impl<'pr> ParseResult<'pr> { pub fn errors(&self) -> Diagnostics<'_> { unsafe { let list = &mut (*self.parser.as_ptr()).error_list; - Diagnostics { diagnostic: list.head.cast::(), marker: PhantomData } + Diagnostics { + diagnostic: list.head.cast::(), + marker: PhantomData, + } } } @@ -170,7 +159,10 @@ impl<'pr> ParseResult<'pr> { pub fn warnings(&self) -> Diagnostics<'_> { unsafe { let list = &mut (*self.parser.as_ptr()).warning_list; - Diagnostics { diagnostic: list.head.cast::(), marker: PhantomData } + Diagnostics { + diagnostic: list.head.cast::(), + marker: PhantomData, + } } } @@ -180,7 +172,10 @@ impl<'pr> ParseResult<'pr> { pub fn comments(&self) -> Comments<'_> { unsafe { let list = &mut (*self.parser.as_ptr()).comment_list; - Comments { comment: list.head.cast::(), marker: PhantomData } + Comments { + comment: list.head.cast::(), + marker: PhantomData, + } } } @@ -202,7 +197,7 @@ impl<'pr> Drop for ParseResult<'pr> { } /// Parses the given source string and returns a parse result. -/// +/// /// # Panics /// /// Panics if the parser fails to initialize. @@ -213,12 +208,7 @@ pub fn parse(source: &[u8]) -> ParseResult<'_> { let uninit = Box::new(MaybeUninit::::uninit()); let uninit = Box::into_raw(uninit); - pm_parser_init( - (*uninit).as_mut_ptr(), - source.as_ptr(), - source.len(), - std::ptr::null(), - ); + pm_parser_init((*uninit).as_mut_ptr(), source.as_ptr(), source.len(), std::ptr::null()); let parser = (*uninit).assume_init_mut(); let parser = NonNull::new_unchecked(parser); @@ -302,22 +292,18 @@ mod tests { #[test] fn visitor_test() { - use super::{ - Visit, - visit_interpolated_regular_expression_node, InterpolatedRegularExpressionNode, - visit_regular_expression_node, RegularExpressionNode, - }; - + use super::{visit_interpolated_regular_expression_node, visit_regular_expression_node, InterpolatedRegularExpressionNode, RegularExpressionNode, Visit}; + struct RegularExpressionVisitor { - count: usize + count: usize, } - + impl Visit<'_> for RegularExpressionVisitor { fn visit_interpolated_regular_expression_node(&mut self, node: &InterpolatedRegularExpressionNode<'_>) { self.count += 1; visit_interpolated_regular_expression_node(self, node); } - + fn visit_regular_expression_node(&mut self, node: &RegularExpressionNode<'_>) { self.count += 1; visit_regular_expression_node(self, node);