go-hasdefer

go-hasdefer

A Go linter to check goroutines for defer

github.com/nathants/go-hasdefer

What

A Go linter to check that all goroutines have a defer statement.

Why

It's too easy to miss panics when they happen inside goroutines, since they exit the defer scope of the caller.

Install

go install github.com/nathants/go-hasdefer@latest

Usage

>> go-hasdefer $(find .test/good/ -name '*.go')

>> go-hasdefer $(find .test/bad/ -name '*.go')
missing defer anon func oneliner:        .test/bad/bad_onliner.go:4      go func() {}()
missing defer anon func multiliner:      .test/bad/bad_multiliner.go:4   go func() {
missing defer top level func multiliner: .test/bad/bad_import.go:12 func Foobar() {
missing defer top level func oneliner:   .test/bad/bad_import.go:6 func (d *Data) Foobar2() {}
missing defer named func multiliner:     .test/bad/bad_imported.go:8     Foobar3 := func() {
missing defer top level func multiliner: .test/bad/bad_import.go:8 func (d *Data) Foobar4() {
missing defer named func oneliner:       .test/bad/bad_imported.go:12    Foobar4 := func() {}

Notes

Either of these is valid:

go func() {
    defer func() {}()
}()
go func() {
   // defer func() {}()
}()

The purpose of this linter is to force you to consider what happens if a goroutine panics, not to force you to include empty defers.

This is similar to always considering what to do with an err, even if you decide to assign it to _.