Skip to content

Commit 361980f

Browse files
authoredDec 5, 2022
Support matching against package-qualified name as well (#12)
1 parent 90e03e0 commit 361980f

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed
 

‎internal/analyzer/analyzer.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,35 @@ func run(pass *analysis.Pass) (interface{}, error) {
4848
func reportImported(pass *analysis.Pass, expr ast.Expr, checkRE *regexp.Regexp, prefix string) {
4949
switch x := expr.(type) {
5050
case *ast.SelectorExpr:
51-
if !checkRE.MatchString(x.Sel.Name) {
52-
return
53-
}
54-
5551
selectIdent, ok := x.X.(*ast.Ident)
5652
if !ok {
5753
return
5854
}
5955

56+
var pkgPath string
6057
if selectObj, ok := pass.TypesInfo.Uses[selectIdent]; ok {
61-
if pkg, ok := selectObj.(*types.PkgName); !ok || pkg.Imported() == pass.Pkg {
58+
pkg, ok := selectObj.(*types.PkgName)
59+
if !ok || pkg.Imported() == pass.Pkg {
6260
return
6361
}
62+
pkgPath = pkg.Imported().Path()
6463
}
6564

66-
pass.Reportf(expr.Pos(), "%s variable %s in other package %s", prefix, x.Sel.Name, selectIdent.Name)
65+
matches := false
66+
if checkRE.MatchString(x.Sel.Name) {
67+
matches = true
68+
}
69+
if !matches {
70+
// Expression may include a package name, so check that too. Support was added later so we check
71+
// just name and qualified name separately for compatibility.
72+
if checkRE.MatchString(pkgPath + "." + x.Sel.Name) {
73+
matches = true
74+
}
75+
}
6776

77+
if matches {
78+
pass.Reportf(expr.Pos(), "%s variable %s in other package %s", prefix, x.Sel.Name, selectIdent.Name)
79+
}
6880
case *ast.Ident:
6981
use, ok := pass.TypesInfo.Uses[x].(*types.Var)
7082
if !ok {

‎internal/analyzer/analyzer_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func TestAnalyzer(t *testing.T) {
2424
dir: "defaultclient",
2525
pattern: `^(DefaultClient|DefaultTransport)$`,
2626
},
27+
{
28+
dir: "packagepattern",
29+
pattern: `^(net/http\.DefaultClient|DefaultTransport)$`,
30+
},
2731
}
2832

2933
wd, err := os.Getwd()

‎testdata/defaultclient/src/a/a.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package a
22

33
import (
4+
"config"
45
"io"
56
"net/http"
6-
7-
"config"
87
)
98

109
var DefaultClient = &http.Client{}

‎testdata/defaults/src/a/a.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ package a
22

33
import (
44
"b"
5-
6-
"errors"
7-
"io"
8-
)
9-
import cc "c"
10-
11-
import (
5+
cc "c"
126
"d"
137
"d/e"
148
"d/e/f"
9+
"errors"
10+
"io"
1511
)
1612

1713
var st = struct {

‎testdata/packagepattern/src/a/a.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package a
2+
3+
import (
4+
"config"
5+
"io"
6+
"net/http"
7+
)
8+
9+
var DefaultClient = &http.Client{}
10+
11+
func reassignPattern() {
12+
io.EOF = nil
13+
14+
config.DefaultClient = nil
15+
DefaultClient = nil
16+
17+
http.DefaultClient = nil // want "reassigning variable"
18+
http.DefaultTransport = nil // want "reassigning variable"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package config
2+
3+
import "net/http"
4+
5+
var DefaultClient = &http.Client{}

0 commit comments

Comments
 (0)
Please sign in to comment.