Documentation
¶
Overview ¶
Package deadcode finds the source functions no program in a module can reach, and removes the ones that are dead by construction.
Reachability is golang.org/x/tools' deadcode analysis: every main package and every test executable is a root, rapid type analysis computes what they reach, and a function declared in source but reached by none is dead. Functions in generated files and interface marker methods are never reported.
Dead is not the same as removable. An exported function of a library package is that package's contract; a caller outside the module, or one not yet written, may need it, so it is reported and kept. Everything no other package could ever call is removable: an unexported function, any function of a main package, and any function in a _test.go file.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoMains reports patterns that hold no main package and no test, so // nothing is a root and every function would read as dead. ErrNoMains = errors.New("deadcode: the patterns hold no main package or test to reach anything from") // ErrLoad reports packages that do not load or type-check. ErrLoad = errors.New("deadcode: packages do not type-check") // ErrNotFound reports a function Remove cannot find at its position. ErrNotFound = errors.New("deadcode: no function declaration at the recorded position") // ErrProtected reports a removal of this package's own source. ErrProtected = errors.New("deadcode: the remover never edits its own package") )
Functions ¶
Types ¶
type Function ¶
type Function struct {
// Position is where its name is declared.
Position token.Position
// Name is Receiver.Method or Function.
Name string
// Package is its import path.
Package string
// Exported reports an exported name on an exported (or no) receiver.
Exported bool
// InMain reports a function of a main package.
InMain bool
// InTest reports a function declared in a _test.go file.
InTest bool
// Satisfies reports a method its type needs to satisfy an interface
// it is converted to: nothing calls it, yet the build does.
Satisfies bool
}
Function is one source function nothing reaches.
func Find ¶
Find loads patterns, relative to dir, with their tests and returns every source function of the module no main package or test reaches, ordered by file and line.
func Fix ¶
Fix removes every removable dead function under patterns until a pass removes nothing, and returns what it removed. Removals are tried a directory at a time and kept only while that directory's package and its tests still type-check; a directory that breaks is retried one function at a time, and a function whose removal breaks the build is kept, which is how a method an interface needs survives.
func (Function) Protected ¶
Protected reports a function of this package: the remover never edits its own source, whatever the analysis says about it.