File tree Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -87,15 +87,25 @@ func (w rangeValInClosure) Visit(node ast.Node) ast.Visitor {
87
87
if ! ok {
88
88
return w
89
89
}
90
+
90
91
if lit .Type == nil {
91
92
// Not referring to a variable (e.g. struct field name)
92
93
return w
93
94
}
94
- ast .Inspect (lit .Body , func (n ast.Node ) bool {
95
+
96
+ var inspector func (n ast.Node ) bool
97
+ inspector = func (n ast.Node ) bool {
98
+ kv , ok := n .(* ast.KeyValueExpr )
99
+ if ok {
100
+ // do not check identifiers acting as key in key-value expressions (see issue #637)
101
+ ast .Inspect (kv .Value , inspector )
102
+ return false
103
+ }
95
104
id , ok := n .(* ast.Ident )
96
105
if ! ok || id .Obj == nil {
97
106
return true
98
107
}
108
+
99
109
for _ , v := range vars {
100
110
if v .Obj == id .Obj {
101
111
w .onFailure (lint.Failure {
@@ -106,6 +116,7 @@ func (w rangeValInClosure) Visit(node ast.Node) ast.Visitor {
106
116
}
107
117
}
108
118
return true
109
- })
119
+ }
120
+ ast .Inspect (lit .Body , inspector )
110
121
return w
111
122
}
Original file line number Diff line number Diff line change @@ -24,14 +24,26 @@ func foo() {
24
24
25
25
for i , newg := range groups {
26
26
go func (newg int ) {
27
- newg .run (m .opts .Context ,i ) // MATCH /loop variable i captured by func literal/
27
+ newg .run (m .opts .Context , i ) // MATCH /loop variable i captured by func literal/
28
28
}(newg )
29
29
}
30
30
31
31
for i , newg := range groups {
32
32
newg := newg
33
33
go func () {
34
- newg .run (m .opts .Context ,i ) // MATCH /loop variable i captured by func literal/
34
+ newg .run (m .opts .Context , i ) // MATCH /loop variable i captured by func literal/
35
+ }()
36
+ }
37
+ }
38
+
39
+ func issue637 () {
40
+ for key := range m {
41
+ myKey := key
42
+ go func () {
43
+ println (t {
44
+ key : myKey ,
45
+ otherField : (10 + key ), // MATCH /loop variable key captured by func literal/
46
+ })
35
47
}()
36
48
}
37
49
}
You can’t perform that action at this time.
0 commit comments