Skip to content

Commit

Permalink
Merge branch 'fix-13'
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoh86 committed Nov 16, 2022
2 parents bc0c835 + 1977096 commit 5bb2638
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions exportloopref.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ func (s *Searcher) isVar(loop ast.Node, expr ast.Expr) bool {
}
switch typed := expr.(type) {
case (*ast.Ident):
if typed.Obj == nil {
return false // global var in another file (ref: #13)
}
_, isVar := vars[typed.Obj.Pos()]
return isVar
case (*ast.IndexExpr): // like X[Y], check X
Expand Down
5 changes: 5 additions & 0 deletions exportloopref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ func TestDepPointer(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, exportloopref.Analyzer, "deeppointer")
}

func TestReRef(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, exportloopref.Analyzer, "reref")
}
4 changes: 4 additions & 0 deletions testdata/src/reref/another_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// Moving this map to main.go fixes nil pointer deference
var globalMapInDifferentFile = map[int]*MyStruct{}
13 changes: 13 additions & 0 deletions testdata/src/reref/issue13.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

type MyStruct struct {
MyStructPtrField *int
}

func main() {
localVal := 0
arr := []MyStruct{{&localVal}}
for _, p := range arr {
t := *p.MyStructPtrField
globalMapInDifferentFile[t] = &p // want "exporting a pointer for the loop variable p"
}

0 comments on commit 5bb2638

Please sign in to comment.