feat(coding-agent/discovery): added Go syntax and API discovery rules

- Added eight new Go-specific rules to the discovery package.
- Registered the new Go rules in the default rule source index.
- Covered the new Go AST matching conditions with test cases in `builtin-defaults.test.ts`.
This commit is contained in:
can1357
2026-06-30 09:49:59 +02:00
parent 38250ce88b
commit dcc7a1ce2c
12 changed files with 399 additions and 1 deletions
@@ -0,0 +1,45 @@
---
description: "Use for i := range n instead of the C-style for i := 0; i < n; i++ loop (Go 1.22)"
interruptMode: never
scope: "tool:edit(*.go), tool:write(*.go)"
astCondition:
- "for $I := 0; $I < $N; $I++ { $$$BODY }"
---
Go 1.22 lets `for` range over an integer. A plain counting loop from `0` to `n` with step `1` reads better as `for i := range n` (or `for range n` when the index is unused).
## Avoid
```go
for i := 0; i < n; i++ {
use(i)
}
for i := 0; i < len(s); i++ {
use(s[i])
}
```
## Use
```go
for i := range n {
use(i)
}
// Ranging the slice directly is usually clearer than indexing.
for i := range s {
use(s[i])
}
// Index unused → drop it entirely.
for range n {
tick()
}
```
## When it does not apply
- Non-zero start, step other than `++`, or a descending loop (`for i := n - 1; i >= 0; i--`) — keep the explicit form.
- The body reassigns the loop variable or depends on `i` surviving past the loop.
- Requires Go 1.22+. If the module's `go` directive is older, keep the classic loop.