Ruleguard by example: File predicates

package gorules

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

// The "path/filepath" package forwards path separators so if
// the file already uses filepath-related API it might be
// a good idea to reduce the direct "os" package dependency.
//
// In some cases it helps to remove the "os" package import completely.

func osFilepath(m dsl.Matcher) {
	// The File() method returns an object that can be used
	// to add file-related filters.
	// Here we require a file to import the "path/filepath" package.

	m.Match(`os.PathSeparator`).
		Where(m.File().Imports("path/filepath")).
		Suggest(`filepath.Separator`)

	m.Match(`os.PathListSeparator`).
		Where(m.File().Imports("path/filepath")).
		Suggest(`filepath.ListSeparator`)
}
$ ruleguard -c 0 -rules rules.go main.go
main.go:9:10: suggestion: filepath.Separator
9		println(os.PathSeparator)
main.go:10:10: suggestion: filepath.ListSeparator
10		println(os.PathListSeparator)
main.go
package main

import (
	"os"
	"path/filepath"
)

func main() {
	println(os.PathSeparator)
	println(os.PathListSeparator)

	// Good:
	println(filepath.Separator)
	println(filepath.ListSeparator)
}

Notes:

To index Next: Text filters Edit this page