mirror of
https://github.com/mgechev/revive.git
synced 2025-06-06 23:16:16 +02:00
unused-param,unused-receiver: refactor configure func (#1157)
This commit is contained in:
parent
7f7d024f11
commit
09fb350a27
@ -9,6 +9,8 @@ import (
|
|||||||
"github.com/mgechev/revive/lint"
|
"github.com/mgechev/revive/lint"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var allowBlankIdentifierRegex = regexp.MustCompile("^_$")
|
||||||
|
|
||||||
// UnusedParamRule lints unused params in functions.
|
// UnusedParamRule lints unused params in functions.
|
||||||
type UnusedParamRule struct {
|
type UnusedParamRule struct {
|
||||||
// regex to check if some name is valid for unused parameter, "^_$" by default
|
// regex to check if some name is valid for unused parameter, "^_$" by default
|
||||||
@ -21,31 +23,30 @@ type UnusedParamRule struct {
|
|||||||
func (r *UnusedParamRule) configure(args lint.Arguments) {
|
func (r *UnusedParamRule) configure(args lint.Arguments) {
|
||||||
// while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives
|
// while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives
|
||||||
// it's more compatible to JSON nature of configurations
|
// it's more compatible to JSON nature of configurations
|
||||||
var allowRegexStr string
|
r.allowRegex = allowBlankIdentifierRegex
|
||||||
if len(args) == 0 {
|
|
||||||
allowRegexStr = "^_$"
|
|
||||||
r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it as _"
|
r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it as _"
|
||||||
} else {
|
if len(args) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
// Arguments = [{}]
|
// Arguments = [{}]
|
||||||
options := args[0].(map[string]any)
|
options := args[0].(map[string]any)
|
||||||
// Arguments = [{allowRegex="^_"}]
|
|
||||||
|
|
||||||
if allowRegexParam, ok := options["allowRegex"]; ok {
|
allowRegexParam, ok := options["allowRegex"]
|
||||||
allowRegexStr, ok = allowRegexParam.(string)
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Arguments = [{allowRegex="^_"}]
|
||||||
|
allowRegexStr, ok := allowRegexParam.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Errorf("error configuring %s rule: allowRegex is not string but [%T]", r.Name(), allowRegexParam))
|
panic(fmt.Errorf("error configuring %s rule: allowRegex is not string but [%T]", r.Name(), allowRegexParam))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
var err error
|
var err error
|
||||||
r.allowRegex, err = regexp.Compile(allowRegexStr)
|
r.allowRegex, err = regexp.Compile(allowRegexStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("error configuring %s rule: allowRegex is not valid regex [%s]: %v", r.Name(), allowRegexStr, err))
|
panic(fmt.Errorf("error configuring %s rule: allowRegex is not valid regex [%s]: %v", r.Name(), allowRegexStr, err))
|
||||||
}
|
}
|
||||||
if r.failureMsg == "" {
|
|
||||||
r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it to match " + r.allowRegex.String()
|
r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it to match " + r.allowRegex.String()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Apply applies the rule to given file.
|
// Apply applies the rule to given file.
|
||||||
func (r *UnusedParamRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
|
func (r *UnusedParamRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
|
||||||
|
@ -21,31 +21,30 @@ type UnusedReceiverRule struct {
|
|||||||
func (r *UnusedReceiverRule) configure(args lint.Arguments) {
|
func (r *UnusedReceiverRule) configure(args lint.Arguments) {
|
||||||
// while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives
|
// while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives
|
||||||
// it's more compatible to JSON nature of configurations
|
// it's more compatible to JSON nature of configurations
|
||||||
var allowRegexStr string
|
r.allowRegex = allowBlankIdentifierRegex
|
||||||
if len(args) == 0 {
|
|
||||||
allowRegexStr = "^_$"
|
|
||||||
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it as _"
|
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it as _"
|
||||||
} else {
|
if len(args) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
// Arguments = [{}]
|
// Arguments = [{}]
|
||||||
options := args[0].(map[string]any)
|
options := args[0].(map[string]any)
|
||||||
// Arguments = [{allowRegex="^_"}]
|
|
||||||
|
|
||||||
if allowRegexParam, ok := options["allowRegex"]; ok {
|
allowRegexParam, ok := options["allowRegex"]
|
||||||
allowRegexStr, ok = allowRegexParam.(string)
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Arguments = [{allowRegex="^_"}]
|
||||||
|
allowRegexStr, ok := allowRegexParam.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not string but [%T]", allowRegexParam))
|
panic(fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not string but [%T]", allowRegexParam))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
var err error
|
var err error
|
||||||
r.allowRegex, err = regexp.Compile(allowRegexStr)
|
r.allowRegex, err = regexp.Compile(allowRegexStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not valid regex [%s]: %v", allowRegexStr, err))
|
panic(fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not valid regex [%s]: %v", allowRegexStr, err))
|
||||||
}
|
}
|
||||||
if r.failureMsg == "" {
|
|
||||||
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it to match " + r.allowRegex.String()
|
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it to match " + r.allowRegex.String()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Apply applies the rule to given file.
|
// Apply applies the rule to given file.
|
||||||
func (r *UnusedReceiverRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
|
func (r *UnusedReceiverRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
|
||||||
|
@ -9,6 +9,10 @@ import (
|
|||||||
|
|
||||||
func TestUnusedParam(t *testing.T) {
|
func TestUnusedParam(t *testing.T) {
|
||||||
testRule(t, "unused_param", &rule.UnusedParamRule{})
|
testRule(t, "unused_param", &rule.UnusedParamRule{})
|
||||||
|
testRule(t, "unused_param", &rule.UnusedParamRule{}, &lint.RuleConfig{Arguments: []any{}})
|
||||||
|
testRule(t, "unused_param", &rule.UnusedParamRule{}, &lint.RuleConfig{Arguments: []any{
|
||||||
|
map[string]any{"a": "^xxx"},
|
||||||
|
}})
|
||||||
testRule(t, "unused_param_custom_regex", &rule.UnusedParamRule{}, &lint.RuleConfig{Arguments: []any{
|
testRule(t, "unused_param_custom_regex", &rule.UnusedParamRule{}, &lint.RuleConfig{Arguments: []any{
|
||||||
map[string]any{"allowRegex": "^xxx"},
|
map[string]any{"allowRegex": "^xxx"},
|
||||||
}})
|
}})
|
||||||
|
@ -9,7 +9,18 @@ import (
|
|||||||
|
|
||||||
func TestUnusedReceiver(t *testing.T) {
|
func TestUnusedReceiver(t *testing.T) {
|
||||||
testRule(t, "unused_receiver", &rule.UnusedReceiverRule{})
|
testRule(t, "unused_receiver", &rule.UnusedReceiverRule{})
|
||||||
|
testRule(t, "unused_receiver", &rule.UnusedReceiverRule{}, &lint.RuleConfig{Arguments: []any{}})
|
||||||
|
testRule(t, "unused_receiver", &rule.UnusedReceiverRule{}, &lint.RuleConfig{Arguments: []any{
|
||||||
|
map[string]any{"a": "^xxx"},
|
||||||
|
}})
|
||||||
testRule(t, "unused_receiver_custom_regex", &rule.UnusedReceiverRule{}, &lint.RuleConfig{Arguments: []any{
|
testRule(t, "unused_receiver_custom_regex", &rule.UnusedReceiverRule{}, &lint.RuleConfig{Arguments: []any{
|
||||||
map[string]any{"allowRegex": "^xxx"},
|
map[string]any{"allowRegex": "^xxx"},
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkUnusedReceiver(b *testing.B) {
|
||||||
|
var t *testing.T
|
||||||
|
for i := 0; i <= b.N; i++ {
|
||||||
|
testRule(t, "unused_receiver", &rule.UnusedReceiverRule{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user