Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused-param,unused-receiver: fix panic message #1156

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions rule/unused_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,27 @@ type UnusedParamRule struct {
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
// it's more compatible to JSON nature of configurations
var allowedRegexStr string
var allowRegexStr string
if len(args) == 0 {
allowedRegexStr = "^_$"
allowRegexStr = "^_$"
r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it as _"
} else {
// Arguments = [{}]
options := args[0].(map[string]any)
// Arguments = [{allowedRegex="^_"}]
// Arguments = [{allowRegex="^_"}]

if allowedRegexParam, ok := options["allowRegex"]; ok {
allowedRegexStr, ok = allowedRegexParam.(string)
if allowRegexParam, ok := options["allowRegex"]; ok {
allowRegexStr, ok = allowRegexParam.(string)
if !ok {
panic(fmt.Errorf("error configuring %s rule: allowedRegex is not string but [%T]", r.Name(), allowedRegexParam))
panic(fmt.Errorf("error configuring %s rule: allowRegex is not string but [%T]", r.Name(), allowRegexParam))
}
}
}
var err error
r.allowRegex, err = regexp.Compile(allowedRegexStr)
r.allowRegex, err = regexp.Compile(allowRegexStr)
if err != nil {
panic(fmt.Errorf("error configuring %s rule: allowedRegex is not valid regex [%s]: %v", r.Name(), allowedRegexStr, 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()
}
Expand Down
16 changes: 8 additions & 8 deletions rule/unused_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ type UnusedReceiverRule struct {
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
// it's more compatible to JSON nature of configurations
var allowedRegexStr string
var allowRegexStr string
if len(args) == 0 {
allowedRegexStr = "^_$"
allowRegexStr = "^_$"
r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it as _"
} else {
// Arguments = [{}]
options := args[0].(map[string]any)
// Arguments = [{allowedRegex="^_"}]
// Arguments = [{allowRegex="^_"}]

if allowedRegexParam, ok := options["allowRegex"]; ok {
allowedRegexStr, ok = allowedRegexParam.(string)
if allowRegexParam, ok := options["allowRegex"]; ok {
allowRegexStr, ok = allowRegexParam.(string)
if !ok {
panic(fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not string but [%T]", allowedRegexParam))
panic(fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not string but [%T]", allowRegexParam))
denisvmedia marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
var err error
r.allowRegex, err = regexp.Compile(allowedRegexStr)
r.allowRegex, err = regexp.Compile(allowRegexStr)
if err != nil {
panic(fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not valid regex [%s]: %v", allowedRegexStr, 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()
Expand Down
Loading