Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ ignoreInterfaceRegexps:
# ReportInternalErrors determines whether wrapcheck should report errors returned
# from inside the package.
reportInternalErrors: true

# IgnoreLocalModule determines whether wrapcheck should ignore errors returned
# from other packages within the module being analyzed. The package currently
# being analyzed is controlled separately by reportInternalErrors.
ignoreLocalModule: false
```

## Usage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignoreLocalModule: true
3 changes: 3 additions & 0 deletions wrapcheck/testdata/config_ignoreLocalModule/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/ignorelocal

go 1.24
7 changes: 7 additions & 0 deletions wrapcheck/testdata/config_ignoreLocalModule/local/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package local

import "errors"

func Error() error {
return errors.New("failed")
}
15 changes: 15 additions & 0 deletions wrapcheck/testdata/config_ignoreLocalModule/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "example.com/ignorelocal/local"

func main() {
_ = do()
}

func do() error {
if err := local.Error(); err != nil {
return err
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignoreLocalModule: false
3 changes: 3 additions & 0 deletions wrapcheck/testdata/config_ignoreLocalModule_false/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/ignorelocal

go 1.24
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package local

import "errors"

func Error() error {
return errors.New("failed")
}
15 changes: 15 additions & 0 deletions wrapcheck/testdata/config_ignoreLocalModule_false/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "example.com/ignorelocal/local"

func main() {
_ = do()
}

func do() error {
if err := local.Error(); err != nil {
return err // want `error returned from external package is unwrapped`
}

return nil
}
32 changes: 31 additions & 1 deletion wrapcheck/wrapcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ type WrapcheckConfig struct {
// ReportInternalErrors determines whether wrapcheck should report errors returned
// from inside the package.
ReportInternalErrors bool `mapstructure:"reportInternalErrors" yaml:"reportInternalErrors"`

// IgnoreLocalModule determines whether wrapcheck should ignore errors returned
// from other packages within the module being analyzed. The package currently
// being analyzed is controlled separately by ReportInternalErrors. It defaults
// to false.
IgnoreLocalModule bool `mapstructure:"ignoreLocalModule" yaml:"ignoreLocalModule"`
}

func NewDefaultConfig() WrapcheckConfig {
Expand Down Expand Up @@ -298,7 +304,15 @@ func reportUnwrapped(
return
}

fnSig := pass.TypesInfo.ObjectOf(sel.Sel).String()
fn := pass.TypesInfo.ObjectOf(sel.Sel)
fnSig := fn.String()

// Packages in the current module are not external packages. Keep this
// separate from ReportInternalErrors, which controls the package currently
// being analyzed.
if cfg.IgnoreLocalModule && isFromOtherPackageInLocalModule(pass, fn.Pkg()) {
return
}

// Check for ignored signatures
if checkSignature(cfg, regexpsSig, fnSig) {
Expand Down Expand Up @@ -363,6 +377,22 @@ func isFromOtherPkg(pass *analysis.Pass, sel *ast.SelectorExpr, pkgGlobs []glob.
return true
}

// isFromOtherPackageInLocalModule reports whether pkg belongs to the module
// currently being analyzed, but is not the package currently being analyzed.
// Package paths are hierarchical, so the module path must be followed by a
// slash when checking subpackages.
func isFromOtherPackageInLocalModule(pass *analysis.Pass, pkg *types.Package) bool {
if pass.Module == nil || pass.Module.Path == "" || pass.Pkg == nil || pkg == nil {
return false
}

if pkg.Path() == pass.Pkg.Path() {
return false
}

return pkg.Path() == pass.Module.Path || strings.HasPrefix(pkg.Path(), pass.Module.Path+"/")
}

// prevErrAssign traverses the AST of a file looking for the most recent
// assignment to an error declaration which is specified by the returnIdent
// identifier.
Expand Down