Ruleguard by example: Go version checks

package gorules

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

// Some rules require particular Go features to be available.
// For example, `ioutil` package was deprecated in 1.16.
// We can't suggest `ioutil` alternatives unless the target
// Go version is at least 1.16. It would be annoying to
// get the warning that is not actionable for you. 

func ioutilDeprecated(m dsl.Matcher) {
	// GreaterEqThan performs a "version >= x" check.
	m.Match(`ioutil.ReadAll($r)`).
		Where(m.GoVersion().GreaterEqThan("1.16")).
		Suggest(`io.ReadAll($r)`).
		Report(`ioutil.ReadAll is deprecated, use io.ReadAll instead`)
}
$ ruleguard -rules rules.go main.go 
main.go:9:1: ioutilDeprecated: ioutil.ReadAll is deprecated, use io.ReadAll instead
main.go
package main

import (
	"io"
	"io/ioutil"
)

func example(r io.Reader) {
	ioutil.ReadAll(r)
}

Notes:

To index Next: Deadcode detection Edit this page