From a69f418a6a7ca0899424d391370d64995e693839 Mon Sep 17 00:00:00 2001 From: Adam Magued Date: Wed, 14 Oct 2026 15:33:22 +0300 Subject: [PATCH 1/2] fix: check error interface implementation in isError Signed-off-by: AdamMagued --- wrapcheck/testdata/custom_error_interface/main.go | 8 ++++++++ .../testdata/custom_error_interface/src/ext/ext.go | 11 +++++++++++ wrapcheck/wrapcheck.go | 9 +++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 wrapcheck/testdata/custom_error_interface/main.go create mode 100644 wrapcheck/testdata/custom_error_interface/src/ext/ext.go diff --git a/wrapcheck/testdata/custom_error_interface/main.go b/wrapcheck/testdata/custom_error_interface/main.go new file mode 100644 index 0000000..9cecbcb --- /dev/null +++ b/wrapcheck/testdata/custom_error_interface/main.go @@ -0,0 +1,8 @@ +package main + +import "ext" + +func bar() error { + err := ext.Foo() + return err // want "error returned from external package is unwrapped" +} 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..bee6001 --- /dev/null +++ b/wrapcheck/testdata/custom_error_interface/src/ext/ext.go @@ -0,0 +1,11 @@ +package ext + +type CustomErr struct{} + +func (CustomErr) Error() string { + return "custom error" +} + +func Foo() CustomErr { + return CustomErr{} +} diff --git a/wrapcheck/wrapcheck.go b/wrapcheck/wrapcheck.go index 12cd156..3a32342 100644 --- a/wrapcheck/wrapcheck.go +++ b/wrapcheck/wrapcheck.go @@ -446,13 +446,18 @@ 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 is an error or implements the error interface func isError(typ types.Type) bool { if typ == nil { return false } - return typ.String() == "error" + if typ.String() == "error" { + return true + } + + errorInterface := types.Universe.Lookup("error").Type().Underlying().(*types.Interface) + return types.Implements(typ, errorInterface) } func isUnresolved(file *ast.File, ident *ast.Ident) bool { From 0c809244df91a0375ec24a56513a4eba36eef097 Mon Sep 17 00:00:00 2001 From: Tom Arrell Date: Wed, 14 Oct 2026 15:25:14 +0200 Subject: [PATCH 2/2] fix: only report error implementations returned through wrappable results --- .../testdata/custom_error_interface/main.go | 84 ++++++++++++++++++- .../custom_error_interface/src/ext/ext.go | 23 +++++ wrapcheck/wrapcheck.go | 45 ++++++++-- 3 files changed, 142 insertions(+), 10 deletions(-) diff --git a/wrapcheck/testdata/custom_error_interface/main.go b/wrapcheck/testdata/custom_error_interface/main.go index 9cecbcb..b3cef49 100644 --- a/wrapcheck/testdata/custom_error_interface/main.go +++ b/wrapcheck/testdata/custom_error_interface/main.go @@ -2,7 +2,89 @@ package main import "ext" -func bar() error { +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 index bee6001..fff04fd 100644 --- a/wrapcheck/testdata/custom_error_interface/src/ext/ext.go +++ b/wrapcheck/testdata/custom_error_interface/src/ext/ext.go @@ -9,3 +9,26 @@ func (CustomErr) Error() string { 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 3a32342..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,18 +450,41 @@ func containsMatchGlob(globs []glob.Glob, el string) bool { return false } -// isError returns whether or not the provided type is an error or implements the error interface +// isError returns whether or not the provided type implements the error +// interface func isError(typ types.Type) bool { if typ == nil { return false } - if typ.String() == "error" { - return true + 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()) } - errorInterface := types.Universe.Lookup("error").Type().Underlying().(*types.Interface) - return types.Implements(typ, errorInterface) + return false } func isUnresolved(file *ast.File, ident *ast.Ident) bool {