Ruleguard by example: Matching comments

package gorules

import (

// 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.
		Report(`using unknown go directive "$tag"`)
$ ruleguard -rules rules.go main.go 
main.go:3:1: unknownGoDirective: using unknown go directive "inline"
main.go:10:2: unknownGoDirective: using unknown go directive "nolint"
package main

func f() int { return 10 }

func g() int { return 0 }

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


To index Next: Go version checks Edit this page