returnerrcheck

package
v0.0.0-...-1b6ad0c Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package returnerrcheck defines an suite of Analyzers that detects conditionals which check for a non-nil error and then proceed to return a nil error.

Index

Constants

View Source
const Doc = `check for return of nil in a conditional which check for non-nil errors`

Doc documents this pass.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "returnerrcheck",
	Doc:      Doc,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
	Run: func(pass *analysis.Pass) (interface{}, error) {

		inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
		inspect.Preorder([]ast.Node{
			(*ast.IfStmt)(nil),
		}, func(n ast.Node) {
			ifStmt := n.(*ast.IfStmt)

			containing := findContainingFunc(pass, n)
			if !funcReturnsErrorLast(containing) {
				return
			}

			errObj, ok := isNonNilErrCheck(pass, ifStmt.Cond)
			if !ok {
				return
			}
			for i, n := range ifStmt.Body.List {
				returnStmt, ok := n.(*ast.ReturnStmt)
				if !ok {
					continue
				}
				if len(returnStmt.Results) == 0 {
					continue
				}
				lastRes := returnStmt.Results[len(returnStmt.Results)-1]
				if !pass.TypesInfo.Types[lastRes].IsNil() {
					continue
				}
				if hasAcceptableAction(pass, errObj, ifStmt.Body.List[:i+1]) {
					continue
				}
				if hasNoLintComment(pass, ifStmt, returnStmt) {
					continue
				}
				pass.Report(analysis.Diagnostic{
					Pos: n.Pos(),
					Message: fmt.Sprintf("unexpected nil error return after checking for a non-nil error" +
						"; if this is not a mistake, add a \"//nolint:returnerrcheck\" comment"),
				})
			}
		})
		return nil, nil
	},
}

Analyzer is a linter that ensures that returns from functions which return an error as their last return value which have returns inside the body of if conditionals which check for an error to be non-nil do not return a nil error without a `//nolint:returnerrcheck` comment.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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