From 5c6445edea1efaa2cac45b2fd16c42441896a11a Mon Sep 17 00:00:00 2001 From: Will Lillis Date: Sun, 6 Oct 2024 17:55:00 -0400 Subject: [PATCH] chore: misc clippy lints --- .github/workflows/ci.yml | 1 + Makefile | 2 +- cli/README.md | 2 +- .../src/build_tables/coincident_tokens.rs | 2 +- cli/generate/src/build_tables/item.rs | 20 ++++++------ .../src/build_tables/item_set_builder.rs | 2 +- .../src/build_tables/minimize_parse_table.rs | 2 +- .../src/build_tables/token_conflicts.rs | 2 +- cli/generate/src/lib.rs | 8 ++--- .../src/prepare_grammar/intern_symbols.rs | 2 +- cli/loader/src/lib.rs | 9 +++--- cli/src/init.rs | 27 ++++++++-------- cli/src/main.rs | 7 ++++- cli/src/parse.rs | 3 +- cli/src/test.rs | 2 +- cli/src/tests/helpers/query_helpers.rs | 4 +-- cli/src/tests/parser_test.rs | 6 ++-- cli/src/tests/query_test.rs | 4 +-- cli/src/tests/tree_test.rs | 4 +-- lib/binding_rust/ffi.rs | 4 +-- lib/binding_rust/lib.rs | 31 ++++++++++--------- lib/binding_rust/wasm_language.rs | 2 +- 22 files changed, 74 insertions(+), 72 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de51faf5..f28b827a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,6 +17,7 @@ jobs: - run: rustup toolchain install stable --profile minimal - run: rustup toolchain install nightly --profile minimal - run: rustup component add --toolchain nightly rustfmt + - run: rustup component add --toolchain nightly clippy - uses: Swatinem/rust-cache@v2 - run: make lint diff --git a/Makefile b/Makefile index 22b878a5..bac4e791 100644 --- a/Makefile +++ b/Makefile @@ -107,7 +107,7 @@ lint: cargo update --workspace --locked --quiet cargo check --workspace --all-targets cargo +nightly fmt --all --check - cargo clippy --workspace --all-targets -- -D warnings + cargo +nightly clippy --workspace --all-targets -- -D warnings format: cargo +nightly fmt --all diff --git a/cli/README.md b/cli/README.md index eb93bcfa..11ea5d80 100644 --- a/cli/README.md +++ b/cli/README.md @@ -7,7 +7,7 @@ [npmjs.com]: https://www.npmjs.org/package/tree-sitter-cli [npmjs.com badge]: https://img.shields.io/npm/v/tree-sitter-cli.svg?color=%23BF4A4A -The Tree-sitter CLI allows you to develop, test, and use Tree-sitter grammars from the command line. It works on MacOS, Linux, and Windows. +The Tree-sitter CLI allows you to develop, test, and use Tree-sitter grammars from the command line. It works on `MacOS`, `Linux`, and `Windows`. ### Installation diff --git a/cli/generate/src/build_tables/coincident_tokens.rs b/cli/generate/src/build_tables/coincident_tokens.rs index 045f0375..d1e40741 100644 --- a/cli/generate/src/build_tables/coincident_tokens.rs +++ b/cli/generate/src/build_tables/coincident_tokens.rs @@ -55,7 +55,7 @@ impl<'a> CoincidentTokenIndex<'a> { } } -impl<'a> fmt::Debug for CoincidentTokenIndex<'a> { +impl fmt::Debug for CoincidentTokenIndex<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "CoincidentTokenIndex {{")?; diff --git a/cli/generate/src/build_tables/item.rs b/cli/generate/src/build_tables/item.rs index ebf03ed3..e20b1a8f 100644 --- a/cli/generate/src/build_tables/item.rs +++ b/cli/generate/src/build_tables/item.rs @@ -180,7 +180,7 @@ impl<'a> ParseItemSet<'a> { } } -impl<'a> fmt::Display for ParseItemDisplay<'a> { +impl fmt::Display for ParseItemDisplay<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { if self.0.is_augmented() { write!(f, "START →")?; @@ -243,7 +243,7 @@ impl<'a> fmt::Display for ParseItemDisplay<'a> { } } -impl<'a> fmt::Display for TokenSetDisplay<'a> { +impl fmt::Display for TokenSetDisplay<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "[")?; for (i, symbol) in self.0.iter().enumerate() { @@ -268,7 +268,7 @@ impl<'a> fmt::Display for TokenSetDisplay<'a> { } } -impl<'a> fmt::Display for ParseItemSetDisplay<'a> { +impl fmt::Display for ParseItemSetDisplay<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { for (item, lookaheads) in &self.0.entries { writeln!( @@ -282,7 +282,7 @@ impl<'a> fmt::Display for ParseItemSetDisplay<'a> { } } -impl<'a> Hash for ParseItem<'a> { +impl Hash for ParseItem<'_> { fn hash(&self, hasher: &mut H) { hasher.write_u32(self.variable_index); hasher.write_u32(self.step_index); @@ -311,7 +311,7 @@ impl<'a> Hash for ParseItem<'a> { } } -impl<'a> PartialEq for ParseItem<'a> { +impl PartialEq for ParseItem<'_> { fn eq(&self, other: &Self) -> bool { if self.variable_index != other.variable_index || self.step_index != other.step_index @@ -348,7 +348,7 @@ impl<'a> PartialEq for ParseItem<'a> { } } -impl<'a> Ord for ParseItem<'a> { +impl Ord for ParseItem<'_> { fn cmp(&self, other: &Self) -> Ordering { self.step_index .cmp(&other.step_index) @@ -388,15 +388,15 @@ impl<'a> Ord for ParseItem<'a> { } } -impl<'a> PartialOrd for ParseItem<'a> { +impl PartialOrd for ParseItem<'_> { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl<'a> Eq for ParseItem<'a> {} +impl Eq for ParseItem<'_> {} -impl<'a> Hash for ParseItemSet<'a> { +impl Hash for ParseItemSet<'_> { fn hash(&self, hasher: &mut H) { hasher.write_usize(self.entries.len()); for (item, lookaheads) in &self.entries { @@ -406,7 +406,7 @@ impl<'a> Hash for ParseItemSet<'a> { } } -impl<'a> Hash for ParseItemSetCore<'a> { +impl Hash for ParseItemSetCore<'_> { fn hash(&self, hasher: &mut H) { hasher.write_usize(self.entries.len()); for item in &self.entries { diff --git a/cli/generate/src/build_tables/item_set_builder.rs b/cli/generate/src/build_tables/item_set_builder.rs index 3ef354a8..aa40dd85 100644 --- a/cli/generate/src/build_tables/item_set_builder.rs +++ b/cli/generate/src/build_tables/item_set_builder.rs @@ -289,7 +289,7 @@ impl<'a> ParseItemSetBuilder<'a> { } } -impl<'a> fmt::Debug for ParseItemSetBuilder<'a> { +impl fmt::Debug for ParseItemSetBuilder<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "ParseItemSetBuilder {{")?; diff --git a/cli/generate/src/build_tables/minimize_parse_table.rs b/cli/generate/src/build_tables/minimize_parse_table.rs index 74f70869..906ebcdd 100644 --- a/cli/generate/src/build_tables/minimize_parse_table.rs +++ b/cli/generate/src/build_tables/minimize_parse_table.rs @@ -44,7 +44,7 @@ struct Minimizer<'a> { simple_aliases: &'a AliasMap, } -impl<'a> Minimizer<'a> { +impl Minimizer<'_> { fn remove_unit_reductions(&mut self) { let mut aliased_symbols = HashSet::new(); for variable in &self.syntax_grammar.variables { diff --git a/cli/generate/src/build_tables/token_conflicts.rs b/cli/generate/src/build_tables/token_conflicts.rs index f79d362f..bacac1b4 100644 --- a/cli/generate/src/build_tables/token_conflicts.rs +++ b/cli/generate/src/build_tables/token_conflicts.rs @@ -145,7 +145,7 @@ impl<'a> TokenConflictMap<'a> { } } -impl<'a> fmt::Debug for TokenConflictMap<'a> { +impl fmt::Debug for TokenConflictMap<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "TokenConflictMap {{")?; diff --git a/cli/generate/src/lib.rs b/cli/generate/src/lib.rs index 9695e821..14f20672 100644 --- a/cli/generate/src/lib.rs +++ b/cli/generate/src/lib.rs @@ -66,16 +66,12 @@ pub fn generate_parser_in_directory( } } - let grammar_path = grammar_path - .map(PathBuf::from) - .unwrap_or_else(|| repo_path.join("grammar.js")); + let grammar_path = grammar_path.map_or_else(|| repo_path.join("grammar.js"), PathBuf::from); // Read the grammar file. let grammar_json = load_grammar_file(&grammar_path, js_runtime)?; - let src_path = out_path - .map(PathBuf::from) - .unwrap_or_else(|| repo_path.join("src")); + let src_path = out_path.map_or_else(|| repo_path.join("src"), PathBuf::from); let header_path = src_path.join("tree_sitter"); // Ensure that the output directories exist. diff --git a/cli/generate/src/prepare_grammar/intern_symbols.rs b/cli/generate/src/prepare_grammar/intern_symbols.rs index 0941676d..0ade04bd 100644 --- a/cli/generate/src/prepare_grammar/intern_symbols.rs +++ b/cli/generate/src/prepare_grammar/intern_symbols.rs @@ -94,7 +94,7 @@ struct Interner<'a> { grammar: &'a InputGrammar, } -impl<'a> Interner<'a> { +impl Interner<'_> { fn intern_rule(&self, rule: &Rule, name: Option<&str>) -> Result { match rule { Rule::Choice(elements) => { diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index 164fd670..7c99a01d 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -68,7 +68,7 @@ impl PathsJSON { } } - fn is_empty(&self) -> bool { + const fn is_empty(&self) -> bool { matches!(self, Self::Empty) } } @@ -141,6 +141,7 @@ pub struct TreeSitterJSON { } impl TreeSitterJSON { + #[must_use] pub fn from_file(path: &Path) -> Option { if let Ok(file) = fs::File::open(path.join("tree-sitter.json")) { Some(serde_json::from_reader(file).ok()?) @@ -149,6 +150,7 @@ impl TreeSitterJSON { } } + #[must_use] pub fn has_multiple_language_configs(&self) -> bool { self.grammars.len() > 1 } @@ -366,7 +368,6 @@ impl<'a> CompileConfig<'a> { } } -unsafe impl Send for Loader {} unsafe impl Sync for Loader {} impl Loader { @@ -1004,7 +1005,7 @@ impl Loader { let mut command = match source { EmccSource::Docker => Command::new("docker"), EmccSource::Podman => Command::new("podman"), - _ => unreachable!(), + EmccSource::Native => unreachable!(), }; command.args(["run", "--rm"]); @@ -1344,7 +1345,7 @@ impl Loader { } } -impl<'a> LanguageConfiguration<'a> { +impl LanguageConfiguration<'_> { #[cfg(feature = "tree-sitter-highlight")] pub fn highlight_config( &self, diff --git a/cli/src/init.rs b/cli/src/init.rs index fc661aab..003dcf5e 100644 --- a/cli/src/init.rs +++ b/cli/src/init.rs @@ -94,6 +94,7 @@ const TEST_BINDING_PY_TEMPLATE: &str = include_str!("./templates/test_binding.py const PACKAGE_SWIFT_TEMPLATE: &str = include_str!("./templates/package.swift"); const TESTS_SWIFT_TEMPLATE: &str = include_str!("./templates/tests.swift"); +#[must_use] pub fn path_in_ignore(repo_path: &Path) -> bool { [ "bindings", @@ -130,6 +131,7 @@ pub struct JsonConfigOpts { } impl JsonConfigOpts { + #[must_use] pub fn to_tree_sitter_json(self) -> TreeSitterJSON { TreeSitterJSON { grammars: vec![Grammar { @@ -255,8 +257,7 @@ pub fn migrate_package_json(repo_path: &Path) -> Result { authors: { let authors = old_config .author - .map(|a| vec![a].into_iter()) - .unwrap_or_else(|| vec![].into_iter()) + .map_or_else(|| vec![].into_iter(), |a| vec![a].into_iter()) .chain(old_config.maintainers.unwrap_or_default()) .filter_map(|a| match a { PackageJSONAuthor::String(s) => { @@ -362,7 +363,7 @@ pub fn generate_grammar_files( repo_path: &Path, language_name: &str, _allow_update: bool, - opts: Option, + opts: Option<&JsonConfigOpts>, ) -> Result<()> { let dashed_language_name = language_name.to_kebab_case(); @@ -371,17 +372,15 @@ pub fn generate_grammar_files( true, |path| { // invariant: opts is always Some when `tree-sitter.json` doesn't exist - let Some(opts) = opts.clone() else { - unreachable!() - }; + let Some(opts) = opts else { unreachable!() }; - let tree_sitter_json = opts.to_tree_sitter_json(); + let tree_sitter_json = opts.clone().to_tree_sitter_json(); write_file(path, serde_json::to_string_pretty(&tree_sitter_json)?) }, |path| { // updating the config, if needed - if let Some(opts) = opts.clone() { - let tree_sitter_json = opts.to_tree_sitter_json(); + if let Some(opts) = opts { + let tree_sitter_json = opts.clone().to_tree_sitter_json(); write_file(path, serde_json::to_string_pretty(&tree_sitter_json)?)?; } Ok(()) @@ -674,7 +673,7 @@ pub fn get_root_path(path: &Path) -> Result { } }) .transpose()?; - if let Some(true) = json { + if json == Some(true) { return Ok(pathbuf.parent().unwrap().to_path_buf()); } pathbuf.pop(); // filename @@ -838,7 +837,7 @@ fn generate_file( match generate_opts.description { Some(description) => { - replacement = replacement.replace(PARSER_DESCRIPTION_PLACEHOLDER, description) + replacement = replacement.replace(PARSER_DESCRIPTION_PLACEHOLDER, description); } _ => { replacement = replacement.replace( @@ -847,7 +846,7 @@ fn generate_file( "{} grammar for tree-sitter", language_name.to_upper_camel_case() ), - ) + ); } } @@ -858,7 +857,7 @@ fn generate_file( PARSER_URL_STRIPPED_PLACEHOLDER, &repository.replace("https://", "").to_lowercase(), ) - .replace(PARSER_URL_PLACEHOLDER, &repository.to_lowercase()) + .replace(PARSER_URL_PLACEHOLDER, &repository.to_lowercase()); } _ => { replacement = replacement @@ -875,7 +874,7 @@ fn generate_file( "https://github.com/tree-sitter/tree-sitter-{}", language_name.to_lowercase() ), - ) + ); } } diff --git a/cli/src/main.rs b/cli/src/main.rs index 256a5229..571df08b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -674,7 +674,12 @@ impl Init { (json.grammars[0].name.clone(), None) }; - generate_grammar_files(current_dir, &language_name, self.update, json_config_opts)?; + generate_grammar_files( + current_dir, + &language_name, + self.update, + json_config_opts.as_ref(), + )?; Ok(()) } diff --git a/cli/src/parse.rs b/cli/src/parse.rs index 69fa6387..d72c770c 100644 --- a/cli/src/parse.rs +++ b/cli/src/parse.rs @@ -372,9 +372,8 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul bytes: source_code.len(), duration: Some(duration), }); - } else { - parser.stop_printing_dot_graphs(); } + parser.stop_printing_dot_graphs(); if opts.print_time { let duration = time.elapsed(); diff --git a/cli/src/test.rs b/cli/src/test.rs index 7690bdd3..2502c44c 100644 --- a/cli/src/test.rs +++ b/cli/src/test.rs @@ -338,7 +338,7 @@ fn run_tests( parser: &mut Parser, test_entry: TestEntry, opts: &mut TestOptions, - mut indent_level: i32, + mut indent_level: u32, failures: &mut Vec<(String, String, String)>, corrected_entries: &mut Vec<(String, String, String, String, usize, usize)>, has_parse_errors: &mut bool, diff --git a/cli/src/tests/helpers/query_helpers.rs b/cli/src/tests/helpers/query_helpers.rs index da6e4769..e7e0f969 100644 --- a/cli/src/tests/helpers/query_helpers.rs +++ b/cli/src/tests/helpers/query_helpers.rs @@ -273,13 +273,13 @@ impl std::fmt::Display for Pattern { } } -impl<'a, 'tree> PartialOrd for Match<'a, 'tree> { +impl PartialOrd for Match<'_, '_> { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl<'a, 'tree> Ord for Match<'a, 'tree> { +impl Ord for Match<'_, '_> { // Tree-sitter returns matches in the order that they terminate // during a depth-first walk of the tree. If multiple matches // terminate on the same node, those matches are produced in the diff --git a/cli/src/tests/parser_test.rs b/cli/src/tests/parser_test.rs index d2181999..1bb0240e 100644 --- a/cli/src/tests/parser_test.rs +++ b/cli/src/tests/parser_test.rs @@ -161,7 +161,7 @@ fn test_parsing_with_custom_utf16le_input() { let lines = ["pub fn foo() {", " 1", "}"] .iter() - .map(|s| s.encode_utf16().map(|u| u.to_le()).collect::>()) + .map(|s| s.encode_utf16().map(u16::to_le).collect::>()) .collect::>(); let newline = [('\n' as u16).to_le()]; @@ -267,7 +267,7 @@ fn test_parsing_text_with_byte_order_mark() { .parse_utf16_le( "\u{FEFF}fn a() {}" .encode_utf16() - .map(|u| u.to_le()) + .map(u16::to_le) .collect::>(), None, ) @@ -1134,7 +1134,7 @@ fn test_parsing_utf16_code_with_errors_at_the_end_of_an_included_range() { let source_code = ""; let utf16_source_code = source_code .encode_utf16() - .map(|u| u.to_le()) + .map(u16::to_le) .collect::>(); let start_byte = 2 * source_code.find("a.").unwrap(); diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index b0aa6b2b..121f2a2e 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -3488,10 +3488,10 @@ fn test_query_captures_with_matches_removed() { let mut captures = cursor.captures(&query, tree.root_node(), source.as_bytes()); while let Some((m, i)) = captures.next() { - println!("captured: {:?}, {}", m, i); + println!("captured: {m:?}, {i}"); let capture = m.captures[*i]; let text = capture.node.utf8_text(source.as_bytes()).unwrap(); - println!("captured: {:?}", text); + println!("captured: {text:?}"); if text == "a" { m.remove(); continue; diff --git a/cli/src/tests/tree_test.rs b/cli/src/tests/tree_test.rs index 90898a0c..083955b1 100644 --- a/cli/src/tests/tree_test.rs +++ b/cli/src/tests/tree_test.rs @@ -705,11 +705,11 @@ fn test_consistency_with_mid_codepoint_edit() { #[test] fn test_tree_cursor_on_aliased_root_with_extra_child() { - let source = r#" + let source = r" fn main() { C/* hi */::::E; } -"#; +"; let mut parser = Parser::new(); parser.set_language(&get_language("rust")).unwrap(); diff --git a/lib/binding_rust/ffi.rs b/lib/binding_rust/ffi.rs index 755d2d52..3b3986d2 100644 --- a/lib/binding_rust/ffi.rs +++ b/lib/binding_rust/ffi.rs @@ -85,7 +85,7 @@ impl Tree { } } -impl<'tree> Node<'tree> { +impl Node<'_> { /// Reconstructs a [`Node`] from a raw pointer. /// /// # Safety @@ -103,7 +103,7 @@ impl<'tree> Node<'tree> { } } -impl<'a> TreeCursor<'a> { +impl TreeCursor<'_> { /// Reconstructs a [`TreeCursor`] from a raw pointer. /// /// # Safety diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index eaf6df12..a03102c9 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -438,7 +438,7 @@ impl Drop for Language { } } -impl<'a> Deref for LanguageRef<'a> { +impl Deref for LanguageRef<'_> { type Target = Language; fn deref(&self) -> &Self::Target { @@ -692,7 +692,7 @@ impl Parser { position: ffi::TSPoint, bytes_read: *mut u32, ) -> *const c_char { - let (callback, text) = (payload as *mut (&mut F, Option)).as_mut().unwrap(); + let (callback, text) = payload.cast::<(&mut F, Option)>().as_mut().unwrap(); *text = Some(callback(byte_offset as usize, position.into())); let slice = text.as_ref().unwrap().as_ref(); *bytes_read = slice.len() as u32; @@ -700,7 +700,7 @@ impl Parser { } let c_input = ffi::TSInput { - payload: &mut payload as *mut (&mut F, Option) as *mut c_void, + payload: core::ptr::addr_of_mut!(payload).cast::(), read: Some(read::), encoding: ffi::TSInputEncodingUTF8, }; @@ -737,7 +737,7 @@ impl Parser { /// * `text` The UTF16-encoded text to parse. /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the /// document has changed since `old_tree` was created, then you must edit `old_tree` to match - /// the new text using [Tree::edit]. + /// the new text using [`Tree::edit`]. pub fn parse_utf16_le( &mut self, input: impl AsRef<[u16]>, @@ -760,7 +760,7 @@ impl Parser { /// empty slice. /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the /// document has changed since `old_tree` was created, then you must edit `old_tree` to match - /// the new text using [Tree::edit]. + /// the new text using [`Tree::edit`]. pub fn parse_utf16_le_with, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, @@ -780,7 +780,7 @@ impl Parser { position: ffi::TSPoint, bytes_read: *mut u32, ) -> *const c_char { - let (callback, text) = (payload as *mut (&mut F, Option)).as_mut().unwrap(); + let (callback, text) = payload.cast::<(&mut F, Option)>().as_mut().unwrap(); *text = Some(callback( (byte_offset / 2) as usize, Point { @@ -794,7 +794,7 @@ impl Parser { } let c_input = ffi::TSInput { - payload: &mut payload as *mut (&mut F, Option) as *mut c_void, + payload: core::ptr::addr_of_mut!(payload).cast::(), read: Some(read::), encoding: ffi::TSInputEncodingUTF16LE, }; @@ -812,7 +812,7 @@ impl Parser { /// * `text` The UTF16-encoded text to parse. /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the /// document has changed since `old_tree` was created, then you must edit `old_tree` to match - /// the new text using [Tree::edit]. + /// the new text using [`Tree::edit`]. pub fn parse_utf16_be( &mut self, input: impl AsRef<[u16]>, @@ -835,7 +835,7 @@ impl Parser { /// empty slice. /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the /// document has changed since `old_tree` was created, then you must edit `old_tree` to match - /// the new text using [Tree::edit]. + /// the new text using [`Tree::edit`]. pub fn parse_utf16_be_with, F: FnMut(usize, Point) -> T>( &mut self, callback: &mut F, @@ -855,7 +855,7 @@ impl Parser { position: ffi::TSPoint, bytes_read: *mut u32, ) -> *const c_char { - let (callback, text) = (payload as *mut (&mut F, Option)).as_mut().unwrap(); + let (callback, text) = payload.cast::<(&mut F, Option)>().as_mut().unwrap(); *text = Some(callback( (byte_offset / 2) as usize, Point { @@ -865,10 +865,10 @@ impl Parser { )); let slice = text.as_ref().unwrap().as_ref(); *bytes_read = slice.len() as u32 * 2; - slice.as_ptr() as *const c_char + slice.as_ptr().cast::() } let c_input = ffi::TSInput { - payload: &mut payload as *mut (&mut F, Option) as *mut c_void, + payload: core::ptr::addr_of_mut!(payload).cast::(), read: Some(read::), encoding: ffi::TSInputEncodingUTF16BE, }; @@ -1394,6 +1394,7 @@ impl<'tree> Node<'tree> { } /// Get the field name of this node's named child at the given index. + #[must_use] pub fn field_name_for_named_child(&self, named_child_index: u32) -> Option<&'static str> { unsafe { let ptr = ffi::ts_node_field_name_for_named_child(self.0, named_child_index); @@ -2842,9 +2843,9 @@ impl QueryProperty { } } -/// Provide StreamingIterator instead of traditional one as the underlying object in the C library -/// gets updated on each iteration. Created copies would have their internal state overwritten, -/// leading to Undefined Behavior +/// Provide a `StreamingIterator` instead of the traditional `Iterator`, as the +/// underlying object in the C library gets updated on each iteration. Copies would +/// have their internal state overwritten, leading to Undefined Behavior impl<'query, 'tree: 'query, T: TextProvider, I: AsRef<[u8]>> StreamingIterator for QueryMatches<'query, 'tree, T, I> { diff --git a/lib/binding_rust/wasm_language.rs b/lib/binding_rust/wasm_language.rs index d1ff067b..c7cc0793 100644 --- a/lib/binding_rust/wasm_language.rs +++ b/lib/binding_rust/wasm_language.rs @@ -45,7 +45,7 @@ impl WasmStore { unsafe { let mut error = MaybeUninit::::uninit(); let store = ffi::ts_wasm_store_new( - (engine as *const wasmtime::Engine as *mut wasmtime::Engine).cast(), + (engine as *const wasmtime::Engine).cast_mut().cast(), error.as_mut_ptr(), ); if store.is_null() {