Skip to content

Commit 5bb2638

Browse files
committed
Merge branch 'fix-13'
2 parents bc0c835 + 1977096 commit 5bb2638

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

exportloopref.go

+3
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ func (s *Searcher) isVar(loop ast.Node, expr ast.Expr) bool {
298298
}
299299
switch typed := expr.(type) {
300300
case (*ast.Ident):
301+
if typed.Obj == nil {
302+
return false // global var in another file (ref: #13)
303+
}
301304
_, isVar := vars[typed.Obj.Pos()]
302305
return isVar
303306
case (*ast.IndexExpr): // like X[Y], check X

exportloopref_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ func TestDepPointer(t *testing.T) {
4646
testdata := analysistest.TestData()
4747
analysistest.Run(t, testdata, exportloopref.Analyzer, "deeppointer")
4848
}
49+
50+
func TestReRef(t *testing.T) {
51+
testdata := analysistest.TestData()
52+
analysistest.Run(t, testdata, exportloopref.Analyzer, "reref")
53+
}

testdata/src/reref/another_file.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package main
2+
3+
// Moving this map to main.go fixes nil pointer deference
4+
var globalMapInDifferentFile = map[int]*MyStruct{}

testdata/src/reref/issue13.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
type MyStruct struct {
4+
MyStructPtrField *int
5+
}
6+
7+
func main() {
8+
localVal := 0
9+
arr := []MyStruct{{&localVal}}
10+
for _, p := range arr {
11+
t := *p.MyStructPtrField
12+
globalMapInDifferentFile[t] = &p // want "exporting a pointer for the loop variable p"
13+
}

0 commit comments

Comments
 (0)