diff --git a/.golangci.yml b/.golangci.yml index 8daf8a8..6d5fc7f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -137,6 +137,7 @@ formatters: enable: - gci - gofmt + - gofumpt - goimports settings: gci: diff --git a/config/config_test.go b/config/config_test.go index 678f7cc..0ce4262 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -287,7 +287,6 @@ func TestGetConfig(t *testing.T) { } cfg, err := config.GetConfig(cfgPath) - if err != nil { t.Fatalf("Unexpected error %v", err) } diff --git a/formatter/friendly.go b/formatter/friendly.go index de24df8..f69ae19 100644 --- a/formatter/friendly.go +++ b/formatter/friendly.go @@ -57,8 +57,10 @@ func (f *Friendly) printFriendlyFailure(sb *strings.Builder, failure lint.Failur sb.WriteString("\n\n") } -var errorEmoji = color.RedString("✘") -var warningEmoji = color.YellowString("⚠") +var ( + errorEmoji = color.RedString("✘") + warningEmoji = color.YellowString("⚠") +) func (*Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) { emoji := warningEmoji diff --git a/logging/logger_test.go b/logging/logger_test.go index 3dfeb85..625f2c3 100644 --- a/logging/logger_test.go +++ b/logging/logger_test.go @@ -12,7 +12,6 @@ func TestGetLogger(t *testing.T) { t.Setenv("DEBUG", "") logger, err := logging.GetLogger() - if err != nil { t.Fatalf("expected no error, got %v", err) } @@ -27,7 +26,6 @@ func TestGetLogger(t *testing.T) { t.Cleanup(func() { os.Remove("revive.log") }) logger, err := logging.GetLogger() - if err != nil { t.Fatalf("expected no error, got %v", err) } diff --git a/revivelib/core_internal_test.go b/revivelib/core_internal_test.go index a6dcd84..5c0368b 100644 --- a/revivelib/core_internal_test.go +++ b/revivelib/core_internal_test.go @@ -36,8 +36,7 @@ func TestReviveCreateInstance(t *testing.T) { } } -type mockRule struct { -} +type mockRule struct{} func (*mockRule) Name() string { return "mock-rule" diff --git a/rule/add_constant_test.go b/rule/add_constant_test.go index c6e4d92..b1fcb9f 100644 --- a/rule/add_constant_test.go +++ b/rule/add_constant_test.go @@ -17,7 +17,6 @@ func TestAddConstantRule_Configure(t *testing.T) { wantStrLitLimit int }{ { - name: "no arguments", arguments: lint.Arguments{}, wantErr: nil, diff --git a/rule/string_format_test.go b/rule/string_format_test.go index ae904d4..47e56bb 100644 --- a/rule/string_format_test.go +++ b/rule/string_format_test.go @@ -18,121 +18,177 @@ func TestStringFormatConfigure(t *testing.T) { { name: "Not a Slice", arguments: lint.Arguments{ - "this is not a slice"}, - wantErr: errors.New("invalid configuration for string-format: argument is not a slice [argument 0, option 0]")}, + "this is not a slice", + }, + wantErr: errors.New("invalid configuration for string-format: argument is not a slice [argument 0, option 0]"), + }, { name: "Missing Regex", arguments: lint.Arguments{ []any{ - "method[0]"}}, - wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]")}, + "method[0]", + }, + }, + wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]"), + }, { name: "Bad Argument Type", arguments: lint.Arguments{ []any{ - 1}}, - wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]")}, + 1, + }, + }, + wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]"), + }, { name: "Empty Scope", arguments: lint.Arguments{ []any{ "", - "//"}}, - wantErr: errors.New("invalid configuration for string-format: empty scope provided [argument 0, option 0]")}, + "//", + }, + }, + wantErr: errors.New("invalid configuration for string-format: empty scope provided [argument 0, option 0]"), + }, { name: "Small or Empty Regex", arguments: lint.Arguments{ []any{ "method[1].a", - "-"}}, - wantErr: errors.New("invalid configuration for string-format: regex is too small (regexes should begin and end with '/') [argument 0, option 1]")}, + "-", + }, + }, + wantErr: errors.New("invalid configuration for string-format: regex is too small (regexes should begin and end with '/') [argument 0, option 1]"), + }, { name: "Bad Scope", arguments: lint.Arguments{ []any{ "1.a", - "//"}}, - wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]")}, + "//", + }, + }, + wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]"), + }, { name: "Bad Regex", arguments: lint.Arguments{ []any{ "method[1].a", - "/(/"}}, - wantErr: errors.New("failed to parse configuration for string-format: unable to compile /(/ as regexp [argument 0, option 1]")}, + "/(/", + }, + }, + wantErr: errors.New("failed to parse configuration for string-format: unable to compile /(/ as regexp [argument 0, option 1]"), + }, { name: "Sample Config", arguments: lint.Arguments{ []any{ - "core.WriteError[1].Message", "/^([^A-Z]$)/", "must not start with a capital letter"}, + "core.WriteError[1].Message", "/^([^A-Z]$)/", "must not start with a capital letter", + }, []any{ - "fmt.Errorf[0]", "/^|[^\\.!?]$/", "must not end in punctuation"}, + "fmt.Errorf[0]", "/^|[^\\.!?]$/", "must not end in punctuation", + }, []any{ - "panic", "/^[^\\n]*$/", "must not contain line breaks"}}}, + "panic", "/^[^\\n]*$/", "must not contain line breaks", + }, + }, + }, { name: "Underscores in Scope", arguments: lint.Arguments{ []any{ "some_pkg._some_function_name[5].some_member", - "//"}}}, + "//", + }, + }, + }, { name: "Underscores in Multiple Scopes", arguments: lint.Arguments{ []any{ "fmt.Errorf[0],core.WriteError[1].Message", - "//"}}}, + "//", + }, + }, + }, { name: "', ' Delimiter", arguments: lint.Arguments{ []any{ "abc, mt.Errorf", - "//"}}}, + "//", + }, + }, + }, { name: "' ,' Delimiter", arguments: lint.Arguments{ []any{ "abc ,mt.Errorf", - "//"}}}, + "//", + }, + }, + }, { name: "', ' Delimiter", arguments: lint.Arguments{ []any{ "abc, mt.Errorf", - "//"}}}, + "//", + }, + }, + }, { name: "', ' Delimiter", arguments: lint.Arguments{ []any{ "abc, mt.Errorf", - "//"}}}, + "//", + }, + }, + }, { name: "Empty Middle Scope", arguments: lint.Arguments{ []any{ "abc, ,mt.Errorf", - "//"}}, - wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 1]")}, + "//", + }, + }, + wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 1]"), + }, { name: "Empty First Scope", arguments: lint.Arguments{ []any{ ",mt.Errorf", - "//"}}, - wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 0]")}, + "//", + }, + }, + wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 0]"), + }, { name: "Bad First Scope", arguments: lint.Arguments{ []any{ "1.a,fmt.Errorf[0]", - "//"}}, - wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]")}, + "//", + }, + }, + wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]"), + }, { name: "Bad Second Scope", arguments: lint.Arguments{ []any{ "fmt.Errorf[0],1.a", - "//"}}, - wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 1]")}} + "//", + }, + }, + wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 1]"), + }, + } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 1d1d787..c245f51 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -631,7 +631,7 @@ func checkTOMLTag(checkCtx *checkContext, tag *structtag.Tag, _ *ast.Field) (mes } func checkURLTag(checkCtx *checkContext, tag *structtag.Tag, _ *ast.Field) (message string, succeeded bool) { - var delimiter = "" + var delimiter string for _, opt := range tag.Options { switch opt { case "int", "omitempty", "numbered", "brackets", diff --git a/rule/unsecure_url_scheme.go b/rule/unsecure_url_scheme.go index 1f2fb79..09b4771 100644 --- a/rule/unsecure_url_scheme.go +++ b/rule/unsecure_url_scheme.go @@ -42,13 +42,15 @@ type lintUnsecureURLSchemeRule struct { onFailure func(lint.Failure) } -const schemeSeparator = "://" -const schemeHTTP = "http" -const schemeWS = "ws" -const urlPrefixHTTP = schemeHTTP + schemeSeparator -const urlPrefixWS = schemeWS + schemeSeparator -const lenURLPrefixHTTP = len(urlPrefixHTTP) -const lenURLPrefixWS = len(urlPrefixWS) +const ( + schemeSeparator = "://" + schemeHTTP = "http" + schemeWS = "ws" + urlPrefixHTTP = schemeHTTP + schemeSeparator + urlPrefixWS = schemeWS + schemeSeparator + lenURLPrefixHTTP = len(urlPrefixHTTP) + lenURLPrefixWS = len(urlPrefixWS) +) func (w lintUnsecureURLSchemeRule) Visit(node ast.Node) ast.Visitor { n, ok := node.(*ast.BasicLit) diff --git a/test/comment_spacings_test.go b/test/comment_spacings_test.go index a7a3d6b..741069a 100644 --- a/test/comment_spacings_test.go +++ b/test/comment_spacings_test.go @@ -9,6 +9,7 @@ import ( func TestCommentSpacings(t *testing.T) { testRule(t, "comment_spacings", &rule.CommentSpacingsRule{}, &lint.RuleConfig{ - Arguments: []any{"myOwnDirective:", "+optional"}}, + Arguments: []any{"myOwnDirective:", "+optional"}, + }, ) } diff --git a/test/max_control_nesting_test.go b/test/max_control_nesting_test.go index 87ffea4..98d5c3e 100644 --- a/test/max_control_nesting_test.go +++ b/test/max_control_nesting_test.go @@ -13,6 +13,7 @@ func TestMaxControlNestingDefault(t *testing.T) { func TestMaxControlNesting(t *testing.T) { testRule(t, "max_control_nesting", &rule.MaxControlNestingRule{}, &lint.RuleConfig{ - Arguments: []any{int64(2)}}, + Arguments: []any{int64(2)}, + }, ) } diff --git a/test/string_format_test.go b/test/string_format_test.go index 1fb745b..979c57b 100644 --- a/test/string_format_test.go +++ b/test/string_format_test.go @@ -13,19 +13,25 @@ func TestStringFormat(t *testing.T) { []any{ "stringFormatMethod1", // The first argument is checked by default "/^[A-Z]/", - "must start with a capital letter"}, + "must start with a capital letter", + }, []any{ "stringFormatMethod2[2].d", - "/[^\\.]$/"}, // Must not end with a period + "/[^\\.]$/", + }, // Must not end with a period []any{ "s.Method3[2]", "!/^[Tt][Hh]/", - "must not start with 'th'"}, + "must not start with 'th'", + }, []any{ "s.Method4", // same as before, but called from a struct "!/^[Ot][Tt]/", - "must not start with 'ot'"}}}) + "must not start with 'ot'", + }, + }, + }) } func TestStringFormatDuplicatedStrings(t *testing.T) { diff --git a/test/var_naming_test.go b/test/var_naming_test.go index 7e67639..ee9247d 100644 --- a/test/var_naming_test.go +++ b/test/var_naming_test.go @@ -16,19 +16,22 @@ func TestVarNaming(t *testing.T) { []any{}, []any{}, []any{map[string]any{"skip-initialism-name-checks": true}}, - }}) + }, + }) testRule(t, "var_naming_skip_initialism_name_checks_false", &rule.VarNamingRule{}, &lint.RuleConfig{ Arguments: []any{ []any{}, []any{}, []any{map[string]any{"skip-initialism-name-checks": false}}, - }}) + }, + }) testRule(t, "var_naming_allowlist_blocklist_skip_initialism_name_checks", &rule.VarNamingRule{}, &lint.RuleConfig{ Arguments: []any{ []any{"ID"}, []any{"VM"}, []any{map[string]any{"skip-initialism-name-checks": true}}, - }}) + }, + }) testRule(t, "var_naming_test", &rule.VarNamingRule{}, &lint.RuleConfig{}) @@ -49,19 +52,25 @@ func TestVarNaming(t *testing.T) { }) testRule(t, "var_naming_meaningless_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{}) testRule(t, "var_naming_meaningless_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{ - Arguments: []any{[]any{}, []any{}, + Arguments: []any{ + []any{}, + []any{}, []any{map[string]any{"skip-package-name-checks": false}}, }, }) testRule(t, "var_naming_bad_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{ - Arguments: []any{[]any{}, []any{}, + Arguments: []any{ + []any{}, + []any{}, []any{map[string]any{"skip-package-name-checks": false, "extra-bad-package-names": []any{"helpers"}}}, }, }) testRule(t, "var_naming_top_level_pkg", &rule.VarNamingRule{}, &lint.RuleConfig{}) testRule(t, "var_naming_std_lib_conflict", &rule.VarNamingRule{}, &lint.RuleConfig{}) testRule(t, "var_naming_std_lib_conflict_skip", &rule.VarNamingRule{}, &lint.RuleConfig{ - Arguments: []any{[]any{}, []any{}, + Arguments: []any{ + []any{}, + []any{}, []any{map[string]any{"skip-package-name-collision-with-go-std": true}}, }, })