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:
parent
b34a6c053e
commit
924e3db3ba
@ -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
|
||||
|
||||
|
@ -67,7 +67,7 @@ var allRules = append([]lint.Rule{
|
||||
&rule.ImportsBlacklistRule{},
|
||||
&rule.FunctionResultsLimitRule{},
|
||||
&rule.MaxPublicStructsRule{},
|
||||
&rule.RangeLoopVarRule{},
|
||||
&rule.RangeValInClosureRule{},
|
||||
}, defaultRules...)
|
||||
|
||||
var allFormatters = []lint.Formatter{
|
||||
|
@ -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/
|
||||
}()
|
||||
}
|
||||
}
|
@ -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{
|
@ -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{})
|
||||
}
|
12
test/range-val-in-closure_test.go
Normal file
12
test/range-val-in-closure_test.go
Normal 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{})
|
||||
}
|
@ -13,4 +13,4 @@
|
||||
[rule.receiver-naming]
|
||||
[rule.indent-error-flow]
|
||||
[rule.empty-block]
|
||||
[rule.range-loop-var]
|
||||
[rule.range-val-in-closure]
|
||||
|
Loading…
x
Reference in New Issue
Block a user