Skip to content

Latest commit

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Undeferred

undeferred is a go/analysis-based tool that flags suspicious early bindings of named results in a defer call.

It will flag references to a named return value that is passed to the invocation of a defer statement (line 8 below), and references in the body of a deferred function to variables that shadowed a named return outside that defer (line 15 below).

CI

Example messages

Given the following source code example.go:

1package example
23import (
4"errors"5 )
67funcfoo() (errerror) {
8defererrors.Join(err)
9returnerrors.New("foo")
10 }
1112funcbar() (errerror) {
13iferr:=errors.New("bar"); err!=nil {
14deferfunc() {
15err=nil16 }()
17 }
18return19 }

undeferred will report the following:

$ undeferred ./....../example.go:8:20: defer captures current value of named result 'err'.../example.go:15:4: defer references shadow of named result 'err'.../example.go:13:5: shadows named result 'err' referenced in later defer

Usage

Run from source with go run github.com/mutility/undeferred@latest or install with go install github.com/mutility/undeferred@latest and run undeferred from GOPATH/bin.

You can configure behvior at the command line by passing the flags below, or in library use by setting fields on undefer.Analyzer().

FlagFieldMeaning
-shadowReferencedShadowsReport references to shadowed named results (default: true)

Bug reports and feature contributions

undeferred is developed in spare time, so while bug reports and feature contributions are welcomed, it may take a while for them to be reviewed. If possible, try to find a minimal reproduction before reporting a bug. Bugs that are difficult or impossible to reproduce will likely be closed.

All bug fixes will include tests to help ensure no regression; correspondingly all contributions should include such tests.

Mutility Analyzers

undeferred is part of mutility-analyzers.

About

Find potential misuses of named returns in defer statements

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages