1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00

New rule: modifies-parameter (#25)

* Adds rule superfluous-else (an extension of indent-error-flow)

* Fix superfluous-else rule struct namming.

* Adds superfuous-else rule to the rules table

* Adds confusing-naming rule

* adds multifile test

* clean-up

* fix config.go

* working version
This commit is contained in:
chavacava 2018-06-24 09:26:21 +02:00 committed by Minko Gechev
parent 7e89359269
commit b2532b3c33
5 changed files with 134 additions and 0 deletions

View File

@ -212,6 +212,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| `superfluous-else` | n/a | Prevents redundant else statements (extends `indent-error-flow`) | no | no |
| `confusing-naming` | n/a | Warns on methods with names that differ only by capitalization | no | no |
| `get-return ` | n/a | Warns on getters that do not yield any result | no | no |
| `modifies-param` | n/a | Warns on assignments to function parameters | no | no |
## Available Formatters

View File

@ -50,6 +50,7 @@ var allRules = append([]lint.Rule{
&rule.EmptyBlockRule{},
&rule.SuperfluousElseRule{},
&rule.GetReturnRule{},
&rule.ModifiesParamRule{},
}, defaultRules...)
var allFormatters = []lint.Formatter{

View File

@ -0,0 +1,26 @@
package fixtures
import (
"go/ast"
)
func one(a int) {
a,b:= 1,2 // MATCH /parameter 'a' seems to be modified/
a++ // MATCH /parameter 'a' seems to be modified/
}
func two(b, c float32) {
if c>0.0 {
b = 1 // MATCH /parameter 'b' seems to be modified/
}
}
type foo struct {
a string
}
func three(s *foo) {
s.a = "foooooo"
}

94
rule/modifies-param.go Normal file
View File

@ -0,0 +1,94 @@
package rule
import (
"fmt"
"go/ast"
"github.com/mgechev/revive/lint"
)
// ModifiesParamRule lints given else constructs.
type ModifiesParamRule struct{}
// Apply applies the rule to given file.
func (r *ModifiesParamRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintModifiesParamRule{onFailure: onFailure}
ast.Walk(w, file.AST)
return failures
}
// Name returns the rule name.
func (r *ModifiesParamRule) Name() string {
return "modifies-parameter"
}
type lintModifiesParamRule struct {
params map[string]bool
onFailure func(lint.Failure)
}
func retrieveParamNames(pl []*ast.Field) map[string]bool {
result := make(map[string]bool, len(pl))
for _, p := range pl {
for _, n := range p.Names {
result[n.Name] = true
}
}
return result
}
func checkAssign(lhs []ast.Expr, w lintModifiesParamRule) {
for _, e := range lhs {
id, ok := e.(*ast.Ident)
if ok {
if w.params[id.Name] {
w.onFailure(lint.Failure{
Confidence: 0.5,
Node: id,
Category: "bad practice",
URL: "#modifies-parameter",
Failure: fmt.Sprintf("parameter '%s' seems to be a modified", id.Name),
})
}
}
}
}
func (w lintModifiesParamRule) Visit(node ast.Node) ast.Visitor {
switch v := node.(type) {
case *ast.FuncDecl:
w.params = retrieveParamNames(v.Type.Params.List)
case *ast.IncDecStmt:
if id, ok := v.X.(*ast.Ident); ok {
checkParam(id, &w)
}
case *ast.AssignStmt:
lhs := v.Lhs
for _, e := range lhs {
id, ok := e.(*ast.Ident)
if ok {
checkParam(id, &w)
}
}
}
return w
}
func checkParam(id *ast.Ident, w *lintModifiesParamRule) {
if w.params[id.Name] {
w.onFailure(lint.Failure{
Confidence: 0.5, // confidence is low because of shadow variables
Node: id,
Category: "bad practice",
URL: "#modifies-parameter",
Failure: fmt.Sprintf("parameter '%s' seems to be modified", id),
})
}
}

View File

@ -0,0 +1,12 @@
package test
import (
"testing"
"github.com/mgechev/revive/rule"
)
// TestModifiesParam rule.
func TestModifiesParam(t *testing.T) {
testRule(t, "modifies-param", &rule.ModifiesParamRule{})
}