Skip to content

Commit 456cbd0

Browse files
var-naming: handle possible panic (#1405)
* var-naming: handle possible panic * Update rule/var_naming.go Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> * fix test * update error message --------- Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
1 parent 87bd73b commit 456cbd0

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

‎rule/var_naming.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ func (r *VarNamingRule) Configure(arguments lint.Arguments) error {
8787
if !ok {
8888
return fmt.Errorf("invalid third argument to the var-naming rule. Expecting extraBadPackageNames of type slice of strings, but %T", v)
8989
}
90-
for _, name := range extraBadPackageNames {
90+
for i, name := range extraBadPackageNames {
9191
if r.extraBadPackageNames == nil {
9292
r.extraBadPackageNames = map[string]struct{}{}
9393
}
94-
r.extraBadPackageNames[strings.ToLower(name.(string))] = struct{}{}
94+
n, ok := name.(string)
95+
if !ok {
96+
return fmt.Errorf("invalid third argument to the var-naming rule: expected element %d of extraBadPackageNames to be a string, but got %v(%T)", i, name, name)
97+
}
98+
r.extraBadPackageNames[strings.ToLower(n)] = struct{}{}
9599
}
96100
}
97101
}

‎rule/var_naming_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ func TestVarNamingRule_Configure(t *testing.T) {
117117
arguments: lint.Arguments{[]any{""}, []any{""}, []any{map[string]any{"extraBadPackageNames": []int{1}}}},
118118
wantErr: errors.New("invalid third argument to the var-naming rule. Expecting extraBadPackageNames of type slice of strings, but []int"),
119119
},
120+
{
121+
name: "invalid third argument value type for extraBadPackageNames",
122+
arguments: lint.Arguments{[]any{""}, []any{""}, []any{map[string]any{"extraBadPackageNames": []any{"helpers", 5}}}},
123+
wantErr: errors.New("invalid third argument to the var-naming rule: expected element 1 of extraBadPackageNames to be a string, but got 5(int)"),
124+
},
120125
}
121126

122127
for _, tt := range tests {

0 commit comments

Comments
 (0)