go-rust-patterns.md 3.2 KB

Go and Rust Patterns

Complete pattern library for ast-grep in Go and Rust.

Go Patterns

Function Declarations

# Find function declarations
sg -p 'func $NAME($$$) $_ { $$$ }' --lang go

# Find functions without return type
sg -p 'func $NAME($$$) { $$$ }' --lang go

# Find method declarations
sg -p 'func ($_ $_) $NAME($$$) $_ { $$$ }' --lang go

# Find pointer receiver methods
sg -p 'func ($_ *$_) $NAME($$$) $_ { $$$ }' --lang go

Type Definitions

# Find interface definitions
sg -p 'type $NAME interface { $$$ }' --lang go

# Find struct definitions
sg -p 'type $NAME struct { $$$ }' --lang go

# Find type aliases
sg -p 'type $NAME = $_' --lang go

Error Handling

# Find error checks
sg -p 'if err != nil { $$$ }' --lang go

# Find error returns
sg -p 'return $_, err' --lang go

# Find error wrapping
sg -p 'fmt.Errorf($$$)' --lang go

Concurrency

# Find goroutines
sg -p 'go $_' --lang go

# Find defer statements
sg -p 'defer $_' --lang go

# Find channel operations
sg -p '$_ <- $_' --lang go

# Find select statements
sg -p 'select { $$$ }' --lang go

# Find mutex locks
sg -p '$_.Lock()' --lang go
sg -p '$_.Unlock()' --lang go

Common Patterns

# Find make calls
sg -p 'make($_)' --lang go

# Find new calls
sg -p 'new($_)' --lang go

# Find range loops
sg -p 'for $_, $_ := range $_ { $$$ }' --lang go

# Find init functions
sg -p 'func init() { $$$ }' --lang go

Rust Patterns

Function Definitions

# Find function definitions with return type
sg -p 'fn $NAME($$$) -> $_ { $$$ }' --lang rust

# Find function definitions without return
sg -p 'fn $NAME($$$) { $$$ }' --lang rust

# Find async functions
sg -p 'async fn $NAME($$$) -> $_ { $$$ }' --lang rust

# Find public functions
sg -p 'pub fn $NAME($$$) -> $_ { $$$ }' --lang rust

Impl Blocks

# Find impl blocks
sg -p 'impl $_ { $$$ }' --lang rust

# Find trait implementations
sg -p 'impl $_ for $_ { $$$ }' --lang rust

# Find generic impl
sg -p 'impl<$_> $_ { $$$ }' --lang rust

Error Handling

# Find unwrap calls (potential panics)
sg -p '$_.unwrap()' --lang rust

# Find expect calls
sg -p '$_.expect($_)' --lang rust

# Find ? operator
sg -p '$_?' --lang rust

# Find Result types
sg -p 'Result<$_, $_>' --lang rust

# Find Option types
sg -p 'Option<$_>' --lang rust

Match Expressions

# Find match expressions
sg -p 'match $_ { $$$ }' --lang rust

# Find if let patterns
sg -p 'if let $_ = $_ { $$$ }' --lang rust

# Find while let patterns
sg -p 'while let $_ = $_ { $$$ }' --lang rust

Macros and Attributes

# Find derive attributes
sg -p '#[derive($$$)]' --lang rust

# Find macro invocations
sg -p '$_!($$$)' --lang rust

# Find specific macros
sg -p 'println!($$$)' --lang rust
sg -p 'vec![$$$]' --lang rust

Async/Await

# Find .await calls
sg -p '$_.await' --lang rust

# Find tokio::spawn
sg -p 'tokio::spawn($_)' --lang rust

# Find async blocks
sg -p 'async { $$$ }' --lang rust

Smart Pointers

# Find Box usage
sg -p 'Box::new($_)' --lang rust

# Find Rc usage
sg -p 'Rc::new($_)' --lang rust

# Find Arc usage
sg -p 'Arc::new($_)' --lang rust

# Find RefCell
sg -p 'RefCell::new($_)' --lang rust