Ruleguard by example: Matching comments

package gorules

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

// Match() is great for matching AST nodes; comments are
// usually ignored when we speak about the structural matching.
// But what if you want to check the comments?
//
// MatchComment() method is designed just for that.
// You specify regexp patterns that are applied to all comment nodes.

func unknownGoDirective(m dsl.Matcher) {
	// Named capture groups populate the m map.
	// Here, we use a "tag" as a capture name and use m["tag"]
	// to add some filters. Since it's a normal match var,
	// it can be interpolated into Report() or Suggest() message.
	m.MatchComment(`//go:(?P<tag>\w+)`).
		Where(!m["tag"].Text.Matches(`noescape|noinline|nosplit|norace`)).
		Report(`using unknown go directive "$tag"`)
}
package main

//go:inline
func f() int { return 10 }

//go:noinline
func g() int { return 0 }

func main() {
	//go:nolint unused-call-result
	f()
}

Notes: