Skip to content

Commit

Permalink
code cleanup: replace interface{} with any (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfederowicz authored Sep 24, 2023
1 parent 5ccebe8 commit 36c2ee2
Show file tree
Hide file tree
Showing 36 changed files with 75 additions and 75 deletions.
2 changes: 1 addition & 1 deletion rule/add-constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) {
r.strLitLimit = defaultStrLitLimit
r.whiteList = newWhiteList()
if len(arguments) > 0 {
args, ok := arguments[0].(map[string]interface{})
args, ok := arguments[0].(map[string]any)
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule. Expecting a k,v map, got %T", arguments[0]))
}
Expand Down
2 changes: 1 addition & 1 deletion rule/context-as-argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (w lintContextArguments) Visit(n ast.Node) ast.Visitor {
func getAllowTypesFromArguments(args lint.Arguments) map[string]struct{} {
allowTypesBefore := []string{}
if len(args) >= 1 {
argKV, ok := args[0].(map[string]interface{})
argKV, ok := args[0].(map[string]any)
if !ok {
panic(fmt.Sprintf("Invalid argument to the context-as-argument rule. Expecting a k,v map, got %T", args[0]))
}
Expand Down
2 changes: 1 addition & 1 deletion rule/defer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (*DeferRule) allowFromArgs(args lint.Arguments) map[string]bool {
return allow
}

aa, ok := args[0].([]interface{})
aa, ok := args[0].([]any)
if !ok {
panic(fmt.Sprintf("Invalid argument '%v' for 'defer' rule. Expecting []string, got %T", args[0], args[0]))
}
Expand Down
4 changes: 2 additions & 2 deletions rule/enforce-map-style.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type EnforceMapStyleRule struct {
func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()

if r.configured {
return
}
Expand All @@ -58,7 +58,7 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) {
return
}

enforceMapStyle, ok := arguments[0].(string)
enforceMapStyle, ok := arguments[0].(string)
if !ok {
panic(fmt.Sprintf("Invalid argument '%v' for 'enforce-map-style' rule. Expecting string, got %T", arguments[0], arguments[0]))
}
Expand Down
2 changes: 1 addition & 1 deletion rule/function-length.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (w lintFuncLength) countFuncLitStmts(stmt ast.Expr) int {
return 0
}

func (w lintFuncLength) countBodyListStmts(t interface{}) int {
func (w lintFuncLength) countBodyListStmts(t any) int {
i := reflect.ValueOf(t).Elem().FieldByName(`Body`).Elem().FieldByName(`List`).Interface()
return w.countStmts(i.([]ast.Stmt))
}
6 changes: 3 additions & 3 deletions rule/string-format.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (*StringFormatRule) Name() string {
// ParseArgumentsTest is a public wrapper around w.parseArguments used for testing. Returns the error message provided to panic, or nil if no error was encountered
func (StringFormatRule) ParseArgumentsTest(arguments lint.Arguments) *string {
w := lintStringFormatRule{}
c := make(chan interface{})
c := make(chan any)
// Parse the arguments in a goroutine, defer a recover() call, return the error encountered (or nil if there was no error)
go func() {
defer func() {
Expand Down Expand Up @@ -101,8 +101,8 @@ func (w *lintStringFormatRule) parseArguments(arguments lint.Arguments) {
}
}

func (w lintStringFormatRule) parseArgument(argument interface{}, ruleNum int) (scope stringFormatSubruleScope, regex *regexp.Regexp, negated bool, errorMessage string) {
g, ok := argument.([]interface{}) // Cast to generic slice first
func (w lintStringFormatRule) parseArgument(argument any, ruleNum int) (scope stringFormatSubruleScope, regex *regexp.Regexp, negated bool, errorMessage string) {
g, ok := argument.([]any) // Cast to generic slice first
if !ok {
w.configError("argument is not a slice", ruleNum, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion rule/unused-param.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *UnusedParamRule) configure(args lint.Arguments) {
r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it as _"
} else {
// Arguments = [{}]
options := args[0].(map[string]interface{})
options := args[0].(map[string]any)
// Arguments = [{allowedRegex="^_"}]

if allowedRegexParam, ok := options["allowRegex"]; ok {
Expand Down
2 changes: 1 addition & 1 deletion rule/unused-receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) {
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]interface{})
options := args[0].(map[string]any)
// Arguments = [{allowedRegex="^_"}]

if allowedRegexParam, ok := options["allowRegex"]; ok {
Expand Down
2 changes: 1 addition & 1 deletion rule/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func isExprABooleanLit(n ast.Node) (lexeme string, ok bool) {
}

// gofmt returns a string representation of an AST subtree.
func gofmt(x interface{}) string {
func gofmt(x any) string {
buf := bytes.Buffer{}
fs := token.NewFileSet()
printer.Fprint(&buf, fs, x)
Expand Down
8 changes: 4 additions & 4 deletions rule/var-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) {
if len(arguments) >= 3 {
// not pretty code because should keep compatibility with TOML (no mixed array types) and new map parameters
thirdArgument := arguments[2]
asSlice, ok := thirdArgument.([]interface{})
asSlice, ok := thirdArgument.([]any)
if !ok {
panic(fmt.Sprintf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, got %T", "options", arguments[2]))
}
if len(asSlice) != 1 {
panic(fmt.Sprintf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, but %d", "options", len(asSlice)))
}
args, ok := asSlice[0].(map[string]interface{})
args, ok := asSlice[0].(map[string]any)
if !ok {
panic(fmt.Sprintf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, with map, but %T", "options", asSlice[0]))
}
Expand Down Expand Up @@ -255,8 +255,8 @@ func (w *lintNames) Visit(n ast.Node) ast.Visitor {
return w
}

func getList(arg interface{}, argName string) []string {
temp, ok := arg.([]interface{})
func getList(arg any, argName string) []string {
temp, ok := arg.([]any)
if !ok {
panic(fmt.Sprintf("Invalid argument to the var-naming rule. Expecting a %s of type slice with initialisms, got %T", argName, arg))
}
Expand Down
2 changes: 1 addition & 1 deletion test/add-constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestAddConstant(t *testing.T) {
args := []interface{}{map[string]interface{}{
args := []any{map[string]any{
"maxLitCount": "2",
"allowStrs": "\"\"",
"allowInts": "0,1,2",
Expand Down
2 changes: 1 addition & 1 deletion test/argument-limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

func TestArgumentLimit(t *testing.T) {
testRule(t, "argument-limit", &rule.ArgumentsLimitRule{}, &lint.RuleConfig{
Arguments: []interface{}{int64(3)},
Arguments: []any{int64(3)},
})
}
2 changes: 1 addition & 1 deletion test/banned-characters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import (
// One banned character from the list is not present in the fixture file.
func TestBannedCharacters(t *testing.T) {
testRule(t, "banned-characters", &rule.BannedCharsRule{}, &lint.RuleConfig{
Arguments: []interface{}{"Ω", "Σ", "σ", "1"},
Arguments: []any{"Ω", "Σ", "σ", "1"},
})
}
2 changes: 1 addition & 1 deletion test/cognitive-complexity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

func TestCognitiveComplexity(t *testing.T) {
testRule(t, "cognitive-complexity", &rule.CognitiveComplexityRule{}, &lint.RuleConfig{
Arguments: []interface{}{int64(0)},
Arguments: []any{int64(0)},
})
}
2 changes: 1 addition & 1 deletion test/comment-spacings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

func TestCommentSpacings(t *testing.T) {
testRule(t, "comment-spacings", &rule.CommentSpacingsRule{}, &lint.RuleConfig{
Arguments: []interface{}{"myOwnDirective"}},
Arguments: []any{"myOwnDirective"}},
)
}
4 changes: 2 additions & 2 deletions test/context-as-argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

func TestContextAsArgument(t *testing.T) {
testRule(t, "context-as-argument", &rule.ContextAsArgumentRule{}, &lint.RuleConfig{
Arguments: []interface{}{
map[string]interface{}{
Arguments: []any{
map[string]any{
"allowTypesBefore": "AllowedBeforeType,AllowedBeforeStruct,*AllowedBeforePtrStruct,*testing.T",
},
},
Expand Down
4 changes: 2 additions & 2 deletions test/cyclomatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

func TestCyclomatic(t *testing.T) {
testRule(t, "cyclomatic", &rule.CyclomaticRule{}, &lint.RuleConfig{
Arguments: []interface{}{int64(1)},
Arguments: []any{int64(1)},
})
testRule(t, "cyclomatic-2", &rule.CyclomaticRule{}, &lint.RuleConfig{
Arguments: []interface{}{int64(3)},
Arguments: []any{int64(3)},
})
}
4 changes: 2 additions & 2 deletions test/defer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestDefer(t *testing.T) {

func TestDeferLoopDisabled(t *testing.T) {
testRule(t, "defer-loop-disabled", &rule.DeferRule{}, &lint.RuleConfig{
Arguments: []interface{}{[]interface{}{"return", "recover", "call-chain", "method-call"}},
Arguments: []any{[]any{"return", "recover", "call-chain", "method-call"}},
})
}

func TestDeferOthersDisabled(t *testing.T) {
testRule(t, "defer-only-loop-enabled", &rule.DeferRule{}, &lint.RuleConfig{
Arguments: []interface{}{[]interface{}{"loop"}},
Arguments: []any{[]any{"loop"}},
})
}
2 changes: 1 addition & 1 deletion test/early-return_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import (
// TestEarlyReturn tests early-return rule.
func TestEarlyReturn(t *testing.T) {
testRule(t, "early-return", &rule.EarlyReturnRule{})
testRule(t, "early-return-scope", &rule.EarlyReturnRule{}, &lint.RuleConfig{Arguments: []interface{}{ifelse.PreserveScope}})
testRule(t, "early-return-scope", &rule.EarlyReturnRule{}, &lint.RuleConfig{Arguments: []any{ifelse.PreserveScope}})
}
4 changes: 2 additions & 2 deletions test/enforce-map-style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ func TestEnforceMapStyle_any(t *testing.T) {

func TestEnforceMapStyle_make(t *testing.T) {
testRule(t, "enforce-map-style-make", &rule.EnforceMapStyleRule{}, &lint.RuleConfig{
Arguments: []interface{}{"make"},
Arguments: []any{"make"},
})
}

func TestEnforceMapStyle_literal(t *testing.T) {
testRule(t, "enforce-map-style-literal", &rule.EnforceMapStyleRule{}, &lint.RuleConfig{
Arguments: []interface{}{"literal"},
Arguments: []any{"literal"},
})
}
4 changes: 2 additions & 2 deletions test/enforce-slice-style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ func TestEnforceSliceStyle_any(t *testing.T) {

func TestEnforceSliceStyle_make(t *testing.T) {
testRule(t, "enforce-slice-style-make", &rule.EnforceSliceStyleRule{}, &lint.RuleConfig{
Arguments: []interface{}{"make"},
Arguments: []any{"make"},
})
}

func TestEnforceSliceStyle_literal(t *testing.T) {
testRule(t, "enforce-slice-style-literal", &rule.EnforceSliceStyleRule{}, &lint.RuleConfig{
Arguments: []interface{}{"literal"},
Arguments: []any{"literal"},
})
}
2 changes: 1 addition & 1 deletion test/error-strings-custom-functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestErrorStringsWithCustomFunctions(t *testing.T) {
args := []interface{}{"pkgErrors.Wrap"}
args := []any{"pkgErrors.Wrap"}
testRule(t, "error-strings-with-custom-functions", &rule.ErrorStringsRule{}, &lint.RuleConfig{
Arguments: args,
})
Expand Down
6 changes: 3 additions & 3 deletions test/exported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
)

func TestExportedWithDisableStutteringCheck(t *testing.T) {
args := []interface{}{"disableStutteringCheck"}
args := []any{"disableStutteringCheck"}

testRule(t, "exported-issue-555", &rule.ExportedRule{}, &lint.RuleConfig{Arguments: args})
}

func TestExportedWithChecksOnMethodsOfPrivateTypes(t *testing.T) {
args := []interface{}{"checkPrivateReceivers"}
args := []any{"checkPrivateReceivers"}

testRule(t, "exported-issue-552", &rule.ExportedRule{}, &lint.RuleConfig{Arguments: args})
}

func TestExportedReplacingStuttersByRepetitive(t *testing.T) {
args := []interface{}{"sayRepetitiveInsteadOfStutters"}
args := []any{"sayRepetitiveInsteadOfStutters"}

testRule(t, "exported-issue-519", &rule.ExportedRule{}, &lint.RuleConfig{Arguments: args})
}
14 changes: 7 additions & 7 deletions test/file-header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ import (

func TestLintFileHeader(t *testing.T) {
testRule(t, "lint-file-header1", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"foobar"},
Arguments: []any{"foobar"},
})

testRule(t, "lint-file-header2", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"foobar"},
Arguments: []any{"foobar"},
})

testRule(t, "lint-file-header3", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"foobar"},
Arguments: []any{"foobar"},
})

testRule(t, "lint-file-header4", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"^\\sfoobar$"},
Arguments: []any{"^\\sfoobar$"},
})

testRule(t, "lint-file-header5", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"^\\sfoo.*bar$"},
Arguments: []any{"^\\sfoo.*bar$"},
})

testRule(t, "lint-file-header6", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"foobar"},
Arguments: []any{"foobar"},
})
}

func BenchmarkLintFileHeader(b *testing.B) {
var t *testing.T
for i := 0; i <= b.N; i++ {
testRule(t, "lint-file-header1", &rule.FileHeaderRule{}, &lint.RuleConfig{
Arguments: []interface{}{"foobar"},
Arguments: []any{"foobar"},
})
}
}
6 changes: 3 additions & 3 deletions test/function-length_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (

func TestFuncLengthLimitsStatements(t *testing.T) {
testRule(t, "function-length1", &rule.FunctionLength{}, &lint.RuleConfig{
Arguments: []interface{}{int64(2), int64(100)},
Arguments: []any{int64(2), int64(100)},
})
}

func TestFuncLengthLimitsLines(t *testing.T) {
testRule(t, "function-length2", &rule.FunctionLength{}, &lint.RuleConfig{
Arguments: []interface{}{int64(100), int64(5)},
Arguments: []any{int64(100), int64(5)},
})
}

func TestFuncLengthLimitsDeactivated(t *testing.T) {
testRule(t, "function-length3", &rule.FunctionLength{}, &lint.RuleConfig{
Arguments: []interface{}{int64(0), int64(0)},
Arguments: []any{int64(0), int64(0)},
})
}
2 changes: 1 addition & 1 deletion test/function-result-limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

func TestFunctionResultsLimit(t *testing.T) {
testRule(t, "function-result-limit", &rule.FunctionResultsLimitRule{}, &lint.RuleConfig{
Arguments: []interface{}{int64(3)},
Arguments: []any{int64(3)},
})
}
6 changes: 3 additions & 3 deletions test/import-blacklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
)

func TestImportsBlacklistOriginal(t *testing.T) {
args := []interface{}{"crypto/md5", "crypto/sha1"}
args := []any{"crypto/md5", "crypto/sha1"}

testRule(t, "imports-blacklist-original", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{
Arguments: args,
})
}

func TestImportsBlacklist(t *testing.T) {
args := []interface{}{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"}
args := []any{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"}

testRule(t, "imports-blacklist", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{
Arguments: args,
})
}

func BenchmarkImportsBlacklist(b *testing.B) {
args := []interface{}{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"}
args := []any{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"}
var t *testing.T
for i := 0; i <= b.N; i++ {
testRule(t, "imports-blacklist", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{
Expand Down
2 changes: 1 addition & 1 deletion test/line-length-limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

func TestLineLengthLimit(t *testing.T) {
testRule(t, "line-length-limit", &rule.LineLengthLimitRule{}, &lint.RuleConfig{
Arguments: []interface{}{int64(100)},
Arguments: []any{int64(100)},
})
}
2 changes: 1 addition & 1 deletion test/max-public-structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (

func TestMaxPublicStructs(t *testing.T) {
testRule(t, "max-public-structs", &rule.MaxPublicStructsRule{}, &lint.RuleConfig{
Arguments: []interface{}{int64(1)},
Arguments: []any{int64(1)},
})
}
Loading

0 comments on commit 36c2ee2

Please sign in to comment.