Ruleguard by example: Multi-pattern rules

package gorules

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

func boolLiteralInExpr(m dsl.Matcher) {
	// The following rule includes 4 patterns: 2 for each operator and bool value.
	// Every pattern in this list is called "an alternative".
	// Alternatives are tried out one by one, until we have a match.
	m.Match(`$x == true`,
		`$x != true`,
		`$x == false`,
		`$x != false`).
		Report(`omit bool literal in expression`)
}
$ ruleguard -c 0 -rules rules.go main.go
main.go:7:10: omit bool literal in expression
7		println(cond == true)
main.go:8:10: omit bool literal in expression
8		println(cond == false)
main.go:9:10: omit bool literal in expression
9		println(cond != true)
main.go:10:10: omit bool literal in expression
10		println(cond != false)
main.go
package main

func main() {
	var cond bool
	var boolVar bool

	println(cond == true)
	println(cond == false)
	println(cond != true)
	println(cond != false)

	// No warnings for these:
	println(cond == boolVar)
	println(cond == boolVar)
	println(cond != boolVar)
	println(cond != boolVar)
}

Notes:

To index Next: Multi-statement patterns Edit this page