2018-09-27 17:21:10 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
2018-09-29 05:32:32 +02:00
|
|
|
// RangeValInClosureRule lints given else constructs.
|
|
|
|
type RangeValInClosureRule struct{}
|
2018-09-27 17:21:10 +02:00
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2018-09-29 07:04:10 +02:00
|
|
|
func (r *RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2018-09-27 17:21:10 +02:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
2018-09-29 05:32:32 +02:00
|
|
|
walker := rangeValInClosure{
|
2018-09-27 17:21:10 +02:00
|
|
|
onFailure: func(failure lint.Failure) {
|
|
|
|
failures = append(failures, failure)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ast.Walk(walker, file.AST)
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2018-09-29 05:32:32 +02:00
|
|
|
func (r *RangeValInClosureRule) Name() string {
|
|
|
|
return "range-val-in-closure"
|
2018-09-27 17:21:10 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 05:32:32 +02:00
|
|
|
type rangeValInClosure struct {
|
2018-09-27 17:21:10 +02:00
|
|
|
onFailure func(lint.Failure)
|
|
|
|
}
|
|
|
|
|
2018-09-29 05:32:32 +02:00
|
|
|
func (w rangeValInClosure) Visit(node ast.Node) ast.Visitor {
|
2018-09-27 17:21:10 +02:00
|
|
|
// Find the variables updated by the loop statement.
|
|
|
|
var vars []*ast.Ident
|
|
|
|
addVar := func(expr ast.Expr) {
|
|
|
|
if id, ok := expr.(*ast.Ident); ok {
|
|
|
|
vars = append(vars, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var body *ast.BlockStmt
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.RangeStmt:
|
|
|
|
body = n.Body
|
|
|
|
addVar(n.Key)
|
|
|
|
addVar(n.Value)
|
|
|
|
case *ast.ForStmt:
|
|
|
|
body = n.Body
|
|
|
|
switch post := n.Post.(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
// e.g. for p = head; p != nil; p = p.next
|
|
|
|
for _, lhs := range post.Lhs {
|
|
|
|
addVar(lhs)
|
|
|
|
}
|
|
|
|
case *ast.IncDecStmt:
|
|
|
|
// e.g. for i := 0; i < n; i++
|
|
|
|
addVar(post.X)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vars == nil {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inspect a go or defer statement
|
|
|
|
// if it's the last one in the loop body.
|
|
|
|
// (We give up if there are following statements,
|
|
|
|
// because it's hard to prove go isn't followed by wait,
|
|
|
|
// or defer by return.)
|
|
|
|
if len(body.List) == 0 {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
var last *ast.CallExpr
|
|
|
|
switch s := body.List[len(body.List)-1].(type) {
|
|
|
|
case *ast.GoStmt:
|
|
|
|
last = s.Call
|
|
|
|
case *ast.DeferStmt:
|
|
|
|
last = s.Call
|
|
|
|
default:
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
lit, ok := last.Fun.(*ast.FuncLit)
|
|
|
|
if !ok {
|
|
|
|
return w
|
|
|
|
}
|
2022-02-15 23:46:24 +02:00
|
|
|
|
2018-09-29 05:32:32 +02:00
|
|
|
if lit.Type == nil {
|
|
|
|
// Not referring to a variable (e.g. struct field name)
|
|
|
|
return w
|
|
|
|
}
|
2022-02-15 23:46:24 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-09-27 17:21:10 +02:00
|
|
|
id, ok := n.(*ast.Ident)
|
|
|
|
if !ok || id.Obj == nil {
|
|
|
|
return true
|
|
|
|
}
|
2022-02-15 23:46:24 +02:00
|
|
|
|
2018-09-27 17:21:10 +02:00
|
|
|
for _, v := range vars {
|
|
|
|
if v.Obj == id.Obj {
|
|
|
|
w.onFailure(lint.Failure{
|
|
|
|
Confidence: 1,
|
|
|
|
Failure: fmt.Sprintf("loop variable %v captured by func literal", id.Name),
|
|
|
|
Node: n,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2022-02-15 23:46:24 +02:00
|
|
|
}
|
|
|
|
ast.Inspect(lit.Body, inspector)
|
2018-09-27 17:21:10 +02:00
|
|
|
return w
|
|
|
|
}
|