1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00

new rule: unused-receiver (#119)

* new rule: unused-receiver

* unused-receiver: adds more test cases
This commit is contained in:
SalvadorC
2019-04-18 19:35:51 +02:00
committed by Minko Gechev
parent 4c0c2b62da
commit 00a86ae1fd
6 changed files with 140 additions and 0 deletions

View File

@@ -296,6 +296,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| [`duplicated-imports`](./RULES_DESCRIPTIONS#duplicated-imports) | n/a | Looks for packages that are imported two or more times | no | no | | [`duplicated-imports`](./RULES_DESCRIPTIONS#duplicated-imports) | n/a | Looks for packages that are imported two or more times | no | no |
| [`import-shadowing`](./RULES_DESCRIPTIONS.md#import-shadowing) | n/a | Spots identifiers that shadow an import | no | no | | [`import-shadowing`](./RULES_DESCRIPTIONS.md#import-shadowing) | n/a | Spots identifiers that shadow an import | no | no |
| [`bare-return`](./RULES_DESCRIPTIONS#bare-return) | n/a | Warns on bare returns | no | no | | [`bare-return`](./RULES_DESCRIPTIONS#bare-return) | n/a | Warns on bare returns | no | no |
| [`unused-receiver`](./RULES_DESCRIPTIONS.md#unused-receiver) | n/a | Suggests to rename or remove unused method receivers | no | no |
## Configurable rules ## Configurable rules

View File

@@ -54,6 +54,7 @@ List of all available rules.
- [unnecessary-stmt](#unnecessary-stmt) - [unnecessary-stmt](#unnecessary-stmt)
- [unreachable-code](#unreachable-code) - [unreachable-code](#unreachable-code)
- [unused-parameter](#unused-parameter) - [unused-parameter](#unused-parameter)
- [unused-receiver](#unused-receiver)
- [waitgroup-by-value](#waitgroup-by-value) - [waitgroup-by-value](#waitgroup-by-value)
## add-constant ## add-constant
@@ -451,6 +452,12 @@ _Description_: This rule warns on unused parameters. Functions or methods with u
_Configuration_: N/A _Configuration_: N/A
## unused-receiver
_Description_: This rule warns on unused method receivers. Methods with unused receivers can be a symptom of an unfinished refactoring or a bug.
_Configuration_: N/A
## waitgroup-by-value ## waitgroup-by-value
_Description_: Function parameters that are passed by value, are in fact a copy of the original argument. Passing a copy of a `sync.WaitGroup` is usually not what the developer wants to do. _Description_: Function parameters that are passed by value, are in fact a copy of the original argument. Passing a copy of a `sync.WaitGroup` is usually not what the developer wants to do.

View File

@@ -76,6 +76,7 @@ var allRules = append([]lint.Rule{
&rule.DuplicatedImportsRule{}, &rule.DuplicatedImportsRule{},
&rule.ImportShadowingRule{}, &rule.ImportShadowingRule{},
&rule.BareReturnRule{}, &rule.BareReturnRule{},
&rule.UnusedReceiverRule{},
}, defaultRules...) }, defaultRules...)
var allFormatters = []lint.Formatter{ var allFormatters = []lint.Formatter{

View File

@@ -0,0 +1,43 @@
package fixtures
import (
"fmt"
"github.com/mgechev/revive/lint"
)
func (f *Unix) Name() string { // MATCH /method receiver 'f' is not referenced in method's body, consider removing or renaming it as _/
return "unix"
}
func (f *Unix) Format(failures <-chan lint.Failure, _ lint.RulesConfig) (string, error) { // MATCH /method receiver 'f' is not referenced in method's body, consider removing or renaming it as _/
for failure := range failures {
fmt.Printf("%v: [%s] %s\n", failure.Position.Start, failure.RuleName, failure.Failure)
}
return "", nil
}
func (u *Unix) Foo() (string, error) { // MATCH /method receiver 'u' is not referenced in method's body, consider removing or renaming it as _/
for failure := range failures {
u := 1 // shadowing the receiver
fmt.Printf("%v\n", u)
}
return "", nil
}
func (u *Unix) Foos() (string, error) {
for failure := range failures {
u := 1 // shadowing the receiver
fmt.Printf("%v\n", u)
}
return u, nil // use of the receiver
}
func (u *Unix) Bar() (string, error) {
for failure := range failures {
u.path = nil // modifies the receiver
fmt.Printf("%v\n", u)
}
return "", nil
}

77
rule/unused-receiver.go Normal file
View File

@@ -0,0 +1,77 @@
package rule
import (
"fmt"
"go/ast"
"github.com/mgechev/revive/lint"
)
// UnusedReceiverRule lints unused params in functions.
type UnusedReceiverRule struct{}
// Apply applies the rule to given file.
func (_ *UnusedReceiverRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintUnusedReceiverRule{onFailure: onFailure}
ast.Walk(w, file.AST)
return failures
}
// Name returns the rule name.
func (_ *UnusedReceiverRule) Name() string {
return "unused-receiver"
}
type lintUnusedReceiverRule struct {
onFailure func(lint.Failure)
}
func (w lintUnusedReceiverRule) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.FuncDecl:
if n.Recv == nil {
return nil // skip this func decl, not a method
}
rec := n.Recv.List[0] // safe to access only the first (unique) element of the list
if len(rec.Names) < 1 {
return nil // the receiver is anonymous: func (aType) Foo(...) ...
}
recID := rec.Names[0]
if recID.Name == "_" {
return nil // the receiver is already named _
}
// inspect the func body looking for references to the receiver id
fselect := func(n ast.Node) bool {
ident, isAnID := n.(*ast.Ident)
return isAnID && ident.Obj == recID.Obj
}
refs2recID := pick(n.Body, fselect, nil)
if len(refs2recID) > 0 {
return nil // the receiver is referenced in the func body
}
w.onFailure(lint.Failure{
Confidence: 1,
Node: recID,
Category: "bad practice",
Failure: fmt.Sprintf("method receiver '%s' is not referenced in method's body, consider removing or renaming it as _", recID.Name),
})
return nil // full method body already inspected
}
return w
}

View File

@@ -0,0 +1,11 @@
package test
import (
"testing"
"github.com/mgechev/revive/rule"
)
func TestUnusedReceiver(t *testing.T) {
testRule(t, "unused-receiver", &rule.UnusedReceiverRule{})
}