aboutsummaryrefslogtreecommitdiff

typelint

typelint runs four Go linters that need type information: fieldlint, limitlint, handlerlint and ifacelint. Type-checking the module is most of what each costs, so the packages load once, tests included, and each check gets the packages it covers. Every check runs, and the command fails when any does.

fieldlint

fieldlint reports struct fields that are read but never set. Such a field always holds its zero value, so whatever reads it is wrong without failing: a flag that a query never loads from the database reads as false everywhere, and the tests pass because their mocks set it.

A field is set by a keyed struct literal, an unkeyed literal of its type, a conversion to its type, an assignment or ++/-- through a selector, or taking its address, as rows.Scan(&x.Field) does. Setting a field of a field sets the outer one too, at any depth: x.F.G = v and &x.F.G set both F and G. A struct that a decoder may fill by reflection has every field set: one passed by address as an any, as to json.Unmarshal, or that a generic function returning one is instantiated with, as a form decoder's Decode[T] is. Fields with a decoder's struct tag (see -decoder-tags) are skipped for the same reason, as are fields whose zero value is ready to use, such as a sync.Mutex.

Tests never count as setting a field, since a mock that sets one hides the bug. A setting only tests change, such as one that skips background jobs, says so with a fieldlint:ignore comment on the field. A type that no non-test code ever sets a field of is left to deadcode: it's unused rather than half filled in. Test and generated files are skipped.

limitlint

limitlint reports keyed struct literals, tests included, whose type has an integer Limit field that they don't set. A list query with a zero limit returns no rows, so a forgotten Limit fails silently, and mocks that ignore Limit hide it from the tests.

Only named types ending in Params are checked, which is how sqlc names the parameters of a query; a Limit elsewhere, such as a rate limit result, means something else. Unkeyed literals set every field, so only keyed literals are checked. Setting Limit: 0 explicitly is allowed, for tests of the validation that rejects it. Generated files are skipped.

handlerlint

handlerlint reports HTTP handlers that call more than one service method in a request. A handler that calls two has started orchestrating, which belongs in the service, where it can be tested without HTTP.

A handler is a function in the packages -handlers names that returns an http.Handler or http.HandlerFunc. Its services are the parameters of an interface type with a method taking a context.Context. Each call of a method on a service counts, and so does each call that passes a service to a helper, since the helper calls it in turn. Calls add up along the way a request takes, so a dispatcher that calls one service per branch of an if or switch is fine. Middleware, which returns func(http.Handler) http.Handler, is exempt. Test and generated files are skipped.

ifacelint

ifacelint checks that every interface belongs to its consumer. A package declares, unexported, the interface it calls, holding only the methods it calls, and repeats a near-identical one rather than share an exported interface with other packages. It checks the whole module, tests included, and reports:

  • an exported type whose underlying type is an interface, whether declared as one (type Store interface{...}) or defined from another (type Store store);
  • an exported alias of an interface declared in this module, or of an interface literal (type Store = store). An alias of an interface from another module, such as type R = io.Reader, declares nothing new, so it passes;
  • an assertion that a type implements an interface: a blank variable of interface type (var _ Store = (*DB)(nil)), a blank conversion to one (var _ = Store(db) or _ = Store(db)), or a variable of interface type only ever assigned to _ (var s Store = db; _ = s). The wiring in main, where the concrete types meet their consumers, proves it.

Files the build constraints exclude, such as those for another OS, can't be type-checked, so they get the checks that need no types: an exported interface literal and a typed blank variable. The go tool's ignored directories, such as testdata, are skipped.

Unlike the other checks, ifacelint doesn't skip a file for its Code generated ... DO NOT EDIT. header alone: the file must also match a -generated pattern naming where one of your generators writes, so a copied header can't hide an interface. With no patterns, no file is exempt.

Install

go get -tool evantann.com/typelint@latest

Use

Run it from the module's root:

go tool typelint
go tool typelint -handlers internal/http
go tool typelint -generated '*_templ.go' -generated 'internal/repo/*.go'

Flags:

  • -handlers dirs: comma-separated directories, relative to the module's root, whose packages and those below them handlerlint checks. The default, ., checks the whole module.
  • -decoder-tags keys: comma-separated struct tag keys of decoders that set fields by reflection, whose fields fieldlint skips. The default is json,schema,db,xml.
  • -generated pattern: exempts from ifacelint the generated files matching pattern, and may be repeated. Each pattern is a path.Match pattern: one holding a / matches a file's slash-separated path relative to the module's root, and one without matches its base name in any directory, so *_templ.go exempts every file templ writes.

limitlint and ifacelint print their problems to stderr, and handlerlint and fieldlint to stdout. ifacelint prints each as file:line: ..., with paths relative to the module's root. Each check that fails prints a count to stderr, and the command exits 1 naming them.

Makefile

For a module using templ and sqlc:

lint-type:
    go tool typelint \
        -handlers internal/http \
        -generated '*_templ.go' \
        -generated 'internal/repo/*.go'
.PHONY: lint-type

License

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