1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
This commit is contained in:
chavacava 2022-02-15 22:46:24 +01:00 committed by GitHub
parent baa70eb87c
commit d4fbc92440
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -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
}

View File

@ -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/
})
}()
}
}