Skip to content
Back to skills

Rust Tree Sitter Queries

ASecurity

Use when a native Tree-sitter query captures nested syntax unexpectedly, a grammar update breaks a pattern, or Rust capture offsets corrupt UTF-8 text.

  • 50,717 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 2, 2026
ai-agentsrustgonodeexpressrefactoringgitdocumentation

Works with

  • cursor
  • cli

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned December 2, 2026

npx -y skills add sickn33/agentic-awesome-skills --skill rust-tree-sitter-queries --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Rust Tree Sitter Queries?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Rust Tree Sitter Queries
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/sickn33-rust-tree-sitter-queries/badge)](https://www.skillsdirectory.com/skills/sickn33-rust-tree-sitter-queries)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: rust-tree-sitter-queries
description: "Use when a native Tree-sitter query captures nested syntax unexpectedly, a grammar update breaks a pattern, or Rust capture offsets corrupt UTF-8 text."
category: development
risk: critical
source: self
source_type: self
date_added: "2026-12-01"
author: ShianMike
tags: [rust, tree-sitter, ast, queries, syntax-analysis, utf-8]
---

# Rust Tree-sitter Queries

## When to Use

- A native Tree-sitter query includes nested declarations that should be outside its scope.
- A syntax indexer or refactoring tool must pair a declaration with its name and byte range.
- A query succeeds on ASCII inputs but extracts the wrong text in a UTF-8 file.

## Prerequisites

- Pin and record both the Tree-sitter runtime and the grammar version. The worked example uses `tree-sitter` 0.27.2 and `tree-sitter-rust` 0.24.2.
- Have the same source bytes available for parsing, predicate evaluation, and capture extraction.
- Include positive and negative fixtures with the grammar contexts you intend to support.

## 1. Inspect the tree and express the context

1. Parse representative source first and examine its node types and fields. Use the grammar's `node-types.json` and parser output to confirm that a field exists for the intended node.
2. Use field constraints for roles such as `name: (identifier)`. Surround the target with the parent pattern when the parent is part of the contract. A function pattern run on the file root can still match anywhere below that root.
3. Compile the query with the exact grammar at setup. Report an invalid node type, field, or capture as a query error. Handle parse recovery separately by checking `root_node().has_error()` or the error status of the region your tool will use.

```scheme
(source_file
  (function_item
    name: (identifier) @name) @definition)
```

## 2. Preserve match and source boundaries

1. Use `QueryCursor::matches` when captures belong together. Resolve captures by name and consume each match before advancing the streaming iterator. In the tested binding use `hit.captures()` and import `StreamingIterator`.
2. Use `node.utf8_text(source.as_bytes())` or slice the matching input by `node.byte_range()`. Byte offsets are different from character counts and from UTF-16 positions used by some clients. Convert at the boundary with the original source available.
3. If narrowing a scan to a range, distinguish intersecting from contained matches. A byte-range restriction can still return a match with captures outside the requested interval. Use a containing range or verify capture ranges explicitly when containment is required.

## 3. Verify before consuming or editing captures

1. Test the same construct at the file level, inside a body, and inside comments and strings. Assert the selected names and their relationship to the owning declaration.
2. Pass UTF-8 and CRLF examples through the same parser and query. Check byte offsets and captured text against the original bytes.
3. If results drive source edits, reject out-of-scope or ambiguous spans and handle overlaps before writing. Reparse and test the resulting source.

## Example

Run the [top-level capture fixture](references/top-level-captures-and-byte-ranges.md). It contrasts an unrestricted query with one matching declarations directly under the file root.

## Limitations

- The query covers file-level Rust functions. Module bodies, methods, macros, name resolution, and language-specific predicate behavior need separate handling.
- Tree-sitter can return a tree for invalid source. A capture alone does not establish that the resulting program compiles.

## Safety and Side Effects

- The fixture parses synthetic source in memory. Cargo writes local build output and a scratch lockfile.
- Query changes can affect downstream indexing and edits. Review the actual matches and spans before changing source files.

## Related Skills and Documentation

- For the ast-grep CLI and YAML codemods, see [ast-grep Codemods](https://github.com/sickn33/agentic-awesome-skills/blob/main/skills/ast-grep-codemods/SKILL.md).
- For Rust project and Cargo setup, see [Systems Programming Rust Project](https://github.com/sickn33/agentic-awesome-skills/blob/main/skills/systems-programming-rust-project/SKILL.md).
- Tree-sitter documents [query syntax](https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html), [the Rust query cursor](https://docs.rs/tree-sitter/latest/tree_sitter/struct.QueryCursor.html), and [the Rust grammar binding](https://docs.rs/tree-sitter-rust/latest/tree_sitter_rust/).

Files in this skill

  • SKILL.md4.5 KB
  • references/top-level-captures-and-byte-ranges.md3.9 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.