aboutsummaryrefslogtreecommitdiff

deadlint

deadlint lints a Go module for functions no program reaches. It runs the analysis of deadcode twice, on one load of the module: from the main packages alone, and with the tests as well. The build flags that GOFLAGS sets, such as -tags, apply to the load. Code that only its own tests still call is dead all the same, so a function must be reachable from a main package, or be marked as test support with a doc comment line that starts with the marker and gives the reason:

// Helper builds a store for tests.
//
// deadcode:testonly, since the tests of other packages fake with it.
func Helper() *Store {

The marker can go on a package's doc comment instead, for a package that exists for tests, such as a fake. A marked function must still be called by some test, and must not be reachable from a main package, so that a marker can't outlive its reason.

deadcode keeps every method of a type that converts to an interface, and every method of the interface, so it misses methods nothing calls behind one, such as the queries sqlc generates. deadlint also reports each exported method whose name no non-test code selects: every interface method, and every concrete one unless it satisfies an interface of another module, through which that module may call it. The predeclared error counts as one, and so does the error chain that errors.Is, errors.As and errors.Unwrap call through interfaces of their own: Unwrap() error, Unwrap() []error, Is(error) bool and As(any) bool. A method of the same name with another signature is still checked. The methods of an interface in a generated file, one with the standard // Code generated ... DO NOT EDIT. header, are skipped, since they mirror a concrete type's, as sqlc's DBTX mirrors *sql.DB, and a generated file can't carry a marker; a generated type's methods, such as sqlc's queries, are still checked, and deadcode itself skips generated files. Names that the module's text templates (.html, .txt and .tmpl files) select, as in {{.Amount.String}}, count as selected. The marker covers these methods too, and such a method must then be selected by a test.

Install

go get -tool evantann.com/deadlint@latest

Use

Run it from the module's root. It takes no flags or arguments:

go tool deadlint

Each problem is printed to stderr, and the command exits 1 when there are any.

Makefile

test-dead:
    go tool deadlint
.PHONY: test-dead

License

GNU Affero General Public License v3.0 or later. See LICENSE.