notany

package module
v0.0.0-...-b2f29f1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2023 License: MIT Imports: 9 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer(targets ...Target) *analysis.Analyzer

Types

type Allowed

type Allowed struct {
	// The path of the package that defines the type.
	// If the type is builtin, let it be an empty string.
	PkgPath string
	// The name of the type.
	TypeName string
}

Allowed represents a type that is allowed for the argument.

type Target

type Target struct {
	// Package path of the target function (or method).
	PkgPath string
	// Name of the target function (or method).
	FuncName string
	// Position of argument of type any.
	// ArgPos is 0-indexed.
	ArgPos int
	// List of allowed types for the argument.
	Allowed []Allowed
}

Target represents a pair of a function and a list of arguments with allowed types.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL