mirror of
https://github.com/mgechev/revive.git
synced 2024-11-28 08:49:11 +02:00
parent
baa70eb87c
commit
d4fbc92440
@ -87,15 +87,25 @@ func (w rangeValInClosure) Visit(node ast.Node) ast.Visitor {
|
||||
if !ok {
|
||||
return w
|
||||
}
|
||||
|
||||
if lit.Type == nil {
|
||||
// Not referring to a variable (e.g. struct field name)
|
||||
return w
|
||||
}
|
||||
ast.Inspect(lit.Body, func(n ast.Node) bool {
|
||||
|
||||
var inspector func(n ast.Node) bool
|
||||
inspector = func(n ast.Node) bool {
|
||||
kv, ok := n.(*ast.KeyValueExpr)
|
||||
if ok {
|
||||
// do not check identifiers acting as key in key-value expressions (see issue #637)
|
||||
ast.Inspect(kv.Value, inspector)
|
||||
return false
|
||||
}
|
||||
id, ok := n.(*ast.Ident)
|
||||
if !ok || id.Obj == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, v := range vars {
|
||||
if v.Obj == id.Obj {
|
||||
w.onFailure(lint.Failure{
|
||||
@ -106,6 +116,7 @@ func (w rangeValInClosure) Visit(node ast.Node) ast.Visitor {
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
ast.Inspect(lit.Body, inspector)
|
||||
return w
|
||||
}
|
||||
|
16
testdata/range-val-in-closure.go
vendored
16
testdata/range-val-in-closure.go
vendored
@ -24,14 +24,26 @@ func foo() {
|
||||
|
||||
for i, newg := range groups {
|
||||
go func(newg int) {
|
||||
newg.run(m.opts.Context,i) // MATCH /loop variable i captured by func literal/
|
||||
newg.run(m.opts.Context, i) // MATCH /loop variable i captured by func literal/
|
||||
}(newg)
|
||||
}
|
||||
|
||||
for i, newg := range groups {
|
||||
newg := newg
|
||||
go func() {
|
||||
newg.run(m.opts.Context,i) // MATCH /loop variable i captured by func literal/
|
||||
newg.run(m.opts.Context, i) // MATCH /loop variable i captured by func literal/
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func issue637() {
|
||||
for key := range m {
|
||||
myKey := key
|
||||
go func() {
|
||||
println(t{
|
||||
key: myKey,
|
||||
otherField: (10 + key), // MATCH /loop variable key captured by func literal/
|
||||
})
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user