Ruleguard by example: Multi-rule groups

package gorules

import "github.com/quasilyte/go-ruleguard/dsl"

func assignOp(m dsl.Matcher) {
	// This group defines 2 rules.
	//
	// Rules are executed in the order they're described, until
	// the first successfull match.
	//
	// This short-circuit logic allows us to write overlapping rules
	// without conflicts: only one of them will be executed.
	m.Match(`$x = $x + 1`).Report(`can rewrite as $x++`)
	m.Match(`$x = $x + $y`).Report(`can rewrite as $x += $y`)
}
$ ruleguard -c 0 -rules rules.go main.go
main.go:7:2: can rewrite as v1 += v2
7		v1 = v1 + v2
main.go:8:2: can rewrite as v1++
8		v1 = v1 + 1
main.go
package main

func main() {
    v1 := 0
    v2 := 10

    v1 = v1 + v2
    v1 = v1 + 1
}

Notes:

To index Next: Multi-pattern rules Edit this page