diff --git a/README.md b/README.md index 48cd472..8038f5b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/wrapcheck/testdata/config_ignoreLocalModule/.wrapcheck.yaml b/wrapcheck/testdata/config_ignoreLocalModule/.wrapcheck.yaml new file mode 100644 index 0000000..0b55f04 --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule/.wrapcheck.yaml @@ -0,0 +1 @@ +ignoreLocalModule: true diff --git a/wrapcheck/testdata/config_ignoreLocalModule/go.mod b/wrapcheck/testdata/config_ignoreLocalModule/go.mod new file mode 100644 index 0000000..e65d38b --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule/go.mod @@ -0,0 +1,3 @@ +module example.com/ignorelocal + +go 1.24 diff --git a/wrapcheck/testdata/config_ignoreLocalModule/local/local.go b/wrapcheck/testdata/config_ignoreLocalModule/local/local.go new file mode 100644 index 0000000..463427c --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule/local/local.go @@ -0,0 +1,7 @@ +package local + +import "errors" + +func Error() error { + return errors.New("failed") +} diff --git a/wrapcheck/testdata/config_ignoreLocalModule/main.go b/wrapcheck/testdata/config_ignoreLocalModule/main.go new file mode 100644 index 0000000..16c1dd7 --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule/main.go @@ -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 +} diff --git a/wrapcheck/testdata/config_ignoreLocalModule_false/.wrapcheck.yaml b/wrapcheck/testdata/config_ignoreLocalModule_false/.wrapcheck.yaml new file mode 100644 index 0000000..b8902a0 --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule_false/.wrapcheck.yaml @@ -0,0 +1 @@ +ignoreLocalModule: false diff --git a/wrapcheck/testdata/config_ignoreLocalModule_false/go.mod b/wrapcheck/testdata/config_ignoreLocalModule_false/go.mod new file mode 100644 index 0000000..e65d38b --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule_false/go.mod @@ -0,0 +1,3 @@ +module example.com/ignorelocal + +go 1.24 diff --git a/wrapcheck/testdata/config_ignoreLocalModule_false/local/local.go b/wrapcheck/testdata/config_ignoreLocalModule_false/local/local.go new file mode 100644 index 0000000..463427c --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule_false/local/local.go @@ -0,0 +1,7 @@ +package local + +import "errors" + +func Error() error { + return errors.New("failed") +} diff --git a/wrapcheck/testdata/config_ignoreLocalModule_false/main.go b/wrapcheck/testdata/config_ignoreLocalModule_false/main.go new file mode 100644 index 0000000..b9bf230 --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule_false/main.go @@ -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 +} diff --git a/wrapcheck/wrapcheck.go b/wrapcheck/wrapcheck.go index 12cd156..c0a2c7f 100644 --- a/wrapcheck/wrapcheck.go +++ b/wrapcheck/wrapcheck.go @@ -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 { @@ -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) { @@ -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.