diff --git a/wrapcheck/testdata/custom_error_interface/main.go b/wrapcheck/testdata/custom_error_interface/main.go new file mode 100644 index 0000000..b3cef49 --- /dev/null +++ b/wrapcheck/testdata/custom_error_interface/main.go @@ -0,0 +1,90 @@ +package main + +import "ext" + +func main() {} + +// Custom error types returned through an error result are reported. + +func structVar() error { + err := ext.Foo() + return err // want "error returned from external package is unwrapped" +} + +func structCall() error { + return ext.Foo() // want "error returned from external package is unwrapped" +} + +func pointerCall() error { + return ext.Ptr() // want "error returned from external package is unwrapped" +} + +func pointerVar() error { + err := ext.Ptr() + return err // want "error returned from external package is unwrapped" +} + +func tupleCall() (int, error) { + return ext.Bar() // want "error returned from external package is unwrapped" +} + +func tupleVar() (int, error) { + n, err := ext.Bar() + return n, err // want "error returned from external package is unwrapped" +} + +func embeddedInterface() error { + return ext.Coded() // want "error returned from external package is unwrapped" +} + +func anyResult() any { + return ext.Foo() // want "error returned from external package is unwrapped" +} + +func funcLit() ext.CustomErr { + f := func() error { + err := ext.Foo() + return err // want "error returned from external package is unwrapped" + } + _ = f + + return ext.Foo() +} + +// Results which a wrapped error cannot be assigned to are not reported. + +func concreteCall() ext.CustomErr { + return ext.Foo() +} + +func concreteVar() ext.CustomErr { + err := ext.Foo() + return err +} + +func concretePointer() *ext.PtrErr { + return ext.Ptr() +} + +func concreteTupleCall() (int, ext.CustomErr) { + return ext.Bar() +} + +func concreteTupleVar() (int, ext.CustomErr) { + n, err := ext.Bar() + return n, err +} + +func embeddedInterfaceResult() ext.CodedErr { + return ext.Coded() +} + +func funcLitConcrete() error { + f := func() ext.CustomErr { + err := ext.Foo() + return err + } + _ = f + + return nil +} diff --git a/wrapcheck/testdata/custom_error_interface/src/ext/ext.go b/wrapcheck/testdata/custom_error_interface/src/ext/ext.go new file mode 100644 index 0000000..fff04fd --- /dev/null +++ b/wrapcheck/testdata/custom_error_interface/src/ext/ext.go @@ -0,0 +1,34 @@ +package ext + +type CustomErr struct{} + +func (CustomErr) Error() string { + return "custom error" +} + +func Foo() CustomErr { + return CustomErr{} +} + +func Bar() (int, CustomErr) { + return 0, CustomErr{} +} + +type PtrErr struct{} + +func (*PtrErr) Error() string { + return "pointer error" +} + +func Ptr() *PtrErr { + return &PtrErr{} +} + +type CodedErr interface { + error + Code() int +} + +func Coded() CodedErr { + return nil +} diff --git a/wrapcheck/wrapcheck.go b/wrapcheck/wrapcheck.go index 12cd156..38e485b 100644 --- a/wrapcheck/wrapcheck.go +++ b/wrapcheck/wrapcheck.go @@ -12,6 +12,8 @@ import ( "golang.org/x/tools/go/analysis" ) +var errorType = types.Universe.Lookup("error").Type() + var DefaultIgnoreSigs = []string{ ".Errorf(", "errors.New(", @@ -153,7 +155,7 @@ func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) { } // Iterate over the values to be returned looking for errors - for _, expr := range ret.Results { + for idx, expr := range ret.Results { // Check if the return expression is a function call, if it is, we need // to handle it by checking the return params of the function. retFn, ok := expr.(*ast.CallExpr) @@ -173,7 +175,8 @@ func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) { // match an error within multiple return values, for that, the below // tuple check is required. - if isError(pass.TypesInfo.TypeOf(expr)) { + if isError(pass.TypesInfo.TypeOf(expr)) && + isWrappableResult(pass, parents, idx) { reportUnwrapped(pass, retFn, retFn.Pos(), cfg, ignoreSigRegexp, ignoreInterfaceRegexps, ignorePackageGlobs) return true } @@ -191,14 +194,15 @@ func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) { if v == nil { return true } - if isError(v.Type()) { + if isError(v.Type()) && isWrappableResult(pass, parents, i) { reportUnwrapped(pass, retFn, expr.Pos(), cfg, ignoreSigRegexp, ignoreInterfaceRegexps, ignorePackageGlobs) return true } } } - if !isError(pass.TypesInfo.TypeOf(expr)) { + if !isError(pass.TypesInfo.TypeOf(expr)) || + !isWrappableResult(pass, parents, idx) { continue } @@ -446,13 +450,41 @@ func containsMatchGlob(globs []glob.Glob, el string) bool { return false } -// isError returns whether or not the provided type interface is an error +// isError returns whether or not the provided type implements the error +// interface func isError(typ types.Type) bool { if typ == nil { return false } - return typ.String() == "error" + return types.Implements(typ, errorType.Underlying().(*types.Interface)) +} + +// isWrappableResult returns whether the result at index idx of the innermost +// function enclosing the return statement can hold a wrapped error. Wrapping +// produces an error, so the result type must be one that error is assignable +// to. +func isWrappableResult(pass *analysis.Pass, parents []ast.Node, idx int) bool { + for i := len(parents) - 1; i >= 0; i-- { + var typ types.Type + switch fn := parents[i].(type) { + case *ast.FuncLit: + typ = pass.TypesInfo.TypeOf(fn) + case *ast.FuncDecl: + typ = pass.TypesInfo.TypeOf(fn.Name) + default: + continue + } + + sig, ok := typ.(*types.Signature) + if !ok || idx >= sig.Results().Len() { + return false + } + + return types.AssignableTo(errorType, sig.Results().At(idx).Type()) + } + + return false } func isUnresolved(file *ast.File, ident *ast.Ident) bool {