defererr – gopkg.teddywing.com/defererr Index | Files | Directories

package defererr

import "gopkg.teddywing.com/defererr"

Package defererr defines an Analyzer that checks whether an error set in a defer closure is correctly returned.

Analyzer defererr

defererr: report incorrectly returned errors from defer closures.

Errors can be returned from a defer closure by setting a captured error variable within the closure. In order for this to work, the error variable must be declared in the function signature, and must be returned somewhere in the function. This analyzer checks to make sure that captured error variables assigned in defer closures are correctly declared and returned.

For example:

func returnErrorFromDefer() error { // return signature should be '(err error)'
	var err error = nil
	if err != nil {
		return err
	}

	defer func() {
		err = errors.New("defer error")
	}()

	return nil // return should use 'err'
}

Index

Variables

var Analyzer = &analysis.Analyzer{
	Name: "defererr",
	Doc:  "reports issues returning errors from defer",
	Run:  run,
}

Source Files

defererr.go

Directories

PathSynopsis
cmd
cmd/godefererr
Version
v0.0.2 (latest)
Published
Mar 10, 2024
Platform
linux/amd64
Imports
4 packages
Last checked
3 weeks ago

Tools for package owners.