1
0
mirror of https://github.com/mgechev/revive.git synced 2025-02-07 13:31:42 +02:00

Rename rule range-loop-var to range-val-in-closure.

This commit is contained in:
xuri 2018-09-29 11:32:32 +08:00
parent b34a6c053e
commit 924e3db3ba
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
7 changed files with 40 additions and 27 deletions

View File

@ -271,7 +271,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| `redefines-builtin-id`| n/a | Warns on redefinitions of builtin identifiers | no | no |
| `function-result-limit` | int | Specifies the maximum number of results a function can return | no | no |
| `imports-blacklist` | []string | Disallows importing the specified packages | no | no |
| `range-loop-var` | n/a | Disallows incorrect uses of range loop variables in closures | no | no |
| `range-val-in-closure`| n/a | Warns if range value is used in a closure dispatched as goroutine| no | no |
## Configurable rules

View File

@ -67,7 +67,7 @@ var allRules = append([]lint.Rule{
&rule.ImportsBlacklistRule{},
&rule.FunctionResultsLimitRule{},
&rule.MaxPublicStructsRule{},
&rule.RangeLoopVarRule{},
&rule.RangeValInClosureRule{},
}, defaultRules...)
var allFormatters = []lint.Formatter{

View File

@ -21,4 +21,17 @@ func foo() {
fmt.Printf("Value: %s\n", value) // MATCH /loop variable value captured by func literal/
}()
}
for i, newg := range groups {
go func(newg int) {
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/
}()
}
}

View File

@ -7,14 +7,14 @@ import (
"github.com/mgechev/revive/lint"
)
// RangeLoopVarRule lints given else constructs.
type RangeLoopVarRule struct{}
// RangeValInClosureRule lints given else constructs.
type RangeValInClosureRule struct{}
// Apply applies the rule to given file.
func (r *RangeLoopVarRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
func (r *RangeValInClosureRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure
walker := RangeLoopVar{
walker := rangeValInClosure{
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
@ -26,15 +26,15 @@ func (r *RangeLoopVarRule) Apply(file *lint.File, arguments lint.Arguments) []li
}
// Name returns the rule name.
func (r *RangeLoopVarRule) Name() string {
return "range-loop-var"
func (r *RangeValInClosureRule) Name() string {
return "range-val-in-closure"
}
type RangeLoopVar struct {
type rangeValInClosure struct {
onFailure func(lint.Failure)
}
func (w RangeLoopVar) Visit(node ast.Node) ast.Visitor {
func (w rangeValInClosure) Visit(node ast.Node) ast.Visitor {
// Find the variables updated by the loop statement.
var vars []*ast.Ident
@ -87,15 +87,15 @@ func (w RangeLoopVar) 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 {
id, ok := n.(*ast.Ident)
if !ok || id.Obj == nil {
return true
}
if lit.Type == nil {
// Not referring to a variable (e.g. struct field name)
return true
}
for _, v := range vars {
if v.Obj == id.Obj {
w.onFailure(lint.Failure{

View File

@ -1,12 +0,0 @@
package test
import (
"testing"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
func TestRangeLoopVar(t *testing.T) {
testRule(t, "range-loop-var", &rule.RangeLoopVarRule{}, &lint.RuleConfig{})
}

View File

@ -0,0 +1,12 @@
package test
import (
"testing"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
func TestRangeValInClosure(t *testing.T) {
testRule(t, "range-val-in-closure", &rule.RangeValInClosureRule{}, &lint.RuleConfig{})
}

View File

@ -13,4 +13,4 @@
[rule.receiver-naming]
[rule.indent-error-flow]
[rule.empty-block]
[rule.range-loop-var]
[rule.range-val-in-closure]