Ruleguard by example: Local functions

package gorules

import ""

// Sometimes we find ourselves repeating the parts of Where()
// clause in several rules.
// Other times we use some conditions that may be
// more clear if we would give them a better name.
// In normal Go, we use functions for that.

func exposedMutex(m dsl.Matcher) {
	// isExported reports whether bound matcher var
	// represents exported Go identifier.
	isExported := func(v dsl.Var) bool {
		return v.Text.Matches(`^\p{Lu}`)

	// isExported() is more readable than `^\p{Lu}`
	// regular expression itself.

	m.Match(`type $name struct { $*_; sync.Mutex; $*_ }`).
		Report("do not embed sync.Mutex")

	m.Match(`type $name struct { $*_; sync.RWMutex; $*_ }`).
		Report("don not embed sync.RWMutex")
$ ruleguard -rules rules.go main.go
main.go:5:1: exposedMutex: do not embed sync.Mutex (rules.go:21)
package main

import "sync"

type EmbedsMutex struct {
	key int

type unexportedEmbedsMutex struct {

func main() {}


To index Next: ? Edit this page