package gorules
// We always need this import.
import "github.com/quasilyte/go-ruleguard/dsl"
// helloWorld is a *named rules group*.
// It has only 1 simple rule inside and its name is "helloWorld".
// The *rules group* signature is fixed.
func helloWorld(m dsl.Matcher) {
// A chain that starts with Match() and ends with Report() call called a *rule*.
// Therefore, a minimal *rule* consists of Match+Report call.
//
// Match() matches the AST using the gogrep pattern string;
// Report() prints the specified message when this rule is matched.
m.Match(`fmt.Println("Hello, world")`).Report("found hello world")
}
$ ruleguard -c 0 -rules rules.go main.go main.go:6:2: found hello world 6 fmt.Println("Hello, world")
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
}
Notes:
-c 0
argument makes the ruleguard
print one context line in its output (line 6 in this example)-rules
argument accepts a ruleguard
rules source (always rules.go
in out examples)Match()
argument is a github.com/mvdan/gogrep pattern stringpackage gorules
; it can't be other packagedsl
package documentation is a common source of the most answersTo index | Next: Submatches | Edit this page |