Skip to content

Commit 36ebab6

Browse files
authored
fix: check interface package against ignorePackageGlobs (#36)
1 parent a9d3b53 commit 36ebab6

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignorePackageGlobs:
2+
- encoding/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"time"
6+
)
7+
8+
func main() {
9+
do(&time.Time{})
10+
}
11+
12+
func do(fn json.Unmarshaler) error {
13+
err := fn.UnmarshalJSON([]byte{})
14+
if err != nil {
15+
return err
16+
}
17+
18+
return nil
19+
}

wrapcheck/wrapcheck.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,23 @@ func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) {
234234

235235
// Report unwrapped takes a call expression and an identifier and reports
236236
// if the call is unwrapped.
237-
func reportUnwrapped(pass *analysis.Pass, call *ast.CallExpr, tokenPos token.Pos, cfg WrapcheckConfig, regexpsSig []*regexp.Regexp, regexpsInter []*regexp.Regexp, pkgGlobs []glob.Glob) {
237+
func reportUnwrapped(
238+
pass *analysis.Pass,
239+
call *ast.CallExpr,
240+
tokenPos token.Pos,
241+
cfg WrapcheckConfig,
242+
regexpsSig []*regexp.Regexp,
243+
regexpsInter []*regexp.Regexp,
244+
pkgGlobs []glob.Glob,
245+
) {
246+
238247
sel, ok := call.Fun.(*ast.SelectorExpr)
239248
if !ok {
240249
return
241250
}
242251

243252
// Check for ignored signatures
244253
fnSig := pass.TypesInfo.ObjectOf(sel.Sel).String()
245-
246254
if contains(cfg.IgnoreSigs, fnSig) {
247255
return
248256
} else if containsMatch(regexpsSig, fnSig) {
@@ -253,9 +261,9 @@ func reportUnwrapped(pass *analysis.Pass, call *ast.CallExpr, tokenPos token.Pos
253261
// errors returned from interface types should be wrapped, unless ignored
254262
// as per `ignoreInterfaceRegexps`
255263
if isInterface(pass, sel) {
264+
pkgPath := pass.TypesInfo.ObjectOf(sel.Sel).Pkg().Path()
256265
name := types.TypeString(pass.TypesInfo.TypeOf(sel.X), func(p *types.Package) string { return p.Name() })
257-
if containsMatch(regexpsInter, name) {
258-
} else {
266+
if !containsMatch(regexpsInter, name) && !containsMatchGlob(pkgGlobs, pkgPath) {
259267
pass.Reportf(tokenPos, "error returned from interface method should be wrapped: sig: %s", fnSig)
260268
return
261269
}
@@ -318,6 +326,7 @@ func prevErrAssign(pass *analysis.Pass, file *ast.File, returnIdent *ast.Ident)
318326
if !isError(pass.TypesInfo.TypeOf(expr)) {
319327
continue
320328
}
329+
321330
if assIdent, ok := expr.(*ast.Ident); ok {
322331
if assIdent.Obj == nil || returnIdent.Obj == nil {
323332
// If we can't find the Obj for one of the identifiers, just skip
@@ -341,6 +350,7 @@ func prevErrAssign(pass *analysis.Pass, file *ast.File, returnIdent *ast.Ident)
341350
if ass.Pos() > returnIdent.Pos() {
342351
break
343352
}
353+
344354
mostRecentAssign = ass
345355
}
346356

@@ -373,6 +383,7 @@ func containsMatchGlob(globs []glob.Glob, el string) bool {
373383
return true
374384
}
375385
}
386+
376387
return false
377388
}
378389

0 commit comments

Comments
 (0)