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`)
}
package main
import (
"os"
"path/filepath"
)
func main() {
println(os.PathSeparator)
println(os.PathListSeparator)
// Good:
println(filepath.Separator)
println(filepath.ListSeparator)
}
Notes:
File.Name
can be used to check the current file nameFile.PkgPath
can be used to check the current file package path