A static analysis tool for Go that checks for incorrect usage of defer statements, specifically assignments to error-typed variables that are not named return values in deferred functions.
- Detects deferred functions that assign to error variables not declared as named return values.
- Helps avoid the common pitfall of using
deferincorrectly with errors, which can lead to unexpected behavior in error handling.
You can build deferrlint from source and install it locally:
Make sure you have Go and Task installed on your machine.
git clone https://github.com/yourusername/deferrlint.git
cd deferrlint
task installRun deferrlint on your Go codebase:
go run main.go ./...Or, if you built the binary with task install, you can run it directly:
deferrlint ./...You can verify deferrlint is installed correctly by running:
deferrlint ./testdata/src/fail/...package main
import (
"errors"
)
func foo() (err error) {
defer func() {
err = errors.New("foo") // OK: err is a named return value
}()
return err
}
func bar() error {
var err error
defer func() {
err = errors.New("bar") // Not OK: err is not a named return value
}()
return err
}deferrlint will report the assignment in bar as a potential issue.
Pull requests and issues are welcome!
See the LICENSE file for details.