Skip to content
Merged
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
24 changes: 18 additions & 6 deletions internal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,35 @@ func run(pass *analysis.Pass) (interface{}, error) {
func reportImported(pass *analysis.Pass, expr ast.Expr, checkRE *regexp.Regexp, prefix string) {
switch x := expr.(type) {
case *ast.SelectorExpr:
if !checkRE.MatchString(x.Sel.Name) {
return
}

selectIdent, ok := x.X.(*ast.Ident)
if !ok {
return
}

var pkgPath string
if selectObj, ok := pass.TypesInfo.Uses[selectIdent]; ok {
if pkg, ok := selectObj.(*types.PkgName); !ok || pkg.Imported() == pass.Pkg {
pkg, ok := selectObj.(*types.PkgName)
if !ok || pkg.Imported() == pass.Pkg {
return
}
pkgPath = pkg.Imported().Path()
}

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

if matches {
pass.Reportf(expr.Pos(), "%s variable %s in other package %s", prefix, x.Sel.Name, selectIdent.Name)
}
case *ast.Ident:
use, ok := pass.TypesInfo.Uses[x].(*types.Var)
if !ok {
Expand Down
4 changes: 4 additions & 0 deletions internal/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func TestAnalyzer(t *testing.T) {
dir: "defaultclient",
pattern: `^(DefaultClient|DefaultTransport)$`,
},
{
dir: "packagepattern",
pattern: `^(net/http\.DefaultClient|DefaultTransport)$`,
},
}

wd, err := os.Getwd()
Expand Down
3 changes: 1 addition & 2 deletions testdata/defaultclient/src/a/a.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package a

import (
"config"
"io"
"net/http"

"config"
)

var DefaultClient = &http.Client{}
Expand Down
10 changes: 3 additions & 7 deletions testdata/defaults/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package a

import (
"b"

"errors"
"io"
)
import cc "c"

import (
cc "c"
"d"
"d/e"
"d/e/f"
"errors"
"io"
)

var st = struct {
Expand Down
19 changes: 19 additions & 0 deletions testdata/packagepattern/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package a

import (
"config"
"io"
"net/http"
)

var DefaultClient = &http.Client{}

func reassignPattern() {
io.EOF = nil

config.DefaultClient = nil
DefaultClient = nil

http.DefaultClient = nil // want "reassigning variable"
http.DefaultTransport = nil // want "reassigning variable"
}
5 changes: 5 additions & 0 deletions testdata/packagepattern/src/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package config

import "net/http"

var DefaultClient = &http.Client{}