go-hasdefer

command module
v0.0.0-...-52a470f Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2024 License: MIT Imports: 7 Imported by: 0

README

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 _.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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