cockroach: github.com/cockroachdb/cockroach/pkg/testutils/lint/passes/returnerrcheck Index | Files

package returnerrcheck

import "github.com/cockroachdb/cockroach/pkg/testutils/lint/passes/returnerrcheck"

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

Package Files

returnerrcheck.go

Constants

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

Doc documents this pass.

Variables

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.

Package returnerrcheck imports 9 packages (graph) and is imported by 1 packages. Updated 2020-08-12. Refresh now. Tools for package owners.