Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions wrapcheck/testdata/custom_error_interface/main.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions wrapcheck/testdata/custom_error_interface/src/ext/ext.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 38 additions & 6 deletions wrapcheck/wrapcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"golang.org/x/tools/go/analysis"
)

var errorType = types.Universe.Lookup("error").Type()

var DefaultIgnoreSigs = []string{
".Errorf(",
"errors.New(",
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Loading