Linter notany
limits possible types for arguments of any type.
// arg must be string, fmt.Stringer, or AllowedType.
func FuncWithAnyTypeArg(arg any) {
// ...
}
type AllowedType struct{}
func main() {
pkg.FuncWithAnyTypeArg("ok") // ok
pkg.FuncWithAnyTypeArg(time.Now()) // ok because time.Time implements fmt.Stringer
pkg.FuncWithAnyTypeArg(AllowedType{}) // ok
pkg.FuncWithAnyTypeArg(1.0) // <- float64 is not allowed
pkg.FuncWithAnyTypeArg(true) // <- bool is not allowed
}
Build your notany
binary by writing main.go
like below.
package main
import (
"github.com/qawatake/notany"
"golang.org/x/tools/go/analysis/unitchecker"
)
func main() {
unitchecker.Main(
notany.NewAnalyzer(
notany.Target{
PkgPath: "pkg/in/which/target/func/is/defined",
FuncName: "FuncWithAnyTypeArg",
ArgPos: 1,
Allowed: []notany.Allowed{
{
PkgPath: "",
TypeName: "int",
},
{
PkgPath: "fmt",
TypeName: "Stringer",
},
{
PkgPath: "pkg/in/which/allowed/type/is/defined",
TypeName: "AllowedType",
},
},
},
),
)
}
Then, run go vet
with your notany
binary.
go vet -vettool=/path/to/your/notany ./...