ifacecapture

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Loglvl = "info"

	// Captured usages of types implementing interfaces on the ignore list will
	// not be reported.
	InterfacesIgnoreList = arrayFlags{}

	// If not empty, only captured usages of types implementing interfaces on
	// the allow list will not be reported.
	InterfacesAllowList = arrayFlags{}
)

Flags.

View Source
var Analyzer *analysis.Analyzer = &analysis.Analyzer{
	Name: "ifacecapture",
	Doc: "Checks for possibly unintentional captures of variables implementing " +
		"an interface of a parameter in a callback function.",
	Run: run,
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
	Flags:            flag.FlagSet{},
	RunDespiteErrors: false,
	ResultType:       nil,
	FactTypes:        nil,
}
View Source
var ErrNoTypeInfo = fmt.Errorf("no type information available")
View Source
var ErrUnexpectedType = errors.New("unexpected type")

Functions

func IsFunctionCall

func IsFunctionCall(node ast.Node) bool

IsFunctionCall returns true if the given expression is of type `ast.CallExpr`.

func IsFunctionLiteral

func IsFunctionLiteral(node ast.Node) bool

IsFunctionLiteral returns true if the given expression is of type `ast.FuncLit`.

func IsPointerType

func IsPointerType(t types.Type) bool

IsFunctionDeclaration returns true if the given type, when stringified, contains a '*'.

func ReportConcrete

func ReportConcrete(pass *analysis.Pass, capturedCall *CallViaReceiver, paramType ConcreteParamType)

ReportConcrete reports that the receiver of `capturedCall` is the same concrete type as the type of `paramType`.

func ReportInterface

func ReportInterface(pass *analysis.Pass, capturedCall *CallViaReceiver, iParamType InterfaceParamType)

ReportInterface reports that `call` implements the interface `iParamType`.

func ShouldCheckInterface

func ShouldCheckInterface(iface *ast.Ident, allowList, ignoreList []string) bool

ShouldCheckInterface returns true if the given identifier for an interface is in `allowlist`, or not in `ignoreList`.

Types

type CallViaReceiver

type CallViaReceiver struct {

	// The type of the receiver for this call
	ReceivedByType types.Type

	// The chain of Selector expressions that lead to the function. E.g. in
	// a.b.c.foo(), the selectors are [a, b, c].
	Chain []*ast.Ident
	// contains filtered or unexported fields
}

CallViaReceiver represents a call to a function on a receiver, possibly through a chain of Selector expressions.

func NewCallViaReceiver

func NewCallViaReceiver(tinfo *types.Info) CallViaReceiver

func (*CallViaReceiver) ProcessSelExpr

func (c *CallViaReceiver) ProcessSelExpr(expr *ast.SelectorExpr) error

ProcessSelExpr recursively processes a SelectorExpr and adds the chain of Idents to the .Chain field.

func (CallViaReceiver) Receiver

func (c CallViaReceiver) Receiver() *ast.Ident

Receiver returns the receiver for this call, which is the last element in the chain. Will panic if the chain is empty.

func (CallViaReceiver) String

func (c CallViaReceiver) String() string

Formats the CallViaReceiver as a string in the form "a.b.c".

type ConcreteParamType

type ConcreteParamType struct {
	Vars  []*ast.Ident
	Ident *ast.Ident
	Type  types.Type
}

type InterfaceParamType

type InterfaceParamType struct {
	Vars           []*ast.Ident
	InterfaceIdent *ast.Ident
	InterfaceType  *types.Interface
}

type TypeChain

type TypeChain struct {
	Types []*ast.Ident
}

TypeChain traverses a chain of selector expressions like mypkg.MyInterface and collects the idents of the types in the chain.

func NewTypeChain

func NewTypeChain() TypeChain

func (TypeChain) IsInterface

func (t TypeChain) IsInterface() bool

IsInterface returns true if the last element of the chain is an interface.

func (TypeChain) Last

func (t TypeChain) Last() *ast.Ident

Last returns the last element in the chain. Panics if the chain is empty.

func (*TypeChain) ProcessTypeChain

func (t *TypeChain) ProcessTypeChain(expr ast.Expr) error

ProcessTypeChain traverses a chain of selector expressions like mypkg.MyInterface and collects the idents of the types in the chain.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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