2019-04-18 19:35:51 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"go/ast"
|
2023-08-11 10:35:08 +05:00
|
|
|
"regexp"
|
2019-04-18 19:35:51 +02:00
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-01 17:44:41 +02:00
|
|
|
// UnusedReceiverRule lints unused receivers in functions.
|
2023-08-11 10:35:08 +05:00
|
|
|
type UnusedReceiverRule struct {
|
|
|
|
|
// regex to check if some name is valid for unused parameter, "^_$" by default
|
|
|
|
|
allowRegex *regexp.Regexp
|
|
|
|
|
failureMsg string
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 21:38:46 +01:00
|
|
|
// Configure validates the rule configuration, and configures the rule accordingly.
|
|
|
|
|
//
|
|
|
|
|
// Configuration implements the [lint.ConfigurableRule] interface.
|
|
|
|
|
func (r *UnusedReceiverRule) Configure(args lint.Arguments) error {
|
2024-12-31 12:33:51 +01:00
|
|
|
// while by default args is an array, it could be good to provide structures inside it by default, not arrays or primitives
|
|
|
|
|
// as it's more compatible to JSON nature of configurations
|
2024-12-04 06:55:19 +02:00
|
|
|
r.allowRegex = allowBlankIdentifierRegex
|
|
|
|
|
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it as _"
|
2023-08-11 10:35:08 +05:00
|
|
|
if len(args) == 0 {
|
2024-12-11 19:35:58 +01:00
|
|
|
return nil
|
2024-12-04 06:55:19 +02:00
|
|
|
}
|
|
|
|
|
// Arguments = [{}]
|
|
|
|
|
options := args[0].(map[string]any)
|
2023-08-11 10:35:08 +05:00
|
|
|
|
2024-12-04 06:55:19 +02:00
|
|
|
allowRegexParam, ok := options["allowRegex"]
|
|
|
|
|
if !ok {
|
2024-12-11 19:35:58 +01:00
|
|
|
return nil
|
2024-12-04 06:55:19 +02:00
|
|
|
}
|
|
|
|
|
// Arguments = [{allowRegex="^_"}]
|
|
|
|
|
allowRegexStr, ok := allowRegexParam.(string)
|
|
|
|
|
if !ok {
|
2025-02-26 23:27:31 +02:00
|
|
|
return fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not string but [%T]", allowRegexParam)
|
2023-08-11 10:35:08 +05:00
|
|
|
}
|
|
|
|
|
var err error
|
2024-12-03 18:08:21 +02:00
|
|
|
r.allowRegex, err = regexp.Compile(allowRegexStr)
|
2023-08-11 10:35:08 +05:00
|
|
|
if err != nil {
|
2024-12-11 19:35:58 +01:00
|
|
|
return fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not valid regex [%s]: %w", allowRegexStr, err)
|
2023-08-11 10:35:08 +05:00
|
|
|
}
|
2024-12-04 06:55:19 +02:00
|
|
|
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it to match " + r.allowRegex.String()
|
2024-12-11 19:35:58 +01:00
|
|
|
return nil
|
2023-08-11 10:35:08 +05:00
|
|
|
}
|
2019-04-18 19:35:51 +02:00
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2024-12-13 21:38:46 +01:00
|
|
|
func (r *UnusedReceiverRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2019-04-18 19:35:51 +02:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
2024-12-05 13:09:31 +01:00
|
|
|
for _, decl := range file.AST.Decls {
|
|
|
|
|
funcDecl, ok := decl.(*ast.FuncDecl)
|
|
|
|
|
isMethod := ok && funcDecl.Recv != nil
|
|
|
|
|
if !isMethod {
|
|
|
|
|
continue
|
2019-04-18 19:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 13:09:31 +01:00
|
|
|
rec := funcDecl.Recv.List[0] // safe to access only the first (unique) element of the list
|
2019-04-18 19:35:51 +02:00
|
|
|
if len(rec.Names) < 1 {
|
2024-12-05 13:09:31 +01:00
|
|
|
continue // the receiver is anonymous: func (aType) Foo(...) ...
|
2019-04-18 19:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recID := rec.Names[0]
|
|
|
|
|
if recID.Name == "_" {
|
2024-12-05 13:09:31 +01:00
|
|
|
continue // the receiver is already named _
|
2019-04-18 19:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 13:09:31 +01:00
|
|
|
if r.allowRegex != nil && r.allowRegex.FindStringIndex(recID.Name) != nil {
|
|
|
|
|
continue
|
2023-08-11 10:35:08 +05:00
|
|
|
}
|
|
|
|
|
|
2019-04-18 19:35:51 +02:00
|
|
|
// inspect the func body looking for references to the receiver id
|
2024-12-05 13:09:31 +01:00
|
|
|
selectReceiverUses := func(n ast.Node) bool {
|
2019-04-18 19:35:51 +02:00
|
|
|
ident, isAnID := n.(*ast.Ident)
|
|
|
|
|
|
|
|
|
|
return isAnID && ident.Obj == recID.Obj
|
|
|
|
|
}
|
2024-12-05 13:09:31 +01:00
|
|
|
receiverUses := pick(funcDecl.Body, selectReceiverUses)
|
2019-04-18 19:35:51 +02:00
|
|
|
|
2024-12-05 13:09:31 +01:00
|
|
|
if len(receiverUses) > 0 {
|
|
|
|
|
continue // the receiver is referenced in the func body
|
2019-04-18 19:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 13:09:31 +01:00
|
|
|
failures = append(failures, lint.Failure{
|
2019-04-18 19:35:51 +02:00
|
|
|
Confidence: 1,
|
|
|
|
|
Node: recID,
|
2025-01-18 13:16:19 +02:00
|
|
|
Category: lint.FailureCategoryBadPractice,
|
2024-12-05 13:09:31 +01:00
|
|
|
Failure: fmt.Sprintf(r.failureMsg, recID.Name),
|
2019-04-18 19:35:51 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 13:09:31 +01:00
|
|
|
return failures
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
|
|
|
|
func (*UnusedReceiverRule) Name() string {
|
|
|
|
|
return "unused-receiver"
|
2019-04-18 19:35:51 +02:00
|
|
|
}
|