Ruleguard by example: Hello world

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")
main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, world")
}

Notes:

To index Next: Submatches Edit this page