Skip to content

qawatake/notany

Repository files navigation

notany

Go Reference test codecov

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
}

How to use

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 ./...

About

Linter: notany limits possible types for arguments of any type.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published