From 89322bce5acebec04ee0549f105d2531766338b2 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Thu, 14 Nov 2024 02:57:52 +0100 Subject: [PATCH 01/36] refactor: replace panic with error in rules --- lint/file.go | 8 +++- lint/rule.go | 2 +- revivelib/core_internal_test.go | 4 +- revivelib/core_test.go | 4 +- rule/add_constant.go | 23 +++++---- rule/argument_limit.go | 14 +++--- rule/atomic.go | 5 +- rule/banned_characters.go | 26 ++++++---- rule/bare_return.go | 5 +- rule/blank_imports.go | 7 +-- rule/bool_literal_in_expr.go | 5 +- rule/call_to_gc.go | 5 +- rule/cognitive_complexity.go | 14 +++--- rule/comment_spacings.go | 12 +++-- rule/comments_density.go | 16 ++++--- rule/confusing_naming.go | 5 +- rule/confusing_results.go | 5 +- rule/constant_logical_expr.go | 5 +- rule/context_as_argument.go | 21 ++++---- rule/context_keys_type.go | 5 +- rule/cyclomatic.go | 14 +++--- rule/datarace.go | 5 +- rule/deep_exit.go | 5 +- rule/defer.go | 26 ++++++---- rule/dot_imports.go | 18 +++---- rule/duplicated_imports.go | 5 +- rule/early_return.go | 5 +- rule/empty_block.go | 5 +- rule/empty_lines.go | 5 +- rule/enforce_map_style.go | 19 ++++---- rule/enforce_repeated_arg_type_style.go | 56 ++++++++++++++-------- rule/enforce_slice_style.go | 20 ++++---- rule/error_naming.go | 5 +- rule/error_return.go | 5 +- rule/error_strings.go | 13 +++-- rule/errorf.go | 5 +- rule/exported.go | 17 ++++--- rule/file_header.go | 24 +++++----- rule/file_length_limit.go | 26 +++++----- rule/filename_format.go | 21 ++++---- rule/flag_param.go | 5 +- rule/function_length.go | 31 +++++++----- rule/function_result_limit.go | 16 ++++--- rule/get_return.go | 5 +- rule/identical_branches.go | 5 +- rule/if_return.go | 5 +- rule/import_alias_naming.go | 46 +++++++++++------- rule/import_shadowing.go | 5 +- rule/imports_blocklist.go | 12 +++-- rule/increment_decrement.go | 5 +- rule/indent_error_flow.go | 5 +- rule/line_length_limit.go | 14 +++--- rule/max_control_nesting.go | 14 +++--- rule/max_public_structs.go | 15 +++--- rule/modifies_param.go | 5 +- rule/modifies_value_receiver.go | 5 +- rule/nested_structs.go | 5 +- rule/optimize_operands_order.go | 5 +- rule/package_comments.go | 7 +-- rule/range.go | 5 +- rule/range_val_address.go | 7 +-- rule/range_val_in_closure.go | 7 +-- rule/receiver_naming.go | 18 +++---- rule/redefines_builtin_id.go | 5 +- rule/redundant_import_alias.go | 5 +- rule/string_format.go | 64 ++++++++++++++----------- rule/string_of_int.go | 5 +- rule/struct_tag.go | 14 +++--- rule/superfluous_else.go | 4 +- rule/time_equal.go | 7 +-- rule/time_naming.go | 5 +- rule/unchecked_type_assertion.go | 16 ++++--- rule/unconditional_recursion.go | 5 +- rule/unexported_naming.go | 5 +- rule/unexported_return.go | 5 +- rule/unhandled_error.go | 16 ++++--- rule/unnecessary_stmt.go | 4 +- rule/unreachable_code.go | 5 +- rule/unused_param.go | 14 +++--- rule/unused_receiver.go | 14 +++--- rule/use_any.go | 5 +- rule/useless_break.go | 5 +- rule/utils.go | 5 +- rule/var_declarations.go | 4 +- rule/var_naming.go | 36 +++++++++----- rule/waitgroup_by_value.go | 5 +- test/file_filter_test.go | 4 +- 87 files changed, 588 insertions(+), 406 deletions(-) diff --git a/lint/file.go b/lint/file.go index e34f8b7f4..6fdfb26fd 100644 --- a/lint/file.go +++ b/lint/file.go @@ -96,7 +96,7 @@ func (f *File) isMain() bool { const directiveSpecifyDisableReason = "specify-disable-reason" -func (f *File) lint(rules []Rule, config Config, failures chan Failure) { +func (f *File) lint(rules []Rule, config Config, failures chan Failure) error { rulesConfig := config.Rules _, mustSpecifyDisableReason := config.Directives[directiveSpecifyDisableReason] disabledIntervals := f.disabledIntervals(rules, mustSpecifyDisableReason, failures) @@ -105,7 +105,10 @@ func (f *File) lint(rules []Rule, config Config, failures chan Failure) { if ruleConfig.MustExclude(f.Name) { continue } - currentFailures := currentRule.Apply(f, ruleConfig.Arguments) + currentFailures, err := currentRule.Apply(f, ruleConfig.Arguments) + if err != nil { + return err + } for idx, failure := range currentFailures { if failure.RuleName == "" { failure.RuleName = currentRule.Name() @@ -122,6 +125,7 @@ func (f *File) lint(rules []Rule, config Config, failures chan Failure) { } } } + return nil } type enableDisableConfig struct { diff --git a/lint/rule.go b/lint/rule.go index ccc66691c..100f291c5 100644 --- a/lint/rule.go +++ b/lint/rule.go @@ -14,7 +14,7 @@ type DisabledInterval struct { // Rule defines an abstract rule interface type Rule interface { Name() string - Apply(*File, Arguments) []Failure + Apply(*File, Arguments) ([]Failure, error) } // AbstractRule defines an abstract rule. diff --git a/revivelib/core_internal_test.go b/revivelib/core_internal_test.go index a6dcd846a..298346c1d 100644 --- a/revivelib/core_internal_test.go +++ b/revivelib/core_internal_test.go @@ -43,8 +43,8 @@ func (*mockRule) Name() string { return "mock-rule" } -func (*mockRule) Apply(_ *lint.File, _ lint.Arguments) []lint.Failure { - return nil +func (*mockRule) Apply(_ *lint.File, _ lint.Arguments) ([]lint.Failure, error) { + return nil, nil } func getMockRevive(t *testing.T) *Revive { diff --git a/revivelib/core_test.go b/revivelib/core_test.go index 10670c765..2ad47b340 100644 --- a/revivelib/core_test.go +++ b/revivelib/core_test.go @@ -78,8 +78,8 @@ func (r *mockRule) Name() string { return "mock-rule" } -func (r *mockRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - return nil +func (r *mockRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { + return nil, nil } func getMockRevive(t *testing.T) *revivelib.Revive { diff --git a/rule/add_constant.go b/rule/add_constant.go index 233f1d848..55596fec2 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -40,7 +41,7 @@ type AddConstantRule struct { } // Apply applies the rule to given file. -func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -60,7 +61,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. @@ -200,7 +201,7 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool { return ok } -func (r *AddConstantRule) configure(arguments lint.Arguments) { +func (r *AddConstantRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() @@ -210,7 +211,7 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) { if len(arguments) > 0 { 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])) + return fmt.Errorf("Invalid argument to the add-constant rule. Expecting a k,v map, got %T", arguments[0]) } for k, v := range args { kind := "" @@ -229,35 +230,35 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) { } list, ok := v.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v)) + return fmt.Errorf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v) } r.allowList.add(kind, list) case "maxLitCount": sl, ok := v.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v)) + return fmt.Errorf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v) } limit, err := strconv.Atoi(sl) if err != nil { - panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v)) + return fmt.Errorf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v) } r.strLitLimit = limit case "ignoreFuncs": excludes, ok := v.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v)) + return fmt.Errorf("Invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v) } for _, exclude := range strings.Split(excludes, ",") { exclude = strings.Trim(exclude, " ") if exclude == "" { - panic("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty.") + return fmt.Errorf("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty") } exp, err := regexp.Compile(exclude) if err != nil { - panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err)) + return fmt.Errorf(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err)) } r.ignoreFunctions = append(r.ignoreFunctions, exp) @@ -266,4 +267,6 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) { } } } + + return nil } diff --git a/rule/argument_limit.go b/rule/argument_limit.go index b6ce0e81a..f88965f8c 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -16,27 +17,28 @@ type ArgumentsLimitRule struct { const defaultArgumentsLimit = 8 -func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) { +func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.max != 0 { - return + return nil } if len(arguments) < 1 { r.max = defaultArgumentsLimit - return + return nil } maxArguments, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - panic(`invalid value passed as argument number to the "argument-limit" rule`) + return fmt.Errorf(`invalid value passed as argument number to the "argument-limit" rule`) } r.max = int(maxArguments) + return nil } // Apply applies the rule to given file. -func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -51,7 +53,7 @@ func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) [] ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/atomic.go b/rule/atomic.go index 287b28c21..45c9c43d4 100644 --- a/rule/atomic.go +++ b/rule/atomic.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type AtomicRule struct{} // Apply applies the rule to given file. -func (*AtomicRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*AtomicRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure walker := atomic{ pkgTypesInfo: file.Pkg.TypesInfo(), @@ -23,7 +24,7 @@ func (*AtomicRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 12997bae1..7386cba2c 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,17 +18,26 @@ type BannedCharsRule struct { const bannedCharsRuleName = "banned-characters" -func (r *BannedCharsRule) configure(arguments lint.Arguments) { +func (r *BannedCharsRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.bannedCharList == nil && len(arguments) > 0 { - checkNumberOfArguments(1, arguments, bannedCharsRuleName) - r.bannedCharList = r.getBannedCharsList(arguments) + check := checkNumberOfArguments(1, arguments, bannedCharsRuleName) + if check != nil { + return fmt.Errorf("error:%s", check) + } + list, err := r.getBannedCharsList(arguments) + if err != nil { + return err + } + + r.bannedCharList = list } + return nil } // Apply applied the rule to the given file. -func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -41,7 +51,7 @@ func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lin } ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name @@ -50,17 +60,17 @@ func (*BannedCharsRule) Name() string { } // getBannedCharsList converts arguments into the banned characters list -func (r *BannedCharsRule) getBannedCharsList(args lint.Arguments) []string { +func (r *BannedCharsRule) getBannedCharsList(args lint.Arguments) ([]string, error) { var bannedChars []string for _, char := range args { charStr, ok := char.(string) if !ok { - panic(fmt.Sprintf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), char)) + return nil, fmt.Errorf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), char) } bannedChars = append(bannedChars, charStr) } - return bannedChars + return bannedChars, nil } type lintBannedCharsRule struct { diff --git a/rule/bare_return.go b/rule/bare_return.go index 147fa84db..6cc29754a 100644 --- a/rule/bare_return.go +++ b/rule/bare_return.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type BareReturnRule struct{} // Apply applies the rule to given file. -func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +20,7 @@ func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := lintBareReturnRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/blank_imports.go b/rule/blank_imports.go index 0ddb4aad2..fc09e26a9 100644 --- a/rule/blank_imports.go +++ b/rule/blank_imports.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -16,9 +17,9 @@ func (*BlankImportsRule) Name() string { } // Apply applies the rule to given file. -func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { if file.Pkg.IsMain() || file.IsTest() { - return nil + return nil, nil } const ( @@ -59,7 +60,7 @@ func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failu } } - return failures + return failures, nil } func (*BlankImportsRule) fileHasValidEmbedComment(fileAst *ast.File) bool { diff --git a/rule/bool_literal_in_expr.go b/rule/bool_literal_in_expr.go index 71551e55a..74fc281e3 100644 --- a/rule/bool_literal_in_expr.go +++ b/rule/bool_literal_in_expr.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type BoolLiteralRule struct{} // Apply applies the rule to given file. -func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -22,7 +23,7 @@ func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure w := &lintBoolLiteral{astFile, onFailure} ast.Walk(w, astFile) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/call_to_gc.go b/rule/call_to_gc.go index 9c68380a4..cfaae6d1c 100644 --- a/rule/call_to_gc.go +++ b/rule/call_to_gc.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type CallToGCRule struct{} // Apply applies the rule to given file. -func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -23,7 +24,7 @@ func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := lintCallToGC{onFailure, gcTriggeringFunctions} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index 83640fd3d..9b7d236e2 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -18,28 +19,29 @@ type CognitiveComplexityRule struct { const defaultMaxCognitiveComplexity = 7 -func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) { +func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.maxComplexity != 0 { - return // already configured + return nil // already configured } if len(arguments) < 1 { r.maxComplexity = defaultMaxCognitiveComplexity - return + return nil } complexity, ok := arguments[0].(int64) if !ok { - panic(fmt.Sprintf("invalid argument type for cognitive-complexity, expected int64, got %T", arguments[0])) + return fmt.Errorf("invalid argument type for cognitive-complexity, expected int64, got %T", arguments[0]) } r.maxComplexity = int(complexity) + return nil } // Apply applies the rule to given file. -func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -54,7 +56,7 @@ func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Argument linter.lintCognitiveComplexity() - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index f72151301..dba3cc3a8 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -15,25 +16,26 @@ type CommentSpacingsRule struct { sync.Mutex } -func (r *CommentSpacingsRule) configure(arguments lint.Arguments) { +func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.allowList != nil { - return // already configured + return nil // already configured } r.allowList = []string{} for _, arg := range arguments { allow, ok := arg.(string) // Alt. non panicking version if !ok { - panic(fmt.Sprintf("invalid argument %v for %s; expected string but got %T", arg, r.Name(), arg)) + return fmt.Errorf("invalid argument %v for %s; expected string but got %T", arg, r.Name(), arg) } r.allowList = append(r.allowList, `//`+allow) } + return nil } // Apply the rule. -func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { +func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { r.configure(args) var failures []lint.Failure @@ -68,7 +70,7 @@ func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) []lint }) } } - return failures + return failures, nil } // Name yields this rule name. diff --git a/rule/comments_density.go b/rule/comments_density.go index c5298ea07..bf7178210 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -18,30 +19,31 @@ type CommentsDensityRule struct { const defaultMinimumCommentsPercentage = 0 -func (r *CommentsDensityRule) configure(arguments lint.Arguments) { +func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true if len(arguments) < 1 { r.minimumCommentsDensity = defaultMinimumCommentsPercentage - return + return nil } var ok bool r.minimumCommentsDensity, ok = arguments[0].(int64) if !ok { - panic(fmt.Sprintf("invalid argument for %q rule: argument should be an int, got %T", r.Name(), arguments[0])) + return fmt.Errorf("invalid argument for %q rule: argument should be an int, got %T", r.Name(), arguments[0]) } + return nil } // Apply applies the rule to given file. -func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) commentsLines := countDocLines(file.AST.Comments) @@ -56,10 +58,10 @@ func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) [ Failure: fmt.Sprintf("the file has a comment density of %2.f%% (%d comment lines for %d code lines) but expected a minimum of %d%%", density, commentsLines, statementsCount, r.minimumCommentsDensity), }, - } + }, nil } - return nil + return nil, nil } // Name returns the rule name. diff --git a/rule/confusing_naming.go b/rule/confusing_naming.go index 32f6dd803..846980e19 100644 --- a/rule/confusing_naming.go +++ b/rule/confusing_naming.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -47,7 +48,7 @@ var allPkgs = packages{pkgs: make([]pkgMethods, 1)} type ConfusingNamingRule struct{} // Apply applies the rule to given file. -func (*ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST pkgm := allPkgs.methodNames(file.Pkg) @@ -61,7 +62,7 @@ func (*ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail ast.Walk(&walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/confusing_results.go b/rule/confusing_results.go index 1b79ada9c..3935b22f4 100644 --- a/rule/confusing_results.go +++ b/rule/confusing_results.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type ConfusingResultsRule struct{} // Apply applies the rule to given file. -func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -22,7 +23,7 @@ func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fai ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/constant_logical_expr.go b/rule/constant_logical_expr.go index 9e34d3d16..bbf918352 100644 --- a/rule/constant_logical_expr.go +++ b/rule/constant_logical_expr.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type ConstantLogicalExprRule struct{} // Apply applies the rule to given file. -func (*ConstantLogicalExprRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ConstantLogicalExprRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,7 +22,7 @@ func (*ConstantLogicalExprRule) Apply(file *lint.File, _ lint.Arguments) []lint. astFile := file.AST w := &lintConstantLogicalExpr{astFile, onFailure} ast.Walk(w, astFile) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index e0c8cfa5e..6babf0d39 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -16,10 +17,14 @@ type ContextAsArgumentRule struct { } // Apply applies the rule to given file. -func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { +func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { r.Lock() if r.allowTypesLUT == nil { - r.allowTypesLUT = getAllowTypesFromArguments(args) + types, err := getAllowTypesFromArguments(args) + if err != nil { + return nil, err + } + r.allowTypesLUT = types } r.Unlock() @@ -35,7 +40,7 @@ func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) []li ast.Walk(walker, file.AST) - return failures + return failures,nil } // Name returns the rule name. @@ -79,23 +84,23 @@ func (w lintContextArguments) Visit(n ast.Node) ast.Visitor { return nil // avoid visiting the function body } -func getAllowTypesFromArguments(args lint.Arguments) map[string]struct{} { +func getAllowTypesFromArguments(args lint.Arguments) (map[string]struct{}, error) { allowTypesBefore := []string{} if len(args) >= 1 { 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])) + return nil, fmt.Errorf("Invalid argument to the context-as-argument rule. Expecting a k,v map, got %T", args[0]) } for k, v := range argKV { switch k { case "allowTypesBefore": typesBefore, ok := v.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T", v)) + return nil, fmt.Errorf("Invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T", v) } allowTypesBefore = append(allowTypesBefore, strings.Split(typesBefore, ",")...) default: - panic(fmt.Sprintf("Invalid argument to the context-as-argument rule. Unrecognized key %s", k)) + return nil, fmt.Errorf("Invalid argument to the context-as-argument rule. Unrecognized key %s", k) } } } @@ -106,5 +111,5 @@ func getAllowTypesFromArguments(args lint.Arguments) map[string]struct{} { } result["context.Context"] = struct{}{} // context.Context is always allowed before another context.Context - return result + return result, nil } diff --git a/rule/context_keys_type.go b/rule/context_keys_type.go index 60ccec560..bb4105125 100644 --- a/rule/context_keys_type.go +++ b/rule/context_keys_type.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type ContextKeysType struct{} // Apply applies the rule to given file. -func (*ContextKeysType) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ContextKeysType) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -27,7 +28,7 @@ func (*ContextKeysType) Apply(file *lint.File, _ lint.Arguments) []lint.Failure file.Pkg.TypeCheck() ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 10413de24..9d4f02492 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -19,27 +20,28 @@ type CyclomaticRule struct { const defaultMaxCyclomaticComplexity = 10 -func (r *CyclomaticRule) configure(arguments lint.Arguments) { +func (r *CyclomaticRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.maxComplexity != 0 { - return // already configured + return nil // already configured } if len(arguments) < 1 { r.maxComplexity = defaultMaxCyclomaticComplexity - return + return nil } complexity, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - panic(fmt.Sprintf("invalid argument for cyclomatic complexity; expected int but got %T", arguments[0])) + return fmt.Errorf("invalid argument for cyclomatic complexity; expected int but got %T", arguments[0]) } r.maxComplexity = int(complexity) + return nil } // Apply applies the rule to given file. -func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -55,7 +57,7 @@ func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/datarace.go b/rule/datarace.go index 21a7a706e..3635f700f 100644 --- a/rule/datarace.go +++ b/rule/datarace.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type DataRaceRule struct{} // Apply applies the rule to given file. -func (*DataRaceRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*DataRaceRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -20,7 +21,7 @@ func (*DataRaceRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/deep_exit.go b/rule/deep_exit.go index 7b3dd0f82..d2f132ad5 100644 --- a/rule/deep_exit.go +++ b/rule/deep_exit.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type DeepExitRule struct{} // Apply applies the rule to given file. -func (*DeepExitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*DeepExitRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -32,7 +33,7 @@ func (*DeepExitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := lintDeepExit{onFailure, exitFunctions, file.IsTest()} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/defer.go b/rule/defer.go index 3c31d507b..dd2abd4d2 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -14,18 +15,23 @@ type DeferRule struct { sync.Mutex } -func (r *DeferRule) configure(arguments lint.Arguments) { +func (r *DeferRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.allow != nil { - return // already configured + return nil // already configured } - r.allow = r.allowFromArgs(arguments) + list, err := r.allowFromArgs(arguments) + if err != nil { + return err + } + r.allow = list + return nil } // Apply applies the rule to given file. -func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -36,7 +42,7 @@ func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Fail ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. @@ -44,7 +50,7 @@ func (*DeferRule) Name() string { return "defer" } -func (*DeferRule) allowFromArgs(args lint.Arguments) map[string]bool { +func (*DeferRule) allowFromArgs(args lint.Arguments) (map[string]bool, error) { if len(args) < 1 { allow := map[string]bool{ "loop": true, @@ -55,24 +61,24 @@ func (*DeferRule) allowFromArgs(args lint.Arguments) map[string]bool { "immediate-recover": true, } - return allow + return allow, nil } aa, ok := args[0].([]any) if !ok { - panic(fmt.Sprintf("Invalid argument '%v' for 'defer' rule. Expecting []string, got %T", args[0], args[0])) + return nil, fmt.Errorf("Invalid argument '%v' for 'defer' rule. Expecting []string, got %T", args[0], args[0]) } allow := make(map[string]bool, len(aa)) for _, subcase := range aa { sc, ok := subcase.(string) if !ok { - panic(fmt.Sprintf("Invalid argument '%v' for 'defer' rule. Expecting string, got %T", subcase, subcase)) + return nil, fmt.Errorf("Invalid argument '%v' for 'defer' rule. Expecting string, got %T", subcase, subcase) } allow[sc] = true } - return allow + return allow, nil } type lintDeferRule struct { diff --git a/rule/dot_imports.go b/rule/dot_imports.go index df0b2a7f4..d391e82ec 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -15,7 +16,7 @@ type DotImportsRule struct { } // Apply applies the rule to given file. -func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -32,7 +33,7 @@ func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. @@ -40,37 +41,38 @@ func (*DotImportsRule) Name() string { return "dot-imports" } -func (r *DotImportsRule) configure(arguments lint.Arguments) { +func (r *DotImportsRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.allowedPackages != nil { - return + return nil } r.allowedPackages = make(allowPackages) if len(arguments) == 0 { - return + return nil } args, ok := arguments[0].(map[string]any) if !ok { - panic(fmt.Sprintf("Invalid argument to the dot-imports rule. Expecting a k,v map, got %T", arguments[0])) + return fmt.Errorf("Invalid argument to the dot-imports rule. Expecting a k,v map, got %T", arguments[0]) } if allowedPkgArg, ok := args["allowedPackages"]; ok { pkgs, ok := allowedPkgArg.([]any) if !ok { - panic(fmt.Sprintf("Invalid argument to the dot-imports rule, []string expected. Got '%v' (%T)", allowedPkgArg, allowedPkgArg)) + return fmt.Errorf("Invalid argument to the dot-imports rule, []string expected. Got '%v' (%T)", allowedPkgArg, allowedPkgArg) } for _, p := range pkgs { pkg, ok := p.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the dot-imports rule, string expected. Got '%v' (%T)", p, p)) + return fmt.Errorf("Invalid argument to the dot-imports rule, string expected. Got '%v' (%T)", p, p) } r.allowedPackages.add(pkg) } } + return nil } type lintImports struct { diff --git a/rule/duplicated_imports.go b/rule/duplicated_imports.go index 2b177fac6..a3e62b606 100644 --- a/rule/duplicated_imports.go +++ b/rule/duplicated_imports.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type DuplicatedImportsRule struct{} // Apply applies the rule to given file. -func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure impPaths := map[string]struct{}{} @@ -30,7 +31,7 @@ func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa impPaths[path] = struct{}{} } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/early_return.go b/rule/early_return.go index 62d491f27..8ef23a333 100644 --- a/rule/early_return.go +++ b/rule/early_return.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,8 +13,8 @@ import ( type EarlyReturnRule struct{} // Apply applies the rule to given file. -func (e *EarlyReturnRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - return ifelse.Apply(e, file.AST, ifelse.TargetIf, args) +func (e *EarlyReturnRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { + return ifelse.Apply(e, file.AST, ifelse.TargetIf, args), nil } // Name returns the rule name. diff --git a/rule/empty_block.go b/rule/empty_block.go index 25a052a0e..fa8547eee 100644 --- a/rule/empty_block.go +++ b/rule/empty_block.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type EmptyBlockRule struct{} // Apply applies the rule to given file. -func (*EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +20,7 @@ func (*EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := lintEmptyBlock{make(map[*ast.BlockStmt]bool), onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/empty_lines.go b/rule/empty_lines.go index 2710a8979..7630c58c7 100644 --- a/rule/empty_lines.go +++ b/rule/empty_lines.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type EmptyLinesRule struct{} // Apply applies the rule to given file. -func (r *EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (r *EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -20,7 +21,7 @@ func (r *EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure w := lintEmptyLines{file, r.commentLines(file.CommentMap(), file), onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index c698c40ed..b9ccecc26 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -44,39 +45,41 @@ type EnforceMapStyleRule struct { sync.Mutex } -func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) { +func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true if len(arguments) < 1 { r.enforceMapStyle = enforceMapStyleTypeAny - return + return nil } 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])) + return fmt.Errorf("Invalid argument '%v' for 'enforce-map-style' rule. Expecting string, got %T", arguments[0], arguments[0]) } var err error r.enforceMapStyle, err = mapStyleFromString(enforceMapStyle) if err != nil { - panic(fmt.Sprintf("Invalid argument to the enforce-map-style rule: %v", err)) + return fmt.Errorf("Invalid argument to the enforce-map-style rule: %v", err) } + + return nil } // Apply applies the rule to given file. -func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) if r.enforceMapStyle == enforceMapStyleTypeAny { // this linter is not configured - return nil + return nil, nil } var failures []lint.Failure @@ -136,7 +139,7 @@ func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) [ return true }) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index a435ee186..ee1141c95 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -16,14 +17,14 @@ const ( enforceRepeatedArgTypeStyleTypeFull enforceRepeatedArgTypeStyleType = "full" ) -func repeatedArgTypeStyleFromString(s string) enforceRepeatedArgTypeStyleType { +func repeatedArgTypeStyleFromString(s string) (enforceRepeatedArgTypeStyleType, error) { switch s { case string(enforceRepeatedArgTypeStyleTypeAny), "": - return enforceRepeatedArgTypeStyleTypeAny + return enforceRepeatedArgTypeStyleTypeAny, nil case string(enforceRepeatedArgTypeStyleTypeShort): - return enforceRepeatedArgTypeStyleTypeShort + return enforceRepeatedArgTypeStyleTypeShort, nil case string(enforceRepeatedArgTypeStyleTypeFull): - return enforceRepeatedArgTypeStyleTypeFull + return enforceRepeatedArgTypeStyleTypeFull, nil default: err := fmt.Errorf( "invalid repeated arg type style: %s (expecting one of %v)", @@ -35,7 +36,7 @@ func repeatedArgTypeStyleFromString(s string) enforceRepeatedArgTypeStyleType { }, ) - panic(fmt.Sprintf("Invalid argument to the enforce-repeated-arg-type-style rule: %v", err)) + return "", fmt.Errorf("Invalid argument to the enforce-repeated-arg-type-style rule: %v", err) } } @@ -48,12 +49,12 @@ type EnforceRepeatedArgTypeStyleRule struct { sync.Mutex } -func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) { +func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true @@ -61,44 +62,61 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) { r.funcRetValStyle = enforceRepeatedArgTypeStyleTypeAny if len(arguments) == 0 { - return + return nil } switch funcArgStyle := arguments[0].(type) { case string: - r.funcArgStyle = repeatedArgTypeStyleFromString(funcArgStyle) - r.funcRetValStyle = repeatedArgTypeStyleFromString(funcArgStyle) + argstyle, err := repeatedArgTypeStyleFromString(funcArgStyle) + if err != nil { + return err + } + r.funcArgStyle = argstyle + valstyle, err := repeatedArgTypeStyleFromString(funcArgStyle) + if err != nil { + return err + } + r.funcRetValStyle = valstyle case map[string]any: // expecting map[string]string for k, v := range funcArgStyle { switch k { case "funcArgStyle": val, ok := v.(string) if !ok { - panic(fmt.Sprintf("Invalid map value type for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v)) + return fmt.Errorf("Invalid map value type for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v) } - r.funcArgStyle = repeatedArgTypeStyleFromString(val) + valstyle, err := repeatedArgTypeStyleFromString(val) + if err != nil { + return err + } + r.funcArgStyle = valstyle case "funcRetValStyle": val, ok := v.(string) if !ok { - panic(fmt.Sprintf("Invalid map value '%v' for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v, v)) + return fmt.Errorf("Invalid map value '%v' for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v, v) + } + argstyle, err := repeatedArgTypeStyleFromString(val) + if err != nil { + return err } - r.funcRetValStyle = repeatedArgTypeStyleFromString(val) + r.funcRetValStyle = argstyle default: - panic(fmt.Sprintf("Invalid map key for 'enforce-repeated-arg-type-style' rule. Expecting 'funcArgStyle' or 'funcRetValStyle', got %v", k)) + return fmt.Errorf("Invalid map key for 'enforce-repeated-arg-type-style' rule. Expecting 'funcArgStyle' or 'funcRetValStyle', got %v", k) } } default: - panic(fmt.Sprintf("Invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0])) + return fmt.Errorf("Invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0]) } + return nil } // Apply applies the rule to a given file. -func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. - return nil + return nil, nil } var failures []lint.Failure @@ -178,7 +196,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint. return true }) - return failures + return failures, nil } // Name returns the name of the linter rule. diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 14be25893..29edb2e66 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -28,7 +29,7 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { case string(enforceSliceStyleTypeNil): return enforceSliceStyleTypeNil, nil default: - return enforceSliceStyleTypeAny, fmt.Errorf( + return "", fmt.Errorf( "invalid slice style: %s (expecting one of %v)", s, []enforceSliceStyleType{ @@ -48,39 +49,40 @@ type EnforceSliceStyleRule struct { sync.Mutex } -func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) { +func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true if len(arguments) < 1 { r.enforceSliceStyle = enforceSliceStyleTypeAny - return + return nil } enforceSliceStyle, ok := arguments[0].(string) if !ok { - panic(fmt.Sprintf("Invalid argument '%v' for 'enforce-slice-style' rule. Expecting string, got %T", arguments[0], arguments[0])) + return fmt.Errorf("Invalid argument '%v' for 'enforce-slice-style' rule. Expecting string, got %T", arguments[0], arguments[0]) } var err error r.enforceSliceStyle, err = sliceStyleFromString(enforceSliceStyle) if err != nil { - panic(fmt.Sprintf("Invalid argument to the enforce-slice-style rule: %v", err)) + return fmt.Errorf("Invalid argument to the enforce-slice-style rule: %v", err) } + return nil } // Apply applies the rule to given file. -func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) if r.enforceSliceStyle == enforceSliceStyleTypeAny { // this linter is not configured - return nil + return nil, nil } var failures []lint.Failure @@ -181,7 +183,7 @@ func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) return true }) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/error_naming.go b/rule/error_naming.go index a4f24f3f0..488c77114 100644 --- a/rule/error_naming.go +++ b/rule/error_naming.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -13,7 +14,7 @@ import ( type ErrorNamingRule struct{} // Apply applies the rule to given file. -func (*ErrorNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ErrorNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -27,7 +28,7 @@ func (*ErrorNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/error_return.go b/rule/error_return.go index a724e001c..7ec73efce 100644 --- a/rule/error_return.go +++ b/rule/error_return.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type ErrorReturnRule struct{} // Apply applies the rule to given file. -func (*ErrorReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ErrorReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -24,7 +25,7 @@ func (*ErrorReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/error_strings.go b/rule/error_strings.go index 81ebda540..a94faa37f 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -1,6 +1,8 @@ +// Package rule implements revive's linting rules. package rule import ( + "fmt" "go/ast" "go/token" "strconv" @@ -18,12 +20,12 @@ type ErrorStringsRule struct { sync.Mutex } -func (r *ErrorStringsRule) configure(arguments lint.Arguments) { +func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.errorFunctions != nil { - return + return nil } r.errorFunctions = map[string]map[string]struct{}{ @@ -52,12 +54,13 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) { } } if len(invalidCustomFunctions) != 0 { - panic("found invalid custom function: " + strings.Join(invalidCustomFunctions, ",")) + return fmt.Errorf("found invalid custom function: " + strings.Join(invalidCustomFunctions, ",")) } + return nil } // Apply applies the rule to given file. -func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure r.configure(arguments) @@ -74,7 +77,7 @@ func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) []li ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/errorf.go b/rule/errorf.go index 1588a745d..6e05864af 100644 --- a/rule/errorf.go +++ b/rule/errorf.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -13,7 +14,7 @@ import ( type ErrorfRule struct{} // Apply applies the rule to given file. -func (*ErrorfRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ErrorfRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -28,7 +29,7 @@ func (*ErrorfRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { file.Pkg.TypeCheck() ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/exported.go b/rule/exported.go index e3972d40e..d2d3394f6 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -61,11 +62,11 @@ type ExportedRule struct { sync.Mutex } -func (r *ExportedRule) configure(arguments lint.Arguments) { +func (r *ExportedRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true @@ -94,21 +95,23 @@ func (r *ExportedRule) configure(arguments lint.Arguments) { case "disableChecksOnVariables": r.disabledChecks.Var = true default: - panic(fmt.Sprintf("Unknown configuration flag %s for %s rule", flag, r.Name())) + return fmt.Errorf("Unknown configuration flag %s for %s rule", flag, r.Name()) } default: - panic(fmt.Sprintf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), flag)) + return fmt.Errorf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), flag) } } + + return nil } // Apply applies the rule to given file. -func (r *ExportedRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { +func (r *ExportedRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { r.configure(args) var failures []lint.Failure if file.IsTest() { - return failures + return failures, nil } fileAst := file.AST @@ -126,7 +129,7 @@ func (r *ExportedRule) Apply(file *lint.File, args lint.Arguments) []lint.Failur ast.Walk(&walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/file_header.go b/rule/file_header.go index 0dcb57746..7e91eaef6 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -19,30 +20,31 @@ var ( singleRegexp = regexp.MustCompile("^//") ) -func (r *FileHeaderRule) configure(arguments lint.Arguments) { +func (r *FileHeaderRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.header != "" { - return // already configured + return nil // already configured } if len(arguments) < 1 { - return + return nil } var ok bool r.header, ok = arguments[0].(string) if !ok { - panic(fmt.Sprintf("invalid argument for \"file-header\" rule: argument should be a string, got %T", arguments[0])) + return fmt.Errorf("invalid argument for \"file-header\" rule: argument should be a string, got %T", arguments[0]) } + return nil } // Apply applies the rule to given file. -func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) if r.header == "" { - return nil + return nil, nil } failure := []lint.Failure{ @@ -54,12 +56,12 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint } if len(file.AST.Comments) == 0 { - return failure + return failure, nil } g := file.AST.Comments[0] if g == nil { - return failure + return failure, nil } comment := "" for _, c := range g.List { @@ -74,13 +76,13 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint regex, err := regexp.Compile(r.header) if err != nil { - panic(err.Error()) + return nil, err } if !regex.MatchString(comment) { - return failure + return failure, nil } - return nil + return nil, nil } // Name returns the rule name. diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index c5a5641f4..3e3ec5ea6 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -24,12 +25,12 @@ type FileLengthLimitRule struct { } // Apply applies the rule to given file. -func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) if r.max <= 0 { // when max is negative or 0 the rule is disabled - return nil + return nil, nil } all := 0 @@ -43,7 +44,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ } if err := scanner.Err(); err != nil { - panic(err.Error()) + return nil, fmt.Errorf(err.Error()) } lines := all @@ -56,7 +57,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ } if lines <= r.max { - return nil + return nil, nil } return []lint.Failure{ @@ -71,47 +72,48 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ }, Failure: fmt.Sprintf("file length is %d lines, which exceeds the limit of %d", lines, r.max), }, - } + }, nil } -func (r *FileLengthLimitRule) configure(arguments lint.Arguments) { +func (r *FileLengthLimitRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.max != 0 { - return // already configured + return nil // already configured } if len(arguments) < 1 { - return // use default + return nil // use default } argKV, ok := arguments[0].(map[string]any) if !ok { - panic(fmt.Sprintf(`invalid argument to the "file-length-limit" rule. Expecting a k,v map, got %T`, arguments[0])) + return fmt.Errorf(`invalid argument to the "file-length-limit" rule. Expecting a k,v map, got %T`, arguments[0]) } for k, v := range argKV { switch k { case "max": maxLines, ok := v.(int64) if !ok || maxLines < 0 { - panic(fmt.Sprintf(`invalid configuration value for max lines in "file-length-limit" rule; need positive int64 but got %T`, arguments[0])) + return fmt.Errorf(`invalid configuration value for max lines in "file-length-limit" rule; need positive int64 but got %T`, arguments[0]) } r.max = int(maxLines) case "skipComments": skipComments, ok := v.(bool) if !ok { - panic(fmt.Sprintf(`invalid configuration value for skip comments in "file-length-limit" rule; need bool but got %T`, arguments[1])) + return fmt.Errorf(`invalid configuration value for skip comments in "file-length-limit" rule; need bool but got %T`, arguments[1]) } r.skipComments = skipComments case "skipBlankLines": skipBlankLines, ok := v.(bool) if !ok { - panic(fmt.Sprintf(`invalid configuration value for skip blank lines in "file-length-limit" rule; need bool but got %T`, arguments[2])) + return fmt.Errorf(`invalid configuration value for skip blank lines in "file-length-limit" rule; need bool but got %T`, arguments[2]) } r.skipBlankLines = skipBlankLines } } + return nil } // Name returns the rule name. diff --git a/rule/filename_format.go b/rule/filename_format.go index 49fdf9c3e..fc47caef4 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,12 +18,12 @@ type FilenameFormatRule struct { } // Apply applies the rule to the given file. -func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) filename := filepath.Base(file.Name) if r.format.MatchString(filename) { - return nil + return nil, nil } failureMsg := fmt.Sprintf("Filename %s is not of the format %s.%s", filename, r.format.String(), r.getMsgForNonASCIIChars(filename)) @@ -31,7 +32,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) [] Failure: failureMsg, RuleName: r.Name(), Node: file.AST.Name, - }} + }}, nil } func (r *FilenameFormatRule) getMsgForNonASCIIChars(str string) string { @@ -54,34 +55,36 @@ func (*FilenameFormatRule) Name() string { var defaultFormat = regexp.MustCompile("^[_A-Za-z0-9][_A-Za-z0-9-]*.go$") -func (r *FilenameFormatRule) configure(arguments lint.Arguments) { +func (r *FilenameFormatRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.format != nil { - return + return nil } argsCount := len(arguments) if argsCount == 0 { r.format = defaultFormat - return + return nil } if argsCount > 1 { - panic(fmt.Sprintf("rule %q expects only one argument, got %d %v", r.Name(), argsCount, arguments)) + return fmt.Errorf("rule %q expects only one argument, got %d %v", r.Name(), argsCount, arguments) } arg := arguments[0] str, ok := arg.(string) if !ok { - panic(fmt.Sprintf("rule %q expects a string argument, got %v of type %T", r.Name(), arg, arg)) + return fmt.Errorf("rule %q expects a string argument, got %v of type %T", r.Name(), arg, arg) } format, err := regexp.Compile(str) if err != nil { - panic(fmt.Sprintf("rule %q expects a valid regexp argument, got %v for %s", r.Name(), err, arg)) + return fmt.Errorf("rule %q expects a valid regexp argument, got %v for %s", r.Name(), err, arg) } r.format = format + + return nil } diff --git a/rule/flag_param.go b/rule/flag_param.go index f9bfb712c..0d917a27c 100644 --- a/rule/flag_param.go +++ b/rule/flag_param.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type FlagParamRule struct{} // Apply applies the rule to given file. -func (*FlagParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*FlagParamRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -20,7 +21,7 @@ func (*FlagParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := lintFlagParamRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/function_length.go b/rule/function_length.go index 30402313d..89dfe8a5d 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,21 +18,25 @@ type FunctionLength struct { sync.Mutex } -func (r *FunctionLength) configure(arguments lint.Arguments) { +func (r *FunctionLength) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true - maxStmt, maxLines := r.parseArguments(arguments) + maxStmt, maxLines, err := r.parseArguments(arguments) + if err != nil { + return err + } r.maxStmt = int(maxStmt) r.maxLines = int(maxLines) + return nil } // Apply applies the rule to given file. -func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -47,7 +52,7 @@ func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) []lint ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. @@ -58,33 +63,33 @@ func (*FunctionLength) Name() string { const defaultFuncStmtsLimit = 50 const defaultFuncLinesLimit = 75 -func (*FunctionLength) parseArguments(arguments lint.Arguments) (maxStmt, maxLines int64) { +func (*FunctionLength) parseArguments(arguments lint.Arguments) (maxStmt, maxLines int64, err error) { if len(arguments) == 0 { - return defaultFuncStmtsLimit, defaultFuncLinesLimit + return defaultFuncStmtsLimit, defaultFuncLinesLimit, nil } const minArguments = 2 if len(arguments) != minArguments { - panic(fmt.Sprintf(`invalid configuration for "function-length" rule, expected %d arguments but got %d`, minArguments, len(arguments))) + return 0, 0, fmt.Errorf(`invalid configuration for "function-length" rule, expected %d arguments but got %d`, minArguments, len(arguments)) } maxStmt, maxStmtOk := arguments[0].(int64) if !maxStmtOk { - panic(fmt.Sprintf(`invalid configuration value for max statements in "function-length" rule; need int64 but got %T`, arguments[0])) + return 0, 0, fmt.Errorf(`invalid configuration value for max statements in "function-length" rule; need int64 but got %T`, arguments[0]) } if maxStmt < 0 { - panic(fmt.Sprintf(`the configuration value for max statements in "function-length" rule cannot be negative, got %d`, maxStmt)) + return 0, 0, fmt.Errorf(`the configuration value for max statements in "function-length" rule cannot be negative, got %d`, maxStmt) } maxLines, maxLinesOk := arguments[1].(int64) if !maxLinesOk { - panic(fmt.Sprintf(`invalid configuration value for max lines in "function-length" rule; need int64 but got %T`, arguments[1])) + return 0, 0, fmt.Errorf(`invalid configuration value for max lines in "function-length" rule; need int64 but got %T`, arguments[1]) } if maxLines < 0 { - panic(fmt.Sprintf(`the configuration value for max statements in "function-length" rule cannot be negative, got %d`, maxLines)) + return 0, 0, fmt.Errorf(`the configuration value for max statements in "function-length" rule cannot be negative, got %d`, maxLines) } - return maxStmt, maxLines + return maxStmt, maxLines, nil } type lintFuncLength struct { diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 23474b5ee..e49ca2319 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -16,31 +17,32 @@ type FunctionResultsLimitRule struct { const defaultResultsLimit = 3 -func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) { +func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.max != 0 { - return // already configured + return nil // already configured } if len(arguments) < 1 { r.max = defaultResultsLimit - return + return nil } maxResults, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - panic(fmt.Sprintf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0])) + return fmt.Errorf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0]) } if maxResults < 0 { - panic(`the value passed as return results number to the "function-result-limit" rule cannot be negative`) + return fmt.Errorf(`the value passed as return results number to the "function-result-limit" rule cannot be negative`) } r.max = int(maxResults) + return nil } // Apply applies the rule to given file. -func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -54,7 +56,7 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/get_return.go b/rule/get_return.go index 06323a087..ba98fe353 100644 --- a/rule/get_return.go +++ b/rule/get_return.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type GetReturnRule struct{} // Apply applies the rule to given file. -func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,7 +22,7 @@ func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := lintReturnRule{onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/identical_branches.go b/rule/identical_branches.go index c6008925f..32ca9bc70 100644 --- a/rule/identical_branches.go +++ b/rule/identical_branches.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type IdenticalBranchesRule struct{} // Apply applies the rule to given file. -func (*IdenticalBranchesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*IdenticalBranchesRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -20,7 +21,7 @@ func (*IdenticalBranchesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa astFile := file.AST w := &lintIdenticalBranches{astFile, onFailure} ast.Walk(w, astFile) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/if_return.go b/rule/if_return.go index a6a3113ad..49f1b3557 100644 --- a/rule/if_return.go +++ b/rule/if_return.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type IfReturnRule struct{} // Apply applies the rule to given file. -func (*IfReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*IfReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -22,7 +23,7 @@ func (*IfReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { astFile := file.AST w := &lintElseError{astFile, onFailure} ast.Walk(w, astFile) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 48d22566a..382d09a26 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -20,43 +21,54 @@ const defaultImportAliasNamingAllowRule = "^[a-z][a-z0-9]{0,}$" var defaultImportAliasNamingAllowRegexp = regexp.MustCompile(defaultImportAliasNamingAllowRule) -func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) { +func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } if len(arguments) == 0 { r.allowRegexp = defaultImportAliasNamingAllowRegexp - return + return nil } switch namingRule := arguments[0].(type) { case string: - r.setAllowRule(namingRule) + err := r.setAllowRule(namingRule) + if err != nil { + return err + } case map[string]any: // expecting map[string]string for k, v := range namingRule { switch k { case "allowRegex": - r.setAllowRule(v) + err := r.setAllowRule(v) + if err != nil { + return err + } case "denyRegex": - r.setDenyRule(v) + err := r.setDenyRule(v) + if err != nil { + return err + } + default: - panic(fmt.Sprintf("Invalid map key for 'import-alias-naming' rule. Expecting 'allowRegex' or 'denyRegex', got %v", k)) + return fmt.Errorf("Invalid map key for 'import-alias-naming' rule. Expecting 'allowRegex' or 'denyRegex', got %v", k) } } default: - panic(fmt.Sprintf("Invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0])) + return fmt.Errorf("Invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0]) } if r.allowRegexp == nil && r.denyRegexp == nil { r.allowRegexp = defaultImportAliasNamingAllowRegexp } + return nil } // Apply applies the rule to given file. -func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -91,7 +103,7 @@ func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) } } - return failures + return failures, nil } // Name returns the rule name. @@ -99,28 +111,30 @@ func (*ImportAliasNamingRule) Name() string { return "import-alias-naming" } -func (r *ImportAliasNamingRule) setAllowRule(value any) { +func (r *ImportAliasNamingRule) setAllowRule(value any) error { namingRule, ok := value.(string) if !ok { - panic(fmt.Sprintf("Invalid argument '%v' for import-alias-naming allowRegexp rule. Expecting string, got %T", value, value)) + return fmt.Errorf("Invalid argument '%v' for import-alias-naming allowRegexp rule. Expecting string, got %T", value, value) } namingRuleRegexp, err := regexp.Compile(namingRule) if err != nil { - panic(fmt.Sprintf("Invalid argument to the import-alias-naming allowRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err)) + return fmt.Errorf("Invalid argument to the import-alias-naming allowRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) } r.allowRegexp = namingRuleRegexp + return nil } -func (r *ImportAliasNamingRule) setDenyRule(value any) { +func (r *ImportAliasNamingRule) setDenyRule(value any) error { namingRule, ok := value.(string) if !ok { - panic(fmt.Sprintf("Invalid argument '%v' for import-alias-naming denyRegexp rule. Expecting string, got %T", value, value)) + return fmt.Errorf("Invalid argument '%v' for import-alias-naming denyRegexp rule. Expecting string, got %T", value, value) } namingRuleRegexp, err := regexp.Compile(namingRule) if err != nil { - panic(fmt.Sprintf("Invalid argument to the import-alias-naming denyRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err)) + return fmt.Errorf("Invalid argument to the import-alias-naming denyRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) } r.denyRegexp = namingRuleRegexp + return nil } diff --git a/rule/import_shadowing.go b/rule/import_shadowing.go index 046aeb688..a3c5b098e 100644 --- a/rule/import_shadowing.go +++ b/rule/import_shadowing.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -13,7 +14,7 @@ import ( type ImportShadowingRule struct{} // Apply applies the rule to given file. -func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure importNames := map[string]struct{}{} @@ -34,7 +35,7 @@ func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 431066403..8af2b788f 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -16,7 +17,7 @@ type ImportsBlocklistRule struct { var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`) -func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) { +func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() @@ -26,15 +27,16 @@ func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) { for _, arg := range arguments { argStr, ok := arg.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the imports-blocklist rule. Expecting a string, got %T", arg)) + return fmt.Errorf("Invalid argument to the imports-blocklist rule. Expecting a string, got %T", arg) } regStr, err := regexp.Compile(fmt.Sprintf(`(?m)"%s"$`, replaceImportRegexp.ReplaceAllString(argStr, `(\W|\w)*`))) if err != nil { - panic(fmt.Sprintf("Invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %v", argStr, err)) + return fmt.Errorf("Invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %v", argStr, err) } r.blocklist = append(r.blocklist, regStr) } } + return nil } func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { @@ -47,7 +49,7 @@ func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { } // Apply applies the rule to given file. -func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -64,7 +66,7 @@ func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) } } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/increment_decrement.go b/rule/increment_decrement.go index 34a8e1ec5..fb9330fa6 100644 --- a/rule/increment_decrement.go +++ b/rule/increment_decrement.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type IncrementDecrementRule struct{} // Apply applies the rule to given file. -func (*IncrementDecrementRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*IncrementDecrementRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -25,7 +26,7 @@ func (*IncrementDecrementRule) Apply(file *lint.File, _ lint.Arguments) []lint.F ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/indent_error_flow.go b/rule/indent_error_flow.go index ebc1e793a..ba08b044e 100644 --- a/rule/indent_error_flow.go +++ b/rule/indent_error_flow.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -9,8 +10,8 @@ import ( type IndentErrorFlowRule struct{} // Apply applies the rule to given file. -func (e *IndentErrorFlowRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - return ifelse.Apply(e, file.AST, ifelse.TargetElse, args) +func (e *IndentErrorFlowRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { + return ifelse.Apply(e, file.AST, ifelse.TargetElse, args), nil } // Name returns the rule name. diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index a154b7aec..d9aea844f 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -20,28 +21,29 @@ type LineLengthLimitRule struct { const defaultLineLengthLimit = 80 -func (r *LineLengthLimitRule) configure(arguments lint.Arguments) { +func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.max != 0 { - return // already configured + return nil // already configured } if len(arguments) < 1 { r.max = defaultLineLengthLimit - return + return nil } maxLength, ok := arguments[0].(int64) // Alt. non panicking version if !ok || maxLength < 0 { - panic(`invalid value passed as argument number to the "line-length-limit" rule`) + return fmt.Errorf(`invalid value passed as argument number to the "line-length-limit" rule`) } r.max = int(maxLength) + return nil } // Apply applies the rule to given file. -func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -56,7 +58,7 @@ func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ checker.check() - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 5dbb1eefa..87d9c49ef 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,7 +18,7 @@ type MaxControlNestingRule struct { const defaultMaxControlNesting = 5 // Apply applies the rule to given file. -func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -33,7 +34,7 @@ func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. @@ -106,23 +107,24 @@ func (w *lintMaxControlNesting) walkControlledBlock(b ast.Node) { w.nestingLevelAcc = oldNestingLevel } -func (r *MaxControlNestingRule) configure(arguments lint.Arguments) { +func (r *MaxControlNestingRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if !(r.max < 1) { - return // max already configured + return nil // max already configured } if len(arguments) < 1 { r.max = defaultMaxControlNesting - return + return nil } checkNumberOfArguments(1, arguments, r.Name()) maxNesting, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - panic(`invalid value passed as argument number to the "max-control-nesting" rule`) + return fmt.Errorf(`invalid value passed as argument number to the "max-control-nesting" rule`) } r.max = maxNesting + return nil } diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 70840e734..561a65e0f 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -1,6 +1,8 @@ +// Package rule implements revive's linting rules. package rule import ( + "fmt" "go/ast" "strings" "sync" @@ -16,29 +18,30 @@ type MaxPublicStructsRule struct { const defaultMaxPublicStructs = 5 -func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) { +func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.max == 0 { - return // already configured + return nil // already configured } if len(arguments) < 1 { r.max = defaultMaxPublicStructs - return + return nil } checkNumberOfArguments(1, arguments, r.Name()) maxStructs, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - panic(`invalid value passed as argument number to the "max-public-structs" rule`) + return fmt.Errorf(`invalid value passed as argument number to the "max-public-structs" rule`) } r.max = maxStructs + return nil } // Apply applies the rule to given file. -func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -63,7 +66,7 @@ func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) }) } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/modifies_param.go b/rule/modifies_param.go index a68ae2501..7e804b716 100644 --- a/rule/modifies_param.go +++ b/rule/modifies_param.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type ModifiesParamRule struct{} // Apply applies the rule to given file. -func (*ModifiesParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ModifiesParamRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -20,7 +21,7 @@ func (*ModifiesParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failur w := lintModifiesParamRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/modifies_value_receiver.go b/rule/modifies_value_receiver.go index e9e64b9a6..1da9f8e5e 100644 --- a/rule/modifies_value_receiver.go +++ b/rule/modifies_value_receiver.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type ModifiesValRecRule struct{} // Apply applies the rule to given file. -func (*ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -22,7 +23,7 @@ func (*ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failu file.Pkg.TypeCheck() ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/nested_structs.go b/rule/nested_structs.go index 147bd482b..8ca897cb2 100644 --- a/rule/nested_structs.go +++ b/rule/nested_structs.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type NestedStructs struct{} // Apply applies the rule to given file. -func (*NestedStructs) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*NestedStructs) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure walker := &lintNestedStructs{ @@ -21,7 +22,7 @@ func (*NestedStructs) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/optimize_operands_order.go b/rule/optimize_operands_order.go index 43d982d6b..f174a5c9d 100644 --- a/rule/optimize_operands_order.go +++ b/rule/optimize_operands_order.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type OptimizeOperandsOrderRule struct{} // Apply applies the rule to given file. -func (*OptimizeOperandsOrderRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*OptimizeOperandsOrderRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -22,7 +23,7 @@ func (*OptimizeOperandsOrderRule) Apply(file *lint.File, _ lint.Arguments) []lin onFailure: onFailure, } ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/package_comments.go b/rule/package_comments.go index f1e5462fe..d573f874e 100644 --- a/rule/package_comments.go +++ b/rule/package_comments.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -20,11 +21,11 @@ type PackageCommentsRule struct { } // Apply applies the rule to given file. -func (r *PackageCommentsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (r *PackageCommentsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure if file.IsTest() { - return failures + return failures, nil } onFailure := func(failure lint.Failure) { @@ -34,7 +35,7 @@ func (r *PackageCommentsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa fileAst := file.AST w := &lintPackageComments{fileAst, file, onFailure, r} ast.Walk(w, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/range.go b/rule/range.go index 9d483a673..0cf06dfdd 100644 --- a/rule/range.go +++ b/rule/range.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type RangeRule struct{} // Apply applies the rule to given file. -func (*RangeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*RangeRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,7 +22,7 @@ func (*RangeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := &lintRanges{file, onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/range_val_address.go b/rule/range_val_address.go index d2ab0392a..bbe6135e9 100644 --- a/rule/range_val_address.go +++ b/rule/range_val_address.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -13,11 +14,11 @@ import ( type RangeValAddress struct{} // Apply applies the rule to given file. -func (*RangeValAddress) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*RangeValAddress) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure if file.Pkg.IsAtLeastGo122() { - return failures + return failures, nil } walker := rangeValAddress{ @@ -30,7 +31,7 @@ func (*RangeValAddress) Apply(file *lint.File, _ lint.Arguments) []lint.Failure file.Pkg.TypeCheck() ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/range_val_in_closure.go b/rule/range_val_in_closure.go index 6f9255a74..794895ef8 100644 --- a/rule/range_val_in_closure.go +++ b/rule/range_val_in_closure.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,11 +12,11 @@ import ( type RangeValInClosureRule struct{} // Apply applies the rule to given file. -func (*RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure if file.Pkg.IsAtLeastGo122() { - return failures + return failures, nil } walker := rangeValInClosure{ @@ -26,7 +27,7 @@ func (*RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index afcd99b8f..3dcfe5bbe 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,21 +18,21 @@ type ReceiverNamingRule struct { const defaultReceiverNameMaxLength = -1 // thus will not check -func (r *ReceiverNamingRule) configure(arguments lint.Arguments) { +func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.receiverNameMaxLength != 0 { - return + return nil } r.receiverNameMaxLength = defaultReceiverNameMaxLength if len(arguments) < 1 { - return + return nil } args, ok := arguments[0].(map[string]any) if !ok { - panic(fmt.Sprintf("Unable to get arguments for rule %s. Expected object of key-value-pairs.", r.Name())) + return fmt.Errorf("Unable to get arguments for rule %s. Expected object of key-value-pairs", r.Name()) } for k, v := range args { @@ -39,17 +40,18 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) { case "maxLength": value, ok := v.(int64) if !ok { - panic(fmt.Sprintf("Invalid value %v for argument %s of rule %s, expected integer value got %T", v, k, r.Name(), v)) + return fmt.Errorf("Invalid value %v for argument %s of rule %s, expected integer value got %T", v, k, r.Name(), v) } r.receiverNameMaxLength = int(value) default: - panic(fmt.Sprintf("Unknown argument %s for %s rule.", k, r.Name())) + return fmt.Errorf("Unknown argument %s for %s rule", k, r.Name()) } } + return nil } // Apply applies the rule to given file. -func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { +func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { r.configure(args) var failures []lint.Failure @@ -65,7 +67,7 @@ func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) []lint. ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/redefines_builtin_id.go b/rule/redefines_builtin_id.go index 10ea16ae1..ec1d44ca7 100644 --- a/rule/redefines_builtin_id.go +++ b/rule/redefines_builtin_id.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -68,7 +69,7 @@ var builtInTypes = map[string]bool{ type RedefinesBuiltinIDRule struct{} // Apply applies the rule to given file. -func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -89,7 +90,7 @@ func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.F } ast.Walk(w, astFile) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/redundant_import_alias.go b/rule/redundant_import_alias.go index fa5281f24..4016ef484 100644 --- a/rule/redundant_import_alias.go +++ b/rule/redundant_import_alias.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type RedundantImportAlias struct{} // Apply applies the rule to given file. -func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure for _, imp := range file.AST.Imports { @@ -30,7 +31,7 @@ func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) []lint.Fai } } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/string_format.go b/rule/string_format.go index ecac3fa7c..6738a6316 100644 --- a/rule/string_format.go +++ b/rule/string_format.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,7 +18,7 @@ import ( type StringFormatRule struct{} // Apply applies the rule to the given file. -func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -25,10 +26,14 @@ func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint } w := lintStringFormatRule{onFailure: onFailure} - w.parseArguments(arguments) + err := w.parseArguments(arguments) + if err != nil { + return failures, err + } + ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. @@ -43,10 +48,9 @@ func (StringFormatRule) ParseArgumentsTest(arguments lint.Arguments) *string { // 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() { - err := recover() + err := w.parseArguments(arguments) c <- err - }() - w.parseArguments(arguments) + }() }() err := <-c if err != nil { @@ -91,9 +95,12 @@ var parseStringFormatScope = regexp.MustCompile( // #region Argument parsing -func (w *lintStringFormatRule) parseArguments(arguments lint.Arguments) { +func (w *lintStringFormatRule) parseArguments(arguments lint.Arguments) error { for i, argument := range arguments { - scopes, regex, negated, errorMessage := w.parseArgument(argument, i) + scopes, regex, negated, errorMessage, err := w.parseArgument(argument, i) + if err != nil { + return err + } w.rules = append(w.rules, stringFormatSubrule{ parent: w, scopes: scopes, @@ -102,30 +109,31 @@ func (w *lintStringFormatRule) parseArguments(arguments lint.Arguments) { errorMessage: errorMessage, }) } + return nil } -func (w lintStringFormatRule) parseArgument(argument any, ruleNum int) (scopes stringFormatSubruleScopes, regex *regexp.Regexp, negated bool, errorMessage string) { +func (w lintStringFormatRule) parseArgument(argument any, ruleNum int) (scopes stringFormatSubruleScopes, regex *regexp.Regexp, negated bool, errorMessage string, err error) { g, ok := argument.([]any) // Cast to generic slice first if !ok { - w.configError("argument is not a slice", ruleNum, 0) + return stringFormatSubruleScopes{}, regex, false, "", w.configError("argument is not a slice", ruleNum, 0) } if len(g) < 2 { - w.configError("less than two slices found in argument, scope and regex are required", ruleNum, len(g)-1) + return stringFormatSubruleScopes{}, regex, false, "", w.configError("less than two slices found in argument, scope and regex are required", ruleNum, len(g)-1) } rule := make([]string, len(g)) for i, obj := range g { val, ok := obj.(string) if !ok { - w.configError("unexpected value, string was expected", ruleNum, i) + return stringFormatSubruleScopes{}, regex, false, "", w.configError("unexpected value, string was expected", ruleNum, i) } rule[i] = val } // Validate scope and regex length if rule[0] == "" { - w.configError("empty scope provided", ruleNum, 0) + return stringFormatSubruleScopes{}, regex, false, "", w.configError("empty scope provided", ruleNum, 0) } else if len(rule[1]) < 2 { - w.configError("regex is too small (regexes should begin and end with '/')", ruleNum, 1) + return stringFormatSubruleScopes{}, regex, false, "", w.configError("regex is too small (regexes should begin and end with '/')", ruleNum, 1) } // Parse rule scopes @@ -136,24 +144,24 @@ func (w lintStringFormatRule) parseArgument(argument any, ruleNum int) (scopes s rawScope = strings.TrimSpace(rawScope) if len(rawScope) == 0 { - w.parseScopeError("empty scope in rule scopes:", ruleNum, 0, scopeNum) + return stringFormatSubruleScopes{}, regex, false, "", w.parseScopeError("empty scope in rule scopes:", ruleNum, 0, scopeNum) } scope := stringFormatSubruleScope{} matches := parseStringFormatScope.FindStringSubmatch(rawScope) if matches == nil { // The rule's scope didn't match the parsing regex at all, probably a configuration error - w.parseScopeError("unable to parse rule scope", ruleNum, 0, scopeNum) + return stringFormatSubruleScopes{}, regex, false, "", w.parseScopeError("unable to parse rule scope", ruleNum, 0, scopeNum) } else if len(matches) != 4 { // The rule's scope matched the parsing regex, but an unexpected number of submatches was returned, probably a bug - w.parseScopeError(fmt.Sprintf("unexpected number of submatches when parsing scope: %d, expected 4", len(matches)), ruleNum, 0, scopeNum) + return stringFormatSubruleScopes{}, regex, false, "", w.parseScopeError(fmt.Sprintf("unexpected number of submatches when parsing scope: %d, expected 4", len(matches)), ruleNum, 0, scopeNum) } scope.funcName = matches[1] if len(matches[2]) > 0 { var err error scope.argument, err = strconv.Atoi(matches[2]) if err != nil { - w.parseScopeError("unable to parse argument number in rule scope", ruleNum, 0, scopeNum) + return stringFormatSubruleScopes{}, regex, false, "", w.parseScopeError("unable to parse argument number in rule scope", ruleNum, 0, scopeNum) } } if len(matches[3]) > 0 { @@ -169,31 +177,31 @@ func (w lintStringFormatRule) parseArgument(argument any, ruleNum int) (scopes s if negated { offset++ } - regex, err := regexp.Compile(rule[1][offset : len(rule[1])-1]) - if err != nil { - w.parseError(fmt.Sprintf("unable to compile %s as regexp", rule[1]), ruleNum, 1) + regex, errr := regexp.Compile(rule[1][offset : len(rule[1])-1]) + if errr != nil { + return stringFormatSubruleScopes{}, regex, false, "", w.parseError(fmt.Sprintf("unable to compile %s as regexp", rule[1]), ruleNum, 1) } // Use custom error message if provided if len(rule) == 3 { errorMessage = rule[2] } - return scopes, regex, negated, errorMessage + return scopes, regex, negated, errorMessage, nil } // Report an invalid config, this is specifically the user's fault -func (lintStringFormatRule) configError(msg string, ruleNum, option int) { - panic(fmt.Sprintf("invalid configuration for string-format: %s [argument %d, option %d]", msg, ruleNum, option)) +func (lintStringFormatRule) configError(msg string, ruleNum, option int) error { + return fmt.Errorf("invalid configuration for string-format: %s [argument %d, option %d]", msg, ruleNum, option) } // Report a general config parsing failure, this may be the user's fault, but it isn't known for certain -func (lintStringFormatRule) parseError(msg string, ruleNum, option int) { - panic(fmt.Sprintf("failed to parse configuration for string-format: %s [argument %d, option %d]", msg, ruleNum, option)) +func (lintStringFormatRule) parseError(msg string, ruleNum, option int) error { + return fmt.Errorf("failed to parse configuration for string-format: %s [argument %d, option %d]", msg, ruleNum, option) } // Report a general scope config parsing failure, this may be the user's fault, but it isn't known for certain -func (lintStringFormatRule) parseScopeError(msg string, ruleNum, option, scopeNum int) { - panic(fmt.Sprintf("failed to parse configuration for string-format: %s [argument %d, option %d, scope index %d]", msg, ruleNum, option, scopeNum)) +func (lintStringFormatRule) parseScopeError(msg string, ruleNum, option, scopeNum int) error { + return fmt.Errorf("failed to parse configuration for string-format: %s [argument %d, option %d, scope index %d]", msg, ruleNum, option, scopeNum) } // #endregion diff --git a/rule/string_of_int.go b/rule/string_of_int.go index 3bec1d6ac..e9049a02c 100644 --- a/rule/string_of_int.go +++ b/rule/string_of_int.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type StringOfIntRule struct{} // Apply applies the rule to given file. -func (*StringOfIntRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*StringOfIntRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -24,7 +25,7 @@ func (*StringOfIntRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure w := &lintStringInt{file, onFailure} ast.Walk(w, astFile) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/struct_tag.go b/rule/struct_tag.go index ec3f0c7cf..956ff3487 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,13 +18,13 @@ type StructTagRule struct { sync.Mutex } -func (r *StructTagRule) configure(arguments lint.Arguments) { +func (r *StructTagRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() mustConfigure := r.userDefined == nil && len(arguments) > 0 if !mustConfigure { - return + return nil } checkNumberOfArguments(1, arguments, r.Name()) @@ -31,11 +32,11 @@ func (r *StructTagRule) configure(arguments lint.Arguments) { for _, arg := range arguments { item, ok := arg.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the %s rule. Expecting a string, got %v (of type %T)", r.Name(), arg, arg)) + return fmt.Errorf("Invalid argument to the %s rule. Expecting a string, got %v (of type %T)", r.Name(), arg, arg) } parts := strings.Split(item, ",") if len(parts) < 2 { - panic(fmt.Sprintf("Invalid argument to the %s rule. Expecting a string of the form key[,option]+, got %s", r.Name(), item)) + return fmt.Errorf("Invalid argument to the %s rule. Expecting a string of the form key[,option]+, got %s", r.Name(), item) } key := strings.TrimSpace(parts[0]) for i := 1; i < len(parts); i++ { @@ -43,10 +44,11 @@ func (r *StructTagRule) configure(arguments lint.Arguments) { r.userDefined[key] = append(r.userDefined[key], option) } } + return nil } // Apply applies the rule to given file. -func (r *StructTagRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { +func (r *StructTagRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { r.configure(args) var failures []lint.Failure @@ -61,7 +63,7 @@ func (r *StructTagRule) Apply(file *lint.File, args lint.Arguments) []lint.Failu ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/superfluous_else.go b/rule/superfluous_else.go index 18e8f3bdd..0a24cf0ab 100644 --- a/rule/superfluous_else.go +++ b/rule/superfluous_else.go @@ -11,8 +11,8 @@ import ( type SuperfluousElseRule struct{} // Apply applies the rule to given file. -func (e *SuperfluousElseRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - return ifelse.Apply(e, file.AST, ifelse.TargetElse, args) +func (e *SuperfluousElseRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { + return ifelse.Apply(e, file.AST, ifelse.TargetElse, args), nil } // Name returns the rule name. diff --git a/rule/time_equal.go b/rule/time_equal.go index a4fab88b3..d2384f2cf 100644 --- a/rule/time_equal.go +++ b/rule/time_equal.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type TimeEqualRule struct{} // Apply applies the rule to given file. -func (*TimeEqualRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*TimeEqualRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,11 +22,11 @@ func (*TimeEqualRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { w := &lintTimeEqual{file, onFailure} if w.file.Pkg.TypeCheck() != nil { - return nil + return nil, nil } ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/time_naming.go b/rule/time_naming.go index 5bccf8a7a..151f700e6 100644 --- a/rule/time_naming.go +++ b/rule/time_naming.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -13,7 +14,7 @@ import ( type TimeNamingRule struct{} // Apply applies the rule to given file. -func (*TimeNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*TimeNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -24,7 +25,7 @@ func (*TimeNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { file.Pkg.TypeCheck() ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index eea344060..9503606bd 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -20,19 +21,19 @@ type UncheckedTypeAssertionRule struct { configured bool } -func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) { +func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { u.Lock() defer u.Unlock() if len(arguments) == 0 || u.configured { - return + return nil } u.configured = true args, ok := arguments[0].(map[string]any) if !ok { - panic("Unable to get arguments. Expected object of key-value-pairs.") + return fmt.Errorf("Unable to get arguments. Expected object of key-value-pairs") } for k, v := range args { @@ -40,16 +41,17 @@ func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) { case "acceptIgnoredAssertionResult": u.acceptIgnoredAssertionResult, ok = v.(bool) if !ok { - panic(fmt.Sprintf("Unable to parse argument '%s'. Expected boolean.", k)) + return fmt.Errorf("Unable to parse argument '%s'. Expected boolean", k) } default: - panic(fmt.Sprintf("Unknown argument: %s", k)) + return fmt.Errorf("Unknown argument: %s", k) } } + return nil } // Apply applies the rule to given file. -func (u *UncheckedTypeAssertionRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { +func (u *UncheckedTypeAssertionRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { u.configure(args) var failures []lint.Failure @@ -63,7 +65,7 @@ func (u *UncheckedTypeAssertionRule) Apply(file *lint.File, args lint.Arguments) ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unconditional_recursion.go b/rule/unconditional_recursion.go index d806b6757..16c3b3f14 100644 --- a/rule/unconditional_recursion.go +++ b/rule/unconditional_recursion.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type UnconditionalRecursionRule struct{} // Apply applies the rule to given file. -func (*UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +20,7 @@ func (*UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) []li w := lintUnconditionalRecursionRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unexported_naming.go b/rule/unexported_naming.go index 0c2b39d41..9c6165b89 100644 --- a/rule/unexported_naming.go +++ b/rule/unexported_naming.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -12,7 +13,7 @@ import ( type UnexportedNamingRule struct{} // Apply applies the rule to given file. -func (*UnexportedNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UnexportedNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -21,7 +22,7 @@ func (*UnexportedNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fai ba := &unexportablenamingLinter{onFailure} ast.Walk(ba, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unexported_return.go b/rule/unexported_return.go index 10f8e3fbe..05ef1d1d8 100644 --- a/rule/unexported_return.go +++ b/rule/unexported_return.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -13,7 +14,7 @@ import ( type UnexportedReturnRule struct{} // Apply applies the rule to given file. -func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -28,7 +29,7 @@ func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fai file.Pkg.TypeCheck() ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 95ba56180..7ef388c4e 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -17,36 +18,37 @@ type UnhandledErrorRule struct { sync.Mutex } -func (r *UnhandledErrorRule) configure(arguments lint.Arguments) { +func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.ignoreList != nil { - return // already configured + return nil // already configured } for _, arg := range arguments { argStr, ok := arg.(string) if !ok { - panic(fmt.Sprintf("Invalid argument to the unhandled-error rule. Expecting a string, got %T", arg)) + return fmt.Errorf("Invalid argument to the unhandled-error rule. Expecting a string, got %T", arg) } argStr = strings.Trim(argStr, " ") if argStr == "" { - panic("Invalid argument to the unhandled-error rule, expected regular expression must not be empty.") + return fmt.Errorf("Invalid argument to the unhandled-error rule, expected regular expression must not be empty") } exp, err := regexp.Compile(argStr) if err != nil { - panic(fmt.Sprintf("Invalid argument to the unhandled-error rule: regexp %q does not compile: %v", argStr, err)) + return fmt.Errorf("Invalid argument to the unhandled-error rule: regexp %q does not compile: %v", argStr, err) } r.ignoreList = append(r.ignoreList, exp) } + return nil } // Apply applies the rule to given file. -func (r *UnhandledErrorRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { +func (r *UnhandledErrorRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { r.configure(args) var failures []lint.Failure @@ -62,7 +64,7 @@ func (r *UnhandledErrorRule) Apply(file *lint.File, args lint.Arguments) []lint. file.Pkg.TypeCheck() ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unnecessary_stmt.go b/rule/unnecessary_stmt.go index 8e0784ba4..ede7b0a47 100644 --- a/rule/unnecessary_stmt.go +++ b/rule/unnecessary_stmt.go @@ -11,7 +11,7 @@ import ( type UnnecessaryStmtRule struct{} // Apply applies the rule to given file. -func (*UnnecessaryStmtRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UnnecessaryStmtRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -19,7 +19,7 @@ func (*UnnecessaryStmtRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail w := lintUnnecessaryStmtRule{onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unreachable_code.go b/rule/unreachable_code.go index dcc5b7905..8c3e7518a 100644 --- a/rule/unreachable_code.go +++ b/rule/unreachable_code.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type UnreachableCodeRule struct{} // Apply applies the rule to given file. -func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -38,7 +39,7 @@ func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail w := lintUnreachableCode{onFailure, branchingFunctions} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unused_param.go b/rule/unused_param.go index 4b04ee916..1d2064fbc 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -18,12 +19,12 @@ type UnusedParamRule struct { sync.Mutex } -func (r *UnusedParamRule) configure(args lint.Arguments) { +func (r *UnusedParamRule) configure(args lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true @@ -41,23 +42,24 @@ func (r *UnusedParamRule) configure(args lint.Arguments) { if allowedRegexParam, ok := options["allowRegex"]; ok { allowedRegexStr, ok = allowedRegexParam.(string) if !ok { - panic(fmt.Errorf("error configuring %s rule: allowedRegex is not string but [%T]", r.Name(), allowedRegexParam)) + return fmt.Errorf("error configuring %s rule: allowedRegex is not string but [%T]", r.Name(), allowedRegexParam) } } } var err error r.allowRegex, err = regexp.Compile(allowedRegexStr) if err != nil { - panic(fmt.Errorf("error configuring %s rule: allowedRegex is not valid regex [%s]: %v", r.Name(), allowedRegexStr, err)) + return fmt.Errorf("error configuring %s rule: allowedRegex is not valid regex [%s]: %v", r.Name(), allowedRegexStr, err) } if r.failureMsg == "" { r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it to match " + r.allowRegex.String() } + return nil } // 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, error) { r.configure(args) var failures []lint.Failure @@ -72,7 +74,7 @@ func (r *UnusedParamRule) Apply(file *lint.File, args lint.Arguments) []lint.Fai ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 715dba338..8629b7065 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -18,12 +19,12 @@ type UnusedReceiverRule struct { sync.Mutex } -func (r *UnusedReceiverRule) configure(args lint.Arguments) { +func (r *UnusedReceiverRule) configure(args lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true @@ -41,22 +42,23 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) { if allowedRegexParam, ok := options["allowRegex"]; ok { allowedRegexStr, ok = allowedRegexParam.(string) if !ok { - panic(fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not string but [%T]", allowedRegexParam)) + return fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not string but [%T]", allowedRegexParam) } } } var err error r.allowRegex, err = regexp.Compile(allowedRegexStr) if err != nil { - panic(fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not valid regex [%s]: %v", allowedRegexStr, err)) + return fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not valid regex [%s]: %v", allowedRegexStr, 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() } + return nil } // 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, error) { r.configure(args) var failures []lint.Failure @@ -72,7 +74,7 @@ func (r *UnusedReceiverRule) Apply(file *lint.File, args lint.Arguments) []lint. ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/use_any.go b/rule/use_any.go index 88160c2fa..538a0b1c5 100644 --- a/rule/use_any.go +++ b/rule/use_any.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type UseAnyRule struct{} // Apply applies the rule to given file. -func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure walker := lintUseAny{ @@ -21,7 +22,7 @@ func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { fileAst := file.AST ast.Walk(walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/useless_break.go b/rule/useless_break.go index 8db20c9b8..2223f4993 100644 --- a/rule/useless_break.go +++ b/rule/useless_break.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -11,7 +12,7 @@ import ( type UselessBreak struct{} // Apply applies the rule to given file. -func (*UselessBreak) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UselessBreak) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,7 +22,7 @@ func (*UselessBreak) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { astFile := file.AST w := &lintUselessBreak{onFailure, false} ast.Walk(w, astFile) - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/utils.go b/rule/utils.go index 1267c2d39..2e296ac33 100644 --- a/rule/utils.go +++ b/rule/utils.go @@ -160,10 +160,11 @@ func gofmt(x any) string { } // checkNumberOfArguments fails if the given number of arguments is not, at least, the expected one -func checkNumberOfArguments(expected int, args lint.Arguments, ruleName string) { +func checkNumberOfArguments(expected int, args lint.Arguments, ruleName string) error { if len(args) < expected { - panic(fmt.Sprintf("not enough arguments for %s rule, expected %d, got %d. Please check the rule's documentation", ruleName, expected, len(args))) + return fmt.Errorf("not enough arguments for %s rule, expected %d, got %d. Please check the rule's documentation", ruleName, expected, len(args)) } + return nil } var directiveCommentRE = regexp.MustCompile("^//(line |extern |export |[a-z0-9]+:[a-z0-9])") // see https://go-review.googlesource.com/c/website/+/442516/1..2/_content/doc/comment.md#494 diff --git a/rule/var_declarations.go b/rule/var_declarations.go index 3f9d7068a..a49cd5cf9 100644 --- a/rule/var_declarations.go +++ b/rule/var_declarations.go @@ -13,7 +13,7 @@ import ( type VarDeclarationsRule struct{} // Apply applies the rule to given file. -func (*VarDeclarationsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*VarDeclarationsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure fileAst := file.AST @@ -28,7 +28,7 @@ func (*VarDeclarationsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail file.Pkg.TypeCheck() ast.Walk(walker, fileAst) - return failures + return failures,nil } // Name returns the rule name. diff --git a/rule/var_naming.go b/rule/var_naming.go index 5a4d0dc24..dab11a20c 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -26,20 +27,28 @@ type VarNamingRule struct { sync.Mutex } -func (r *VarNamingRule) configure(arguments lint.Arguments) { +func (r *VarNamingRule) configure(arguments lint.Arguments) error { r.Lock() defer r.Unlock() if r.configured { - return + return nil } r.configured = true if len(arguments) >= 1 { - r.allowList = getList(arguments[0], "allowlist") + list, err := getList(arguments[0], "allowlist") + if err != nil { + return err + } + r.allowList = list } if len(arguments) >= 2 { - r.blockList = getList(arguments[1], "blocklist") + list, err := getList(arguments[1], "blocklist") + if err != nil { + return err + } + r.blockList = list } if len(arguments) >= 3 { @@ -47,18 +56,19 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) { thirdArgument := arguments[2] 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])) + return fmt.Errorf("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))) + return fmt.Errorf("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]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])) + return fmt.Errorf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, with map, but %T", "options", asSlice[0]) } r.allowUpperCaseConst = fmt.Sprint(args["upperCaseConst"]) == "true" r.skipPackageNameChecks = fmt.Sprint(args["skipPackageNameChecks"]) == "true" } + return nil } func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { @@ -82,7 +92,7 @@ func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { } // Apply applies the rule to given file. -func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configure(arguments) var failures []lint.Failure @@ -106,7 +116,7 @@ func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint. ast.Walk(&walker, fileAst) - return failures + return failures, nil } // Name returns the rule name. @@ -263,18 +273,18 @@ func (w *lintNames) Visit(n ast.Node) ast.Visitor { return w } -func getList(arg any, argName string) []string { +func getList(arg any, argName string) ([]string, error) { args, 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)) + return nil, fmt.Errorf("Invalid argument to the var-naming rule. Expecting a %s of type slice with initialisms, got %T", argName, arg) } var list []string for _, v := range args { val, ok := v.(string) if !ok { - panic(fmt.Sprintf("Invalid %s values of the var-naming rule. Expecting slice of strings but got element of type %T", val, arg)) + return nil, fmt.Errorf("Invalid %s values of the var-naming rule. Expecting slice of strings but got element of type %T", val, arg) } list = append(list, val) } - return list + return list, nil } diff --git a/rule/waitgroup_by_value.go b/rule/waitgroup_by_value.go index a2d304ae5..f43d7303e 100644 --- a/rule/waitgroup_by_value.go +++ b/rule/waitgroup_by_value.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( @@ -10,7 +11,7 @@ import ( type WaitGroupByValueRule struct{} // Apply applies the rule to given file. -func (*WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +20,7 @@ func (*WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fai w := lintWaitGroupByValueRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures + return failures, nil } // Name returns the rule name. diff --git a/test/file_filter_test.go b/test/file_filter_test.go index 6efdc3111..470b98730 100644 --- a/test/file_filter_test.go +++ b/test/file_filter_test.go @@ -13,9 +13,9 @@ type TestFileFilterRule struct { var _ lint.Rule = (*TestFileFilterRule)(nil) func (*TestFileFilterRule) Name() string { return "test-file-filter" } -func (tfr *TestFileFilterRule) Apply(*lint.File, lint.Arguments) []lint.Failure { +func (tfr *TestFileFilterRule) Apply(*lint.File, lint.Arguments) ([]lint.Failure, error) { tfr.WasApplied = true - return nil + return nil, nil } func TestFileExcludeFilterAtRuleLevel(t *testing.T) { From 5665063c20d4732734e801a5ca80065957cd5ecb Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 16 Nov 2024 14:57:23 +0100 Subject: [PATCH 02/36] handle outside apply --- lint/linter.go | 6 +++++- lint/package.go | 33 +++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lint/linter.go b/lint/linter.go index b42b397b2..2c6e850f4 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -152,7 +152,11 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS return nil } - pkg.lint(ruleSet, config, failures) + err := pkg.lint(ruleSet, config, failures) + + if err != nil { + return err + } return nil } diff --git a/lint/package.go b/lint/package.go index 4a633f35a..1f1313838 100644 --- a/lint/package.go +++ b/lint/package.go @@ -1,6 +1,7 @@ package lint import ( + "context" "go/ast" "go/importer" "go/token" @@ -182,17 +183,41 @@ func (p *Package) scanSortable() { } } -func (p *Package) lint(rules []Rule, config Config, failures chan Failure) { +func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error { p.scanSortable() var wg sync.WaitGroup + errCh := make(chan error, 1) // Buffered channel to signal the first error + _, cancel := context.WithCancel(context.Background()) + + defer cancel() // Ensure the context is canceled at the end + for _, file := range p.files { wg.Add(1) go (func(file *File) { - file.lint(rules, config, failures) - wg.Done() + defer wg.Done() + if err := file.lint(rules, config, failures); err != nil { + select { + case errCh <- err: // Send the error to the channel if it's empty + cancel() // Signal other goroutines to stop + default: + // If an error is already sent, do nothing + } + } })(file) } - wg.Wait() + + // Wait for all goroutines to complete + go func() { + wg.Wait() + close(errCh) + }() + + // Return the first error if any + if err, ok := <-errCh; ok { + return err + } + + return nil } // IsAtLeastGo121 returns true if the Go version for this package is 1.21 or higher, false otherwise From cf72c3d257b0a1f16091df46bbc926470e7a10ee Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 16 Nov 2024 16:03:26 +0100 Subject: [PATCH 03/36] handle error from r.configure --- rule/add_constant.go | 6 ++++-- rule/argument_limit.go | 7 +++++-- rule/banned_characters.go | 7 +++++-- rule/cognitive_complexity.go | 6 ++++-- rule/comment_spacings.go | 8 +++++--- rule/comments_density.go | 6 +++++- rule/cyclomatic.go | 7 +++++-- rule/defer.go | 7 +++++-- rule/dot_imports.go | 6 ++++-- rule/enforce_map_style.go | 8 +++++--- rule/enforce_repeated_arg_type_style.go | 8 +++++--- rule/enforce_slice_style.go | 8 +++++--- rule/error_strings.go | 6 ++++-- rule/exported.go | 9 ++++++--- rule/file_header.go | 6 +++++- rule/file_length_limit.go | 6 +++++- rule/filename_format.go | 6 +++++- rule/function_length.go | 6 ++++-- rule/function_result_limit.go | 6 ++++-- rule/import_alias_naming.go | 6 ++++-- rule/imports_blocklist.go | 6 ++++-- rule/line_length_limit.go | 6 ++++-- rule/max_control_nesting.go | 6 ++++-- rule/max_public_structs.go | 6 ++++-- rule/receiver_naming.go | 8 +++++--- rule/struct_tag.go | 9 ++++++--- rule/unchecked_type_assertion.go | 8 +++++--- rule/unhandled_error.go | 8 +++++--- rule/unused_param.go | 7 +++++-- rule/unused_receiver.go | 7 +++++-- rule/utils.go | 1 + rule/var_naming.go | 6 ++++-- 32 files changed, 146 insertions(+), 67 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index 55596fec2..f6ee34687 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -42,9 +42,11 @@ type AddConstantRule struct { // Apply applies the rule to given file. func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } onFailure := func(failure lint.Failure) { failures = append(failures, failure) diff --git a/rule/argument_limit.go b/rule/argument_limit.go index f88965f8c..471a0766d 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -39,9 +39,12 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } + onFailure := func(failure lint.Failure) { failures = append(failures, failure) } diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 7386cba2c..af0f1b536 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -38,9 +38,12 @@ func (r *BannedCharsRule) configure(arguments lint.Arguments) error { // Apply applied the rule to the given file. func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } + onFailure := func(failure lint.Failure) { failures = append(failures, failure) } diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index 9b7d236e2..fccf86134 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -42,9 +42,11 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } linter := cognitiveComplexityLinter{ file: file, diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index dba3cc3a8..fcf94ed58 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -35,10 +35,12 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { } // Apply the rule. -func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configure(args) - +func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } for _, cg := range file.AST.Comments { for _, comment := range cg.List { diff --git a/rule/comments_density.go b/rule/comments_density.go index bf7178210..65193d2ca 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -44,7 +44,11 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) + var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } commentsLines := countDocLines(file.AST.Comments) statementsCount := countStatements(file.AST) diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 9d4f02492..a1e489622 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -42,9 +42,12 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } + fileAst := file.AST walker := lintCyclomatic{ diff --git a/rule/defer.go b/rule/defer.go index dd2abd4d2..6c0dd726f 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -32,9 +32,12 @@ func (r *DeferRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } + onFailure := func(failure lint.Failure) { failures = append(failures, failure) } diff --git a/rule/dot_imports.go b/rule/dot_imports.go index d391e82ec..1c85cb1eb 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -17,9 +17,11 @@ type DotImportsRule struct { // Apply applies the rule to given file. func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } fileAst := file.AST walker := lintImports{ diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index b9ccecc26..5260beac1 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -75,15 +75,17 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) + var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } if r.enforceMapStyle == enforceMapStyleTypeAny { // this linter is not configured return nil, nil } - var failures []lint.Failure - astFile := file.AST ast.Inspect(astFile, func(n ast.Node) bool { switch v := n.(type) { diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index ee1141c95..f2ad37292 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -112,15 +112,17 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er // Apply applies the rule to a given file. func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) + var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. return nil, nil } - var failures []lint.Failure - astFile := file.AST ast.Inspect(astFile, func(n ast.Node) bool { switch fn := n.(type) { diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 29edb2e66..f31537553 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -78,15 +78,17 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) + var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } if r.enforceSliceStyle == enforceSliceStyleTypeAny { // this linter is not configured return nil, nil } - var failures []lint.Failure - astFile := file.AST ast.Inspect(astFile, func(n ast.Node) bool { switch v := n.(type) { diff --git a/rule/error_strings.go b/rule/error_strings.go index a94faa37f..01f104f1e 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -62,8 +62,10 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure - - r.configure(arguments) + err := r.configure(arguments) + if err != nil { + return failures, err + } fileAst := file.AST walker := lintErrorStrings{ diff --git a/rule/exported.go b/rule/exported.go index d2d3394f6..a6b8603ac 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -106,10 +106,13 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *ExportedRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configure(args) - +func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } + if file.IsTest() { return failures, nil } diff --git a/rule/file_header.go b/rule/file_header.go index 7e91eaef6..d56d31094 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -41,7 +41,11 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) + var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } if r.header == "" { return nil, nil diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index 3e3ec5ea6..e06c99d1a 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -26,7 +26,11 @@ type FileLengthLimitRule struct { // Apply applies the rule to given file. func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) + var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } if r.max <= 0 { // when max is negative or 0 the rule is disabled diff --git a/rule/filename_format.go b/rule/filename_format.go index fc47caef4..f60db09b5 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -19,7 +19,11 @@ type FilenameFormatRule struct { // Apply applies the rule to the given file. func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) + var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } filename := filepath.Base(file.Name) if r.format.MatchString(filename) { diff --git a/rule/function_length.go b/rule/function_length.go index 89dfe8a5d..a28552697 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -37,9 +37,11 @@ func (r *FunctionLength) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } walker := lintFuncLength{ file: file, diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index e49ca2319..866aef197 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -43,9 +43,11 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } walker := lintFunctionResultsNum{ max: r.max, diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 382d09a26..551e8d840 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -69,9 +69,11 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } for _, is := range file.AST.Imports { path := is.Path diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 8af2b788f..5984ddd92 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -50,9 +50,11 @@ func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { // Apply applies the rule to given file. func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } for _, is := range file.AST.Imports { path := is.Path diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index d9aea844f..c4a111d00 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -44,9 +44,11 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } checker := lintLineLengthNum{ max: r.max, diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 87d9c49ef..782d9ab5e 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -19,9 +19,11 @@ const defaultMaxControlNesting = 5 // Apply applies the rule to given file. func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } fileAst := file.AST diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 561a65e0f..b2501c985 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -42,9 +42,11 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } fileAst := file.AST diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 3dcfe5bbe..22e6bb90b 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -51,10 +51,12 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configure(args) - +func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } fileAst := file.AST walker := lintReceiverName{ diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 956ff3487..beca8e204 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -48,10 +48,13 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *StructTagRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configure(args) - +func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } + onFailure := func(failure lint.Failure) { failures = append(failures, failure) } diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index 9503606bd..fd68ae38a 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -51,10 +51,12 @@ func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (u *UncheckedTypeAssertionRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - u.configure(args) - +func (u *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := u.configure(arguments) + if err != nil { + return failures, err + } walker := &lintUncheckedTypeAssertion{ onFailure: func(failure lint.Failure) { diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 7ef388c4e..74c279533 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -48,10 +48,12 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *UnhandledErrorRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configure(args) - +func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } walker := &lintUnhandledErrors{ ignoreList: r.ignoreList, diff --git a/rule/unused_param.go b/rule/unused_param.go index 1d2064fbc..f9511bfc8 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -59,9 +59,12 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { } // Apply applies the rule to given file. -func (r *UnusedParamRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configure(args) +func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } onFailure := func(failure lint.Failure) { failures = append(failures, failure) diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 8629b7065..61a7fa257 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -58,9 +58,12 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { } // Apply applies the rule to given file. -func (r *UnusedReceiverRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configure(args) +func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } onFailure := func(failure lint.Failure) { failures = append(failures, failure) diff --git a/rule/utils.go b/rule/utils.go index 2e296ac33..991ae858e 100644 --- a/rule/utils.go +++ b/rule/utils.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/var_naming.go b/rule/var_naming.go index dab11a20c..6f3452cde 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -93,9 +93,11 @@ func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { // Apply applies the rule to given file. func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configure(arguments) - var failures []lint.Failure + err := r.configure(arguments) + if err != nil { + return failures, err + } fileAst := file.AST From 689e3c95c17662b2b99119eb3222a22fa433d7ad Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 16 Nov 2024 16:13:07 +0100 Subject: [PATCH 04/36] checkNumberOfArguments: handle error --- rule/max_control_nesting.go | 5 ++++- rule/max_public_structs.go | 5 ++++- rule/struct_tag.go | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 782d9ab5e..d2c140e0e 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -121,7 +121,10 @@ func (r *MaxControlNestingRule) configure(arguments lint.Arguments) error { return nil } - checkNumberOfArguments(1, arguments, r.Name()) + check := checkNumberOfArguments(1, arguments, r.Name()) + if check != nil { + return check + } maxNesting, ok := arguments[0].(int64) // Alt. non panicking version if !ok { diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index b2501c985..20643c922 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -30,7 +30,10 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { return nil } - checkNumberOfArguments(1, arguments, r.Name()) + check := checkNumberOfArguments(1, arguments, r.Name()) + if check != nil { + return check + } maxStructs, ok := arguments[0].(int64) // Alt. non panicking version if !ok { diff --git a/rule/struct_tag.go b/rule/struct_tag.go index beca8e204..a5c60184f 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -27,7 +27,10 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { return nil } - checkNumberOfArguments(1, arguments, r.Name()) + check := checkNumberOfArguments(1, arguments, r.Name()) + if check != nil { + return check + } r.userDefined = make(map[string][]string, len(arguments)) for _, arg := range arguments { item, ok := arg.(string) From 3109bd63229e8476ef8bd07453801c3e20056df6 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 16 Nov 2024 16:52:43 +0100 Subject: [PATCH 05/36] rules after review cleanup --- rule/add_constant.go | 5 +++-- rule/argument_limit.go | 3 ++- rule/enforce_map_style.go | 2 +- rule/enforce_repeated_arg_type_style.go | 2 +- rule/enforce_slice_style.go | 2 +- rule/file_header.go | 2 +- rule/file_length_limit.go | 2 +- rule/filename_format.go | 2 +- rule/function_result_limit.go | 3 ++- rule/imports_blocklist.go | 2 +- rule/line_length_limit.go | 3 ++- rule/max_control_nesting.go | 3 ++- rule/max_public_structs.go | 3 ++- rule/unchecked_type_assertion.go | 3 ++- rule/unhandled_error.go | 5 +++-- rule/unused_param.go | 2 +- rule/unused_receiver.go | 2 +- 17 files changed, 27 insertions(+), 19 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index f6ee34687..b5448953b 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -2,6 +2,7 @@ package rule import ( + "errors" "fmt" "go/ast" "regexp" @@ -255,12 +256,12 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) error { for _, exclude := range strings.Split(excludes, ",") { exclude = strings.Trim(exclude, " ") if exclude == "" { - return fmt.Errorf("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty") + return errors.New("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty") } exp, err := regexp.Compile(exclude) if err != nil { - return fmt.Errorf(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err)) + return fmt.Errorf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %w", exclude, err) } r.ignoreFunctions = append(r.ignoreFunctions, exp) diff --git a/rule/argument_limit.go b/rule/argument_limit.go index 471a0766d..43d88306b 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -2,6 +2,7 @@ package rule import ( + "errors" "fmt" "go/ast" "sync" @@ -31,7 +32,7 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { maxArguments, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - return fmt.Errorf(`invalid value passed as argument number to the "argument-limit" rule`) + return errors.New(`invalid value passed as argument number to the "argument-limit" rule`) } r.max = int(maxArguments) return nil diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index 5260beac1..a17d8afe2 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -67,7 +67,7 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { var err error r.enforceMapStyle, err = mapStyleFromString(enforceMapStyle) if err != nil { - return fmt.Errorf("Invalid argument to the enforce-map-style rule: %v", err) + return fmt.Errorf("Invalid argument to the enforce-map-style rule: %w", err) } return nil diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index f2ad37292..f09d5c6ea 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -36,7 +36,7 @@ func repeatedArgTypeStyleFromString(s string) (enforceRepeatedArgTypeStyleType, }, ) - return "", fmt.Errorf("Invalid argument to the enforce-repeated-arg-type-style rule: %v", err) + return "", fmt.Errorf("Invalid argument to the enforce-repeated-arg-type-style rule: %w", err) } } diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index f31537553..7558475fa 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -29,7 +29,7 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { case string(enforceSliceStyleTypeNil): return enforceSliceStyleTypeNil, nil default: - return "", fmt.Errorf( + return enforceSliceStyleTypeAny, fmt.Errorf( "invalid slice style: %s (expecting one of %v)", s, []enforceSliceStyleType{ diff --git a/rule/file_header.go b/rule/file_header.go index d56d31094..512791b17 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -34,7 +34,7 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error { var ok bool r.header, ok = arguments[0].(string) if !ok { - return fmt.Errorf("invalid argument for \"file-header\" rule: argument should be a string, got %T", arguments[0]) + return fmt.Errorf(`invalid argument for "file-header" rule: argument should be a string, got %T`, arguments[0]) } return nil } diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index e06c99d1a..fbea553ea 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -48,7 +48,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ( } if err := scanner.Err(); err != nil { - return nil, fmt.Errorf(err.Error()) + return nil, err } lines := all diff --git a/rule/filename_format.go b/rule/filename_format.go index f60db09b5..c357deecc 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -85,7 +85,7 @@ func (r *FilenameFormatRule) configure(arguments lint.Arguments) error { format, err := regexp.Compile(str) if err != nil { - return fmt.Errorf("rule %q expects a valid regexp argument, got %v for %s", r.Name(), err, arg) + return fmt.Errorf("rule %q expects a valid regexp argument, got %w for %s", r.Name(), err, arg) } r.format = format diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 866aef197..5d2c020b6 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -2,6 +2,7 @@ package rule import ( + "errors" "fmt" "go/ast" "sync" @@ -34,7 +35,7 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { return fmt.Errorf(`invalid value passed as return results number to the "function-result-limit" rule; need int64 but got %T`, arguments[0]) } if maxResults < 0 { - return fmt.Errorf(`the value passed as return results number to the "function-result-limit" rule cannot be negative`) + return errors.New(`the value passed as return results number to the "function-result-limit" rule cannot be negative`) } r.max = int(maxResults) diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 5984ddd92..6b68819c1 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -31,7 +31,7 @@ func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) error { } regStr, err := regexp.Compile(fmt.Sprintf(`(?m)"%s"$`, replaceImportRegexp.ReplaceAllString(argStr, `(\W|\w)*`))) if err != nil { - return fmt.Errorf("Invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %v", argStr, err) + return fmt.Errorf("Invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %w", argStr, err) } r.blocklist = append(r.blocklist, regStr) } diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index c4a111d00..83cff67f9 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -4,6 +4,7 @@ package rule import ( "bufio" "bytes" + "errors" "fmt" "go/token" "strings" @@ -35,7 +36,7 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { maxLength, ok := arguments[0].(int64) // Alt. non panicking version if !ok || maxLength < 0 { - return fmt.Errorf(`invalid value passed as argument number to the "line-length-limit" rule`) + return errors.New(`invalid value passed as argument number to the "line-length-limit" rule`) } r.max = int(maxLength) diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index d2c140e0e..84c5f5e31 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -2,6 +2,7 @@ package rule import ( + "errors" "fmt" "go/ast" "sync" @@ -128,7 +129,7 @@ func (r *MaxControlNestingRule) configure(arguments lint.Arguments) error { maxNesting, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - return fmt.Errorf(`invalid value passed as argument number to the "max-control-nesting" rule`) + return errors.New(`invalid value passed as argument number to the "max-control-nesting" rule`) } r.max = maxNesting return nil diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 20643c922..fc4612d62 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -2,6 +2,7 @@ package rule import ( + "errors" "fmt" "go/ast" "strings" @@ -37,7 +38,7 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { maxStructs, ok := arguments[0].(int64) // Alt. non panicking version if !ok { - return fmt.Errorf(`invalid value passed as argument number to the "max-public-structs" rule`) + return errors.New(`invalid value passed as argument number to the "max-public-structs" rule`) } r.max = maxStructs return nil diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index fd68ae38a..f97957769 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -2,6 +2,7 @@ package rule import ( + "errors" "fmt" "go/ast" "sync" @@ -33,7 +34,7 @@ func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { args, ok := arguments[0].(map[string]any) if !ok { - return fmt.Errorf("Unable to get arguments. Expected object of key-value-pairs") + return errors.New("Unable to get arguments. Expected object of key-value-pairs") } for k, v := range args { diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 74c279533..5353b7593 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -2,6 +2,7 @@ package rule import ( + "errors" "fmt" "go/ast" "go/types" @@ -34,12 +35,12 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { argStr = strings.Trim(argStr, " ") if argStr == "" { - return fmt.Errorf("Invalid argument to the unhandled-error rule, expected regular expression must not be empty") + return errors.New("Invalid argument to the unhandled-error rule, expected regular expression must not be empty") } exp, err := regexp.Compile(argStr) if err != nil { - return fmt.Errorf("Invalid argument to the unhandled-error rule: regexp %q does not compile: %v", argStr, err) + return fmt.Errorf("Invalid argument to the unhandled-error rule: regexp %q does not compile: %w", argStr, err) } r.ignoreList = append(r.ignoreList, exp) diff --git a/rule/unused_param.go b/rule/unused_param.go index f9511bfc8..57bc55297 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -49,7 +49,7 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { var err error r.allowRegex, err = regexp.Compile(allowedRegexStr) if err != nil { - return fmt.Errorf("error configuring %s rule: allowedRegex is not valid regex [%s]: %v", r.Name(), allowedRegexStr, err) + return fmt.Errorf("error configuring %s rule: allowedRegex is not valid regex [%s]: %w", r.Name(), allowedRegexStr, err) } if r.failureMsg == "" { diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 61a7fa257..0077cdb4f 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -49,7 +49,7 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { var err error r.allowRegex, err = regexp.Compile(allowedRegexStr) if err != nil { - return fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not valid regex [%s]: %v", allowedRegexStr, err) + return fmt.Errorf("error configuring [unused-receiver] rule: allowedRegex is not valid regex [%s]: %w", allowedRegexStr, 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() From c68d9087e3435140784ba5131ab8c5c00fc8b0c7 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 16 Nov 2024 18:28:03 +0100 Subject: [PATCH 06/36] package.go: revert to master version --- lint/package.go | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/lint/package.go b/lint/package.go index 1f1313838..64935dc8b 100644 --- a/lint/package.go +++ b/lint/package.go @@ -1,7 +1,6 @@ package lint import ( - "context" "go/ast" "go/importer" "go/token" @@ -186,37 +185,14 @@ func (p *Package) scanSortable() { func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error { p.scanSortable() var wg sync.WaitGroup - errCh := make(chan error, 1) // Buffered channel to signal the first error - _, cancel := context.WithCancel(context.Background()) - - defer cancel() // Ensure the context is canceled at the end - for _, file := range p.files { wg.Add(1) go (func(file *File) { - defer wg.Done() - if err := file.lint(rules, config, failures); err != nil { - select { - case errCh <- err: // Send the error to the channel if it's empty - cancel() // Signal other goroutines to stop - default: - // If an error is already sent, do nothing - } - } + file.lint(rules, config, failures) + wg.Done() })(file) } - - // Wait for all goroutines to complete - go func() { - wg.Wait() - close(errCh) - }() - - // Return the first error if any - if err, ok := <-errCh; ok { - return err - } - + wg.Wait() return nil } From f5e3a0e1490570a3dcad193c3afda01210a448ed Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 16 Nov 2024 23:55:44 +0100 Subject: [PATCH 07/36] fix lack of variables after merge with master --- rule/comments_density.go | 2 +- rule/context_as_argument.go | 11 ++++++++--- rule/defer.go | 12 ++++++------ rule/dot_imports.go | 2 +- rule/enforce_map_style.go | 2 +- rule/receiver_naming.go | 2 +- rule/unchecked_type_assertion.go | 4 ++-- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/rule/comments_density.go b/rule/comments_density.go index 6607300d1..b54c9f8f7 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -34,7 +34,7 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configureOnce.Do(func() { r.configure(arguments) }) commentsLines := countDocLines(file.AST.Comments) diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index db5faa762..bdda01764 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -31,11 +31,16 @@ func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) ([]l ast.Walk(walker, file.AST) - return failures,nil + return failures, nil } -func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) { - r.allowTypesLUT = getAllowTypesFromArguments(arguments) +func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) error { + types, err := getAllowTypesFromArguments(arguments) + if err != nil { + return err + } + r.allowTypesLUT = types + return nil } // Name returns the rule name. diff --git a/rule/defer.go b/rule/defer.go index ccd4de612..0caa0ef06 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -16,13 +16,13 @@ type DeferRule struct { configureOnce sync.Once } -func (r *DeferRule) configure(arguments lint.Arguments) { +func (r *DeferRule) configure(arguments lint.Arguments) error { list, err := r.allowFromArgs(arguments) - if err != nil { - return err - } - r.allow = list - return nil + if err != nil { + return err + } + r.allow = list + return nil } // Apply applies the rule to given file. diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 0477b1c8e..6d4a345ec 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -42,7 +42,7 @@ func (*DotImportsRule) Name() string { return "dot-imports" } -func (r *DotImportsRule) configure(arguments lint.Arguments) { +func (r *DotImportsRule) configure(arguments lint.Arguments) error { r.allowedPackages = allowPackages{} if len(arguments) == 0 { return nil diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index 01e884e73..e5a86e054 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -73,7 +73,7 @@ func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ( // this linter is not configured return nil, nil } - + var failures []lint.Failure astFile := file.AST ast.Inspect(astFile, func(n ast.Node) bool { switch v := n.(type) { diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 30cc66bcf..98cf7aa29 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -47,7 +47,7 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(args) }) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index 20cfad121..ede606932 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -22,7 +22,7 @@ type UncheckedTypeAssertionRule struct { configureOnce sync.Once } -func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) { +func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { if len(arguments) == 0 { return nil } @@ -48,7 +48,7 @@ func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(args) }) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure From 5c98306446733d40fbcd63ecbd05045266c88be3 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 17 Nov 2024 03:26:36 +0100 Subject: [PATCH 08/36] handle configureErr in Apply func --- lint/package.go | 8 +++++++- rule/add_constant.go | 10 +++++++--- rule/argument_limit.go | 10 +++++++--- rule/banned_characters.go | 25 +++++++++++++++---------- rule/cognitive_complexity.go | 7 ++++++- rule/comment_spacings.go | 7 ++++++- rule/comments_density.go | 7 ++++++- rule/context_as_argument.go | 9 +++++++-- rule/cyclomatic.go | 7 ++++++- rule/defer.go | 7 ++++++- rule/dot_imports.go | 7 ++++++- rule/enforce_map_style.go | 7 ++++++- rule/enforce_repeated_arg_type_style.go | 9 +++++++-- rule/enforce_slice_style.go | 7 ++++++- rule/error_strings.go | 7 ++++++- rule/exported.go | 7 ++++++- rule/file_header.go | 7 ++++++- rule/file_length_limit.go | 7 ++++++- rule/filename_format.go | 7 ++++++- rule/function_length.go | 13 +++++++++---- rule/function_result_limit.go | 7 ++++++- rule/import_alias_naming.go | 7 ++++++- rule/imports_blocklist.go | 7 ++++++- rule/line_length_limit.go | 10 +++++++--- rule/max_control_nesting.go | 7 ++++++- rule/max_public_structs.go | 10 +++++++--- rule/receiver_naming.go | 7 ++++++- rule/struct_tag.go | 7 ++++++- rule/superfluous_else.go | 1 + rule/unchecked_type_assertion.go | 7 ++++++- rule/unhandled_error.go | 7 ++++++- rule/unused_param.go | 8 +++++++- rule/unused_receiver.go | 8 +++++++- rule/var_naming.go | 7 ++++++- 34 files changed, 220 insertions(+), 55 deletions(-) diff --git a/lint/package.go b/lint/package.go index 737c71be4..f471131be 100644 --- a/lint/package.go +++ b/lint/package.go @@ -1,10 +1,12 @@ package lint import ( + "fmt" "go/ast" "go/importer" "go/token" "go/types" + "os" "sync" goversion "github.com/hashicorp/go-version" @@ -188,7 +190,11 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error for _, file := range p.files { wg.Add(1) go (func(file *File) { - file.lint(rules, config, failures) + err := file.lint(rules, config, failures) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } wg.Done() })(file) } diff --git a/rule/add_constant.go b/rule/add_constant.go index bdb63fe85..7116ff323 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -38,13 +38,17 @@ type AddConstantRule struct { allowList allowList ignoreFunctions []*regexp.Regexp strLitLimit int - - configureOnce sync.Once + configureErr error + configureOnce sync.Once } // Apply applies the rule to given file. func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/argument_limit.go b/rule/argument_limit.go index 776644011..d86eb7409 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -12,8 +12,8 @@ import ( // ArgumentsLimitRule lints given else constructs. type ArgumentsLimitRule struct { - max int - + max int + configureErr error configureOnce sync.Once } @@ -35,7 +35,11 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 8d29a35cc..3d7885a85 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -14,6 +14,7 @@ import ( type BannedCharsRule struct { bannedCharList []string + configureErr error configureOnce sync.Once } @@ -22,22 +23,26 @@ const bannedCharsRuleName = "banned-characters" func (r *BannedCharsRule) configure(arguments lint.Arguments) error { if len(arguments) > 0 { check := checkNumberOfArguments(1, arguments, bannedCharsRuleName) - if check != nil { - return check - } - list, err := r.getBannedCharsList(arguments) - if err != nil { - return err - } - - r.bannedCharList = list + if check != nil { + return check + } + list, err := r.getBannedCharsList(arguments) + if err != nil { + return err + } + + r.bannedCharList = list } return nil } // Apply applied the rule to the given file. func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index ccfb77e56..a27d78c1a 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -15,6 +15,7 @@ import ( type CognitiveComplexityRule struct { maxComplexity int + configureErr error configureOnce sync.Once } @@ -37,7 +38,11 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index a96616125..0209c399f 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -14,6 +14,7 @@ import ( type CommentSpacingsRule struct { allowList []string + configureErr error configureOnce sync.Once } @@ -31,7 +32,11 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { // Apply the rule. func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/comments_density.go b/rule/comments_density.go index b54c9f8f7..7cae10858 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -14,6 +14,7 @@ import ( type CommentsDensityRule struct { minimumCommentsDensity int64 + configureErr error configureOnce sync.Once } @@ -35,7 +36,11 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } commentsLines := countDocLines(file.AST.Comments) statementsCount := countStatements(file.AST) diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index bdda01764..ecc100790 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -14,12 +14,17 @@ import ( type ContextAsArgumentRule struct { allowTypesLUT map[string]struct{} + configureErr error configureOnce sync.Once } // Apply applies the rule to given file. -func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(args) }) +func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure walker := lintContextArguments{ diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index b974fe2a4..33f7769dd 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -16,6 +16,7 @@ import ( type CyclomaticRule struct { maxComplexity int + configureErr error configureOnce sync.Once } @@ -37,7 +38,11 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure fileAst := file.AST diff --git a/rule/defer.go b/rule/defer.go index 0caa0ef06..c4d841aaa 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -13,6 +13,7 @@ import ( type DeferRule struct { allow map[string]bool + configureErr error configureOnce sync.Once } @@ -27,7 +28,11 @@ func (r *DeferRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 6d4a345ec..1a939da17 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -13,12 +13,17 @@ import ( type DotImportsRule struct { allowedPackages allowPackages + configureErr error configureOnce sync.Once } // Apply applies the rule to given file. func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index e5a86e054..3e6816ebc 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -42,6 +42,7 @@ func mapStyleFromString(s string) (enforceMapStyleType, error) { type EnforceMapStyleRule struct { enforceMapStyle enforceMapStyleType + configureErr error configureOnce sync.Once } @@ -67,7 +68,11 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } if r.enforceMapStyle == enforceMapStyleTypeAny { // this linter is not configured diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index 4b27ed520..57fbe5dc3 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -45,6 +45,7 @@ type EnforceRepeatedArgTypeStyleRule struct { funcArgStyle enforceRepeatedArgTypeStyleType funcRetValStyle enforceRepeatedArgTypeStyleType + configureErr error configureOnce sync.Once } @@ -103,14 +104,18 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er // Apply applies the rule to a given file. func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. return nil, nil } - var failures []lint.Failure + var failures []lint.Failure astFile := file.AST ast.Inspect(astFile, func(n ast.Node) bool { diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index ee6a3f74b..8180116bc 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -46,6 +46,7 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { type EnforceSliceStyleRule struct { enforceSliceStyle enforceSliceStyleType + configureErr error configureOnce sync.Once } @@ -70,7 +71,11 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } if r.enforceSliceStyle == enforceSliceStyleTypeAny { // this linter is not configured diff --git a/rule/error_strings.go b/rule/error_strings.go index 5fc642cbf..5b568fc56 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -18,6 +18,7 @@ import ( type ErrorStringsRule struct { errorFunctions map[string]map[string]struct{} + configureErr error configureOnce sync.Once } @@ -57,7 +58,11 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } fileAst := file.AST walker := lintErrorStrings{ diff --git a/rule/exported.go b/rule/exported.go index 83f88c824..f188d7a93 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -59,6 +59,7 @@ type ExportedRule struct { stuttersMsg string disabledChecks disabledChecks + configureErr error configureOnce sync.Once } @@ -100,7 +101,11 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure if file.IsTest() { diff --git a/rule/file_header.go b/rule/file_header.go index 596f8cb17..de8fc7408 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -13,6 +13,7 @@ import ( type FileHeaderRule struct { header string + configureErr error configureOnce sync.Once } @@ -36,7 +37,11 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } if r.header == "" { return nil, nil diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index 3d0604ee2..b2f71fd60 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -22,12 +22,17 @@ type FileLengthLimitRule struct { // skipBlankLines indicates whether to skip blank lines when counting lines. skipBlankLines bool + configureErr error configureOnce sync.Once } // Apply applies the rule to given file. func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } if r.max <= 0 { // when max is negative or 0 the rule is disabled diff --git a/rule/filename_format.go b/rule/filename_format.go index 07eb8b0ea..ac184916c 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -15,12 +15,17 @@ import ( type FilenameFormatRule struct { format *regexp.Regexp + configureErr error configureOnce sync.Once } // Apply applies the rule to the given file. func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } filename := filepath.Base(file.Name) if r.format.MatchString(filename) { diff --git a/rule/function_length.go b/rule/function_length.go index 21cb9104e..3aa963635 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -15,14 +15,15 @@ type FunctionLength struct { maxStmt int maxLines int + configureErr error configureOnce sync.Once } func (r *FunctionLength) configure(arguments lint.Arguments) error { maxStmt, maxLines, err := r.parseArguments(arguments) - if err != nil { - return err - } + if err != nil { + return err + } r.maxStmt = int(maxStmt) r.maxLines = int(maxLines) return nil @@ -30,7 +31,11 @@ func (r *FunctionLength) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index aa8c2bbe5..5e2743c58 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -14,6 +14,7 @@ import ( type FunctionResultsLimitRule struct { max int + configureErr error configureOnce sync.Once } @@ -39,7 +40,11 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 43c0fa5a4..c448ba79f 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -14,6 +14,7 @@ type ImportAliasNamingRule struct { allowRegexp *regexp.Regexp denyRegexp *regexp.Regexp + configureErr error configureOnce sync.Once } @@ -63,7 +64,11 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 3c81455c9..ab34882aa 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -13,6 +13,7 @@ import ( type ImportsBlocklistRule struct { blocklist []*regexp.Regexp + configureErr error configureOnce sync.Once } @@ -45,7 +46,11 @@ func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { // Apply applies the rule to given file. func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index b392b6057..d82a32c0d 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -16,8 +16,8 @@ import ( // LineLengthLimitRule lints given else constructs. type LineLengthLimitRule struct { - max int - + max int + configureErr error configureOnce sync.Once } @@ -40,7 +40,11 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index ebb4687d8..3ad532d3e 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -14,6 +14,7 @@ import ( type MaxControlNestingRule struct { max int64 + configureErr error configureOnce sync.Once } @@ -21,7 +22,11 @@ const defaultMaxControlNesting = 5 // Apply applies the rule to given file. func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 2f56dea8d..6fae4a682 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -13,8 +13,8 @@ import ( // MaxPublicStructsRule lints given else constructs. type MaxPublicStructsRule struct { - max int64 - + max int64 + configureErr error configureOnce sync.Once } @@ -41,7 +41,11 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 98cf7aa29..87973294e 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -14,6 +14,7 @@ import ( type ReceiverNamingRule struct { receiverNameMaxLength int + configureErr error configureOnce sync.Once } @@ -47,7 +48,11 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 626395279..d084156b4 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -16,6 +16,7 @@ import ( type StructTagRule struct { userDefined map[string][]string // map: key -> []option + configureErr error configureOnce sync.Once } @@ -49,7 +50,11 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/superfluous_else.go b/rule/superfluous_else.go index 0a24cf0ab..cc89b08cd 100644 --- a/rule/superfluous_else.go +++ b/rule/superfluous_else.go @@ -1,3 +1,4 @@ +// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index ede606932..da0ad4dc0 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -19,6 +19,7 @@ const ( type UncheckedTypeAssertionRule struct { acceptIgnoredAssertionResult bool + configureErr error configureOnce sync.Once } @@ -48,7 +49,11 @@ func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index c2f1e4597..d8b9dc46a 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -17,6 +17,7 @@ import ( type UnhandledErrorRule struct { ignoreList []*regexp.Regexp + configureErr error configureOnce sync.Once } @@ -44,7 +45,11 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure diff --git a/rule/unused_param.go b/rule/unused_param.go index 0aaeef8ba..de869097c 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -16,6 +16,7 @@ type UnusedParamRule struct { allowRegex *regexp.Regexp failureMsg string + configureErr error configureOnce sync.Once } @@ -52,7 +53,12 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } + var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index f0182e7bf..a8f9839ca 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -16,6 +16,7 @@ type UnusedReceiverRule struct { allowRegex *regexp.Regexp failureMsg string + configureErr error configureOnce sync.Once } @@ -51,7 +52,12 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } + var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/var_naming.go b/rule/var_naming.go index 212423a15..ad0448204 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -24,6 +24,7 @@ type VarNamingRule struct { allowUpperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants skipPackageNameChecks bool + configureErr error configureOnce sync.Once } @@ -86,7 +87,11 @@ func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { // Apply applies the rule to given file. func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) + r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) + + if r.configureErr != nil { + return nil, r.configureErr + } var failures []lint.Failure From 63a0088380b7677e4cdeeb65b0898c1893dee68a Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 17 Nov 2024 15:30:09 +0100 Subject: [PATCH 09/36] replace configureErr with local variable --- rule/add_constant.go | 9 ++++----- rule/argument_limit.go | 9 ++++----- rule/banned_characters.go | 12 +++++------- rule/cognitive_complexity.go | 10 ++++------ rule/comment_spacings.go | 12 +++++------- rule/comments_density.go | 13 +++++-------- rule/context_as_argument.go | 10 ++++------ rule/cyclomatic.go | 10 ++++------ rule/defer.go | 11 ++++------- rule/dot_imports.go | 10 ++++------ rule/enforce_map_style.go | 12 +++++------- rule/enforce_repeated_arg_type_style.go | 13 +++++-------- rule/enforce_slice_style.go | 12 +++++------- rule/error_strings.go | 11 ++++------- rule/exported.go | 12 +++++------- rule/file_header.go | 12 +++++------- rule/file_length_limit.go | 12 +++++------- rule/filename_format.go | 12 +++++------- rule/function_length.go | 14 ++++++-------- rule/function_result_limit.go | 12 +++++------- rule/import_alias_naming.go | 14 ++++++-------- rule/imports_blocklist.go | 12 +++++------- rule/line_length_limit.go | 9 ++++----- rule/max_control_nesting.go | 12 +++++------- rule/max_public_structs.go | 9 ++++----- rule/receiver_naming.go | 12 +++++------- rule/struct_tag.go | 12 +++++------- rule/unchecked_type_assertion.go | 12 +++++------- rule/unhandled_error.go | 12 +++++------- rule/unused_param.go | 14 ++++++-------- rule/unused_receiver.go | 14 ++++++-------- rule/var_naming.go | 12 +++++------- 32 files changed, 154 insertions(+), 218 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index 7116ff323..a469438c3 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -38,16 +38,15 @@ type AddConstantRule struct { allowList allowList ignoreFunctions []*regexp.Regexp strLitLimit int - configureErr error configureOnce sync.Once } // Apply applies the rule to given file. func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/argument_limit.go b/rule/argument_limit.go index d86eb7409..f2c105a93 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -13,7 +13,6 @@ import ( // ArgumentsLimitRule lints given else constructs. type ArgumentsLimitRule struct { max int - configureErr error configureOnce sync.Once } @@ -35,10 +34,10 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 3d7885a85..15c79ff3e 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -13,9 +13,7 @@ import ( // BannedCharsRule checks if a file contains banned characters. type BannedCharsRule struct { bannedCharList []string - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } const bannedCharsRuleName = "banned-characters" @@ -38,10 +36,10 @@ func (r *BannedCharsRule) configure(arguments lint.Arguments) error { // Apply applied the rule to the given file. func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index a27d78c1a..c6151a599 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -14,8 +14,6 @@ import ( // CognitiveComplexityRule lints given else constructs. type CognitiveComplexityRule struct { maxComplexity int - - configureErr error configureOnce sync.Once } @@ -38,10 +36,10 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index 0209c399f..7265be360 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -12,9 +12,7 @@ import ( // CommentSpacingsRule check the whether there is a space between // the comment symbol( // ) and the start of the comment text type CommentSpacingsRule struct { - allowList []string - - configureErr error + allowList []string configureOnce sync.Once } @@ -32,10 +30,10 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { // Apply the rule. func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/comments_density.go b/rule/comments_density.go index 7cae10858..e177268b8 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -13,9 +13,7 @@ import ( // CommentsDensityRule lints given else constructs. type CommentsDensityRule struct { minimumCommentsDensity int64 - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } const defaultMinimumCommentsPercentage = 0 @@ -36,12 +34,11 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } - commentsLines := countDocLines(file.AST.Comments) statementsCount := countStatements(file.AST) density := (float32(commentsLines) / float32(statementsCount+commentsLines)) * 100 diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index ecc100790..105442748 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -13,17 +13,15 @@ import ( // ContextAsArgumentRule lints given else constructs. type ContextAsArgumentRule struct { allowTypesLUT map[string]struct{} - - configureErr error configureOnce sync.Once } // Apply applies the rule to given file. func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 33f7769dd..9cdf7e996 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -15,8 +15,6 @@ import ( // CyclomaticRule lints given else constructs. type CyclomaticRule struct { maxComplexity int - - configureErr error configureOnce sync.Once } @@ -38,10 +36,10 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/defer.go b/rule/defer.go index c4d841aaa..3caed5309 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -12,8 +12,6 @@ import ( // DeferRule lints unused params in functions. type DeferRule struct { allow map[string]bool - - configureErr error configureOnce sync.Once } @@ -28,12 +26,11 @@ func (r *DeferRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } - var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 1a939da17..468cf7f3b 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -12,17 +12,15 @@ import ( // DotImportsRule lints given else constructs. type DotImportsRule struct { allowedPackages allowPackages - - configureErr error configureOnce sync.Once } // Apply applies the rule to given file. func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index 3e6816ebc..c37289925 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -41,9 +41,7 @@ func mapStyleFromString(s string) (enforceMapStyleType, error) { // EnforceMapStyleRule implements a rule to enforce `make(map[type]type)` over `map[type]type{}`. type EnforceMapStyleRule struct { enforceMapStyle enforceMapStyleType - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { @@ -68,10 +66,10 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } if r.enforceMapStyle == enforceMapStyleTypeAny { diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index 57fbe5dc3..983629aa4 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -44,9 +44,7 @@ func repeatedArgTypeStyleFromString(s string) (enforceRepeatedArgTypeStyleType, type EnforceRepeatedArgTypeStyleRule struct { funcArgStyle enforceRepeatedArgTypeStyleType funcRetValStyle enforceRepeatedArgTypeStyleType - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) error { @@ -104,12 +102,11 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er // Apply applies the rule to a given file. func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } - if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. return nil, nil diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 8180116bc..04d44ab39 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -45,9 +45,7 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { // EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`. type EnforceSliceStyleRule struct { enforceSliceStyle enforceSliceStyleType - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { @@ -71,10 +69,10 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } if r.enforceSliceStyle == enforceSliceStyleTypeAny { diff --git a/rule/error_strings.go b/rule/error_strings.go index 5b568fc56..6c0074710 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -17,8 +17,6 @@ import ( // ErrorStringsRule lints given else constructs. type ErrorStringsRule struct { errorFunctions map[string]map[string]struct{} - - configureErr error configureOnce sync.Once } @@ -57,11 +55,10 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure - - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } fileAst := file.AST diff --git a/rule/exported.go b/rule/exported.go index f188d7a93..c50e6d683 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -58,9 +58,7 @@ func (dc *disabledChecks) isDisabled(checkName string) bool { type ExportedRule struct { stuttersMsg string disabledChecks disabledChecks - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } func (r *ExportedRule) configure(arguments lint.Arguments) error { @@ -101,10 +99,10 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/file_header.go b/rule/file_header.go index de8fc7408..b0051b98c 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -11,9 +11,7 @@ import ( // FileHeaderRule lints given else constructs. type FileHeaderRule struct { - header string - - configureErr error + header string configureOnce sync.Once } @@ -37,10 +35,10 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } if r.header == "" { diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index b2f71fd60..ed949c88c 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -21,17 +21,15 @@ type FileLengthLimitRule struct { skipComments bool // skipBlankLines indicates whether to skip blank lines when counting lines. skipBlankLines bool - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } // Apply applies the rule to given file. func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } if r.max <= 0 { diff --git a/rule/filename_format.go b/rule/filename_format.go index ac184916c..1e8cea517 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -13,18 +13,16 @@ import ( // FilenameFormatRule lints source filenames according to a set of regular expressions given as arguments type FilenameFormatRule struct { - format *regexp.Regexp - - configureErr error + format *regexp.Regexp configureOnce sync.Once } // Apply applies the rule to the given file. func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } filename := filepath.Base(file.Name) diff --git a/rule/function_length.go b/rule/function_length.go index 3aa963635..d156a74da 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -12,10 +12,8 @@ import ( // FunctionLength lint. type FunctionLength struct { - maxStmt int - maxLines int - - configureErr error + maxStmt int + maxLines int configureOnce sync.Once } @@ -31,10 +29,10 @@ func (r *FunctionLength) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 5e2743c58..1fcce30b0 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -12,9 +12,7 @@ import ( // FunctionResultsLimitRule lints given else constructs. type FunctionResultsLimitRule struct { - max int - - configureErr error + max int configureOnce sync.Once } @@ -40,10 +38,10 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index c448ba79f..fa295d5bb 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -11,10 +11,8 @@ import ( // ImportAliasNamingRule lints import alias naming. type ImportAliasNamingRule struct { - allowRegexp *regexp.Regexp - denyRegexp *regexp.Regexp - - configureErr error + allowRegexp *regexp.Regexp + denyRegexp *regexp.Regexp configureOnce sync.Once } @@ -64,10 +62,10 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index ab34882aa..f935432fe 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -11,9 +11,7 @@ import ( // ImportsBlocklistRule lints given else constructs. type ImportsBlocklistRule struct { - blocklist []*regexp.Regexp - - configureErr error + blocklist []*regexp.Regexp configureOnce sync.Once } @@ -46,10 +44,10 @@ func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { // Apply applies the rule to given file. func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index d82a32c0d..8c559572d 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -17,7 +17,6 @@ import ( // LineLengthLimitRule lints given else constructs. type LineLengthLimitRule struct { max int - configureErr error configureOnce sync.Once } @@ -40,10 +39,10 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 3ad532d3e..8f64083ee 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -12,9 +12,7 @@ import ( // MaxControlNestingRule lints given else constructs. type MaxControlNestingRule struct { - max int64 - - configureErr error + max int64 configureOnce sync.Once } @@ -22,10 +20,10 @@ const defaultMaxControlNesting = 5 // Apply applies the rule to given file. func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 6fae4a682..688fc48d8 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -14,7 +14,6 @@ import ( // MaxPublicStructsRule lints given else constructs. type MaxPublicStructsRule struct { max int64 - configureErr error configureOnce sync.Once } @@ -41,10 +40,10 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 87973294e..85121db02 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -13,9 +13,7 @@ import ( // ReceiverNamingRule lints given else constructs. type ReceiverNamingRule struct { receiverNameMaxLength int - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } const defaultReceiverNameMaxLength = -1 // thus will not check @@ -48,10 +46,10 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/struct_tag.go b/rule/struct_tag.go index d084156b4..6eece838b 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -14,9 +14,7 @@ import ( // StructTagRule lints struct tags. type StructTagRule struct { - userDefined map[string][]string // map: key -> []option - - configureErr error + userDefined map[string][]string // map: key -> []option configureOnce sync.Once } @@ -50,10 +48,10 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index da0ad4dc0..839035e01 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -18,9 +18,7 @@ const ( // UncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. type UncheckedTypeAssertionRule struct { acceptIgnoredAssertionResult bool - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { @@ -49,10 +47,10 @@ func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index d8b9dc46a..69a368327 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -15,9 +15,7 @@ import ( // UnhandledErrorRule lints given else constructs. type UnhandledErrorRule struct { - ignoreList []*regexp.Regexp - - configureErr error + ignoreList []*regexp.Regexp configureOnce sync.Once } @@ -45,10 +43,10 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/unused_param.go b/rule/unused_param.go index de869097c..036f81e6c 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -13,10 +13,8 @@ import ( // UnusedParamRule lints unused params in functions. type UnusedParamRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default - allowRegex *regexp.Regexp - failureMsg string - - configureErr error + allowRegex *regexp.Regexp + failureMsg string configureOnce sync.Once } @@ -53,10 +51,10 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index a8f9839ca..ea6cfd683 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -13,10 +13,8 @@ import ( // UnusedReceiverRule lints unused params in functions. type UnusedReceiverRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default - allowRegex *regexp.Regexp - failureMsg string - - configureErr error + allowRegex *regexp.Regexp + failureMsg string configureOnce sync.Once } @@ -52,10 +50,10 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure diff --git a/rule/var_naming.go b/rule/var_naming.go index ad0448204..c25461673 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -23,9 +23,7 @@ type VarNamingRule struct { blockList []string allowUpperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants skipPackageNameChecks bool - - configureErr error - configureOnce sync.Once + configureOnce sync.Once } func (r *VarNamingRule) configure(arguments lint.Arguments) error { @@ -87,10 +85,10 @@ func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { // Apply applies the rule to given file. func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - - if r.configureErr != nil { - return nil, r.configureErr + var configErr error + r.configureOnce.Do(func() { configErr = r.configure(arguments) }) + if configErr != nil { + return nil, configErr } var failures []lint.Failure From d47112bbd84d5991912da292cd1307d9fd96f036 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Mon, 18 Nov 2024 22:54:42 +0100 Subject: [PATCH 10/36] remove package comment, except doc.go --- rule/argument_limit.go | 1 - rule/atomic.go | 1 - rule/banned_characters.go | 1 - rule/bare_return.go | 1 - rule/blank_imports.go | 1 - rule/bool_literal_in_expr.go | 1 - rule/call_to_gc.go | 1 - rule/cognitive_complexity.go | 1 - rule/comment_spacings.go | 1 - rule/comments_density.go | 1 - rule/confusing_naming.go | 1 - rule/confusing_results.go | 1 - rule/constant_logical_expr.go | 1 - rule/context_as_argument.go | 1 - rule/context_keys_type.go | 1 - rule/cyclomatic.go | 1 - rule/datarace.go | 1 - rule/deep_exit.go | 1 - rule/defer.go | 1 - rule/dot_imports.go | 1 - rule/duplicated_imports.go | 1 - rule/early_return.go | 1 - rule/empty_block.go | 1 - rule/empty_lines.go | 1 - rule/enforce_map_style.go | 1 - rule/enforce_repeated_arg_type_style.go | 1 - rule/enforce_slice_style.go | 1 - rule/error_naming.go | 1 - rule/error_return.go | 1 - rule/error_strings.go | 1 - rule/errorf.go | 1 - rule/exported.go | 1 - rule/file_header.go | 1 - rule/file_length_limit.go | 1 - rule/filename_format.go | 1 - rule/flag_param.go | 1 - rule/function_length.go | 1 - rule/function_result_limit.go | 1 - rule/get_return.go | 1 - rule/identical_branches.go | 1 - rule/if_return.go | 1 - rule/import_alias_naming.go | 1 - rule/import_shadowing.go | 1 - rule/imports_blocklist.go | 1 - rule/increment_decrement.go | 1 - rule/indent_error_flow.go | 1 - rule/line_length_limit.go | 1 - rule/max_control_nesting.go | 1 - rule/max_public_structs.go | 1 - rule/modifies_param.go | 1 - rule/modifies_value_receiver.go | 1 - rule/nested_structs.go | 1 - rule/optimize_operands_order.go | 1 - rule/package_comments.go | 1 - rule/range.go | 1 - rule/range_val_address.go | 1 - rule/range_val_in_closure.go | 1 - rule/receiver_naming.go | 1 - rule/redefines_builtin_id.go | 1 - rule/redundant_import_alias.go | 1 - rule/string_format.go | 1 - rule/string_of_int.go | 1 - rule/struct_tag.go | 1 - rule/superfluous_else.go | 1 - rule/time_equal.go | 1 - rule/time_naming.go | 1 - rule/unchecked_type_assertion.go | 1 - rule/unconditional_recursion.go | 1 - rule/unexported_naming.go | 1 - rule/unexported_return.go | 1 - rule/unhandled_error.go | 1 - rule/unreachable_code.go | 1 - rule/unused_param.go | 1 - rule/unused_receiver.go | 1 - rule/use_any.go | 1 - rule/useless_break.go | 1 - rule/utils.go | 1 - rule/var_naming.go | 1 - rule/waitgroup_by_value.go | 1 - 79 files changed, 79 deletions(-) diff --git a/rule/argument_limit.go b/rule/argument_limit.go index f2c105a93..3f5834541 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/atomic.go b/rule/atomic.go index 45c9c43d4..48cf55679 100644 --- a/rule/atomic.go +++ b/rule/atomic.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 15c79ff3e..7aadec457 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/bare_return.go b/rule/bare_return.go index 6cc29754a..0f6fdf55b 100644 --- a/rule/bare_return.go +++ b/rule/bare_return.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/blank_imports.go b/rule/blank_imports.go index fc09e26a9..396ff6efe 100644 --- a/rule/blank_imports.go +++ b/rule/blank_imports.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/bool_literal_in_expr.go b/rule/bool_literal_in_expr.go index 74fc281e3..dd51d951e 100644 --- a/rule/bool_literal_in_expr.go +++ b/rule/bool_literal_in_expr.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/call_to_gc.go b/rule/call_to_gc.go index cfaae6d1c..ca4a00704 100644 --- a/rule/call_to_gc.go +++ b/rule/call_to_gc.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index c6151a599..7e16b26af 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index 7265be360..6e5595b45 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/comments_density.go b/rule/comments_density.go index e177268b8..e96c78c12 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/confusing_naming.go b/rule/confusing_naming.go index 02754db82..2cdcbd458 100644 --- a/rule/confusing_naming.go +++ b/rule/confusing_naming.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/confusing_results.go b/rule/confusing_results.go index 3935b22f4..9d756b52a 100644 --- a/rule/confusing_results.go +++ b/rule/confusing_results.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/constant_logical_expr.go b/rule/constant_logical_expr.go index bbf918352..aa4d54159 100644 --- a/rule/constant_logical_expr.go +++ b/rule/constant_logical_expr.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index 105442748..f876b8b7c 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/context_keys_type.go b/rule/context_keys_type.go index bb4105125..3598f791a 100644 --- a/rule/context_keys_type.go +++ b/rule/context_keys_type.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 9cdf7e996..88958cdf6 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/datarace.go b/rule/datarace.go index 3635f700f..8a4a626fb 100644 --- a/rule/datarace.go +++ b/rule/datarace.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/deep_exit.go b/rule/deep_exit.go index d2f132ad5..9ae9d54a4 100644 --- a/rule/deep_exit.go +++ b/rule/deep_exit.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/defer.go b/rule/defer.go index 3caed5309..8c08e9e34 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 468cf7f3b..6d935ae3d 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/duplicated_imports.go b/rule/duplicated_imports.go index a3e62b606..ae868be9a 100644 --- a/rule/duplicated_imports.go +++ b/rule/duplicated_imports.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/early_return.go b/rule/early_return.go index 8ef23a333..41ab9a85d 100644 --- a/rule/early_return.go +++ b/rule/early_return.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/empty_block.go b/rule/empty_block.go index defd1097c..d75069d94 100644 --- a/rule/empty_block.go +++ b/rule/empty_block.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/empty_lines.go b/rule/empty_lines.go index 7630c58c7..0c904d034 100644 --- a/rule/empty_lines.go +++ b/rule/empty_lines.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index c37289925..8a0951163 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index 983629aa4..7e7291822 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 04d44ab39..2ea3db076 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/error_naming.go b/rule/error_naming.go index 488c77114..d57f453ef 100644 --- a/rule/error_naming.go +++ b/rule/error_naming.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/error_return.go b/rule/error_return.go index 7ec73efce..89cc5c74a 100644 --- a/rule/error_return.go +++ b/rule/error_return.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/error_strings.go b/rule/error_strings.go index 6c0074710..07e1d5c29 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/errorf.go b/rule/errorf.go index 6e05864af..63081c14a 100644 --- a/rule/errorf.go +++ b/rule/errorf.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/exported.go b/rule/exported.go index c50e6d683..98204196a 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/file_header.go b/rule/file_header.go index b0051b98c..6051a3cd2 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index ed949c88c..b8a70b0f3 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/filename_format.go b/rule/filename_format.go index 1e8cea517..76364c6c6 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/flag_param.go b/rule/flag_param.go index 0d917a27c..efeaff897 100644 --- a/rule/flag_param.go +++ b/rule/flag_param.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/function_length.go b/rule/function_length.go index d156a74da..760c0f18a 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 1fcce30b0..739fcb4ac 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/get_return.go b/rule/get_return.go index ba98fe353..8ec6fe93a 100644 --- a/rule/get_return.go +++ b/rule/get_return.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/identical_branches.go b/rule/identical_branches.go index 32ca9bc70..4144c44cb 100644 --- a/rule/identical_branches.go +++ b/rule/identical_branches.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/if_return.go b/rule/if_return.go index 49f1b3557..8ba55cb45 100644 --- a/rule/if_return.go +++ b/rule/if_return.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index fa295d5bb..c867aee84 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/import_shadowing.go b/rule/import_shadowing.go index a3c5b098e..398c5cf87 100644 --- a/rule/import_shadowing.go +++ b/rule/import_shadowing.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index f935432fe..4581f28f4 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/increment_decrement.go b/rule/increment_decrement.go index fb9330fa6..75f8aba66 100644 --- a/rule/increment_decrement.go +++ b/rule/increment_decrement.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/indent_error_flow.go b/rule/indent_error_flow.go index ba08b044e..2e585ddf4 100644 --- a/rule/indent_error_flow.go +++ b/rule/indent_error_flow.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index 8c559572d..2ee42f04f 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 8f64083ee..0672a6223 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 688fc48d8..eaa58e334 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/modifies_param.go b/rule/modifies_param.go index 7e804b716..365c7b697 100644 --- a/rule/modifies_param.go +++ b/rule/modifies_param.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/modifies_value_receiver.go b/rule/modifies_value_receiver.go index 37404631a..085c07c2e 100644 --- a/rule/modifies_value_receiver.go +++ b/rule/modifies_value_receiver.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/nested_structs.go b/rule/nested_structs.go index 8ca897cb2..c58c089c8 100644 --- a/rule/nested_structs.go +++ b/rule/nested_structs.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/optimize_operands_order.go b/rule/optimize_operands_order.go index f174a5c9d..edc23110f 100644 --- a/rule/optimize_operands_order.go +++ b/rule/optimize_operands_order.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/package_comments.go b/rule/package_comments.go index d573f874e..afa17912a 100644 --- a/rule/package_comments.go +++ b/rule/package_comments.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/range.go b/rule/range.go index 0cf06dfdd..1ad224588 100644 --- a/rule/range.go +++ b/rule/range.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/range_val_address.go b/rule/range_val_address.go index bbe6135e9..327c5afbb 100644 --- a/rule/range_val_address.go +++ b/rule/range_val_address.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/range_val_in_closure.go b/rule/range_val_in_closure.go index 794895ef8..aa79a0135 100644 --- a/rule/range_val_in_closure.go +++ b/rule/range_val_in_closure.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 85121db02..33e382b32 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/redefines_builtin_id.go b/rule/redefines_builtin_id.go index ec1d44ca7..097432504 100644 --- a/rule/redefines_builtin_id.go +++ b/rule/redefines_builtin_id.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/redundant_import_alias.go b/rule/redundant_import_alias.go index 4016ef484..b5fd289f7 100644 --- a/rule/redundant_import_alias.go +++ b/rule/redundant_import_alias.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/string_format.go b/rule/string_format.go index 6738a6316..55027d823 100644 --- a/rule/string_format.go +++ b/rule/string_format.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/string_of_int.go b/rule/string_of_int.go index e9049a02c..a01017b69 100644 --- a/rule/string_of_int.go +++ b/rule/string_of_int.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 6eece838b..35ad80b0f 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/superfluous_else.go b/rule/superfluous_else.go index cc89b08cd..0a24cf0ab 100644 --- a/rule/superfluous_else.go +++ b/rule/superfluous_else.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/time_equal.go b/rule/time_equal.go index d2384f2cf..4aa8bc384 100644 --- a/rule/time_equal.go +++ b/rule/time_equal.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/time_naming.go b/rule/time_naming.go index 151f700e6..bb570c60b 100644 --- a/rule/time_naming.go +++ b/rule/time_naming.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index 839035e01..d90cba333 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unconditional_recursion.go b/rule/unconditional_recursion.go index 16c3b3f14..7d5e03404 100644 --- a/rule/unconditional_recursion.go +++ b/rule/unconditional_recursion.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unexported_naming.go b/rule/unexported_naming.go index 9c6165b89..7c95a61c1 100644 --- a/rule/unexported_naming.go +++ b/rule/unexported_naming.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unexported_return.go b/rule/unexported_return.go index 05ef1d1d8..0260fa8c2 100644 --- a/rule/unexported_return.go +++ b/rule/unexported_return.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 69a368327..a5266f3d0 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unreachable_code.go b/rule/unreachable_code.go index 8c3e7518a..868a0d755 100644 --- a/rule/unreachable_code.go +++ b/rule/unreachable_code.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unused_param.go b/rule/unused_param.go index 036f81e6c..61b4015e9 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index ea6cfd683..43387a013 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/use_any.go b/rule/use_any.go index 538a0b1c5..48abdc100 100644 --- a/rule/use_any.go +++ b/rule/use_any.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/useless_break.go b/rule/useless_break.go index 2223f4993..b65a3fe99 100644 --- a/rule/useless_break.go +++ b/rule/useless_break.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/utils.go b/rule/utils.go index 991ae858e..2e296ac33 100644 --- a/rule/utils.go +++ b/rule/utils.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/var_naming.go b/rule/var_naming.go index c25461673..de5447014 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( diff --git a/rule/waitgroup_by_value.go b/rule/waitgroup_by_value.go index f43d7303e..27bc56bff 100644 --- a/rule/waitgroup_by_value.go +++ b/rule/waitgroup_by_value.go @@ -1,4 +1,3 @@ -// Package rule implements revive's linting rules. package rule import ( From fd97ee3eddd0af6bc19f0f266778f8d3a4c01e21 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Mon, 18 Nov 2024 23:25:55 +0100 Subject: [PATCH 11/36] revert new line above:configureOnce --- rule/add_constant.go | 2 +- rule/argument_limit.go | 3 ++- rule/cognitive_complexity.go | 1 + rule/comment_spacings.go | 3 ++- rule/context_as_argument.go | 1 + rule/cyclomatic.go | 1 + rule/defer.go | 1 + rule/dot_imports.go | 1 + rule/error_strings.go | 1 + rule/file_header.go | 3 ++- rule/filename_format.go | 3 ++- rule/function_length.go | 5 +++-- rule/function_result_limit.go | 3 ++- rule/import_alias_naming.go | 5 +++-- rule/imports_blocklist.go | 1 + rule/line_length_limit.go | 3 ++- rule/max_control_nesting.go | 3 ++- rule/max_public_structs.go | 3 ++- rule/struct_tag.go | 1 + rule/unhandled_error.go | 1 + rule/unused_param.go | 5 +++-- rule/unused_receiver.go | 5 +++-- 22 files changed, 38 insertions(+), 17 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index 6686e3efa..6fc4f2f8b 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -210,7 +210,7 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) error { r.strLitLimit = defaultStrLitLimit r.allowList = newAllowList() if len(arguments) == 0 { - return + return nil } args, ok := arguments[0].(map[string]any) if !ok { diff --git a/rule/argument_limit.go b/rule/argument_limit.go index 3f5834541..69a6b52af 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -11,7 +11,8 @@ import ( // ArgumentsLimitRule lints given else constructs. type ArgumentsLimitRule struct { - max int + max int + configureOnce sync.Once } diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index 7e16b26af..c4bd1b52f 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -13,6 +13,7 @@ import ( // CognitiveComplexityRule lints given else constructs. type CognitiveComplexityRule struct { maxComplexity int + configureOnce sync.Once } diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index 6e5595b45..25736f2de 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -11,7 +11,8 @@ import ( // CommentSpacingsRule check the whether there is a space between // the comment symbol( // ) and the start of the comment text type CommentSpacingsRule struct { - allowList []string + allowList []string + configureOnce sync.Once } diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index f876b8b7c..e848a4e29 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -12,6 +12,7 @@ import ( // ContextAsArgumentRule lints given else constructs. type ContextAsArgumentRule struct { allowTypesLUT map[string]struct{} + configureOnce sync.Once } diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 88958cdf6..4ebd27522 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -14,6 +14,7 @@ import ( // CyclomaticRule lints given else constructs. type CyclomaticRule struct { maxComplexity int + configureOnce sync.Once } diff --git a/rule/defer.go b/rule/defer.go index 8c08e9e34..b6a3d8b0f 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -11,6 +11,7 @@ import ( // DeferRule lints unused params in functions. type DeferRule struct { allow map[string]bool + configureOnce sync.Once } diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 6d935ae3d..36e470246 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -11,6 +11,7 @@ import ( // DotImportsRule lints given else constructs. type DotImportsRule struct { allowedPackages allowPackages + configureOnce sync.Once } diff --git a/rule/error_strings.go b/rule/error_strings.go index 07e1d5c29..2cec8a872 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -16,6 +16,7 @@ import ( // ErrorStringsRule lints given else constructs. type ErrorStringsRule struct { errorFunctions map[string]map[string]struct{} + configureOnce sync.Once } diff --git a/rule/file_header.go b/rule/file_header.go index 6051a3cd2..5e904312a 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -10,7 +10,8 @@ import ( // FileHeaderRule lints given else constructs. type FileHeaderRule struct { - header string + header string + configureOnce sync.Once } diff --git a/rule/filename_format.go b/rule/filename_format.go index 76364c6c6..4060d01d7 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -12,7 +12,8 @@ import ( // FilenameFormatRule lints source filenames according to a set of regular expressions given as arguments type FilenameFormatRule struct { - format *regexp.Regexp + format *regexp.Regexp + configureOnce sync.Once } diff --git a/rule/function_length.go b/rule/function_length.go index 760c0f18a..599fd9e43 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -11,8 +11,9 @@ import ( // FunctionLength lint. type FunctionLength struct { - maxStmt int - maxLines int + maxStmt int + maxLines int + configureOnce sync.Once } diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 739fcb4ac..58032d3e7 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -11,7 +11,8 @@ import ( // FunctionResultsLimitRule lints given else constructs. type FunctionResultsLimitRule struct { - max int + max int + configureOnce sync.Once } diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index c867aee84..d871a9943 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -10,8 +10,9 @@ import ( // ImportAliasNamingRule lints import alias naming. type ImportAliasNamingRule struct { - allowRegexp *regexp.Regexp - denyRegexp *regexp.Regexp + allowRegexp *regexp.Regexp + denyRegexp *regexp.Regexp + configureOnce sync.Once } diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 4581f28f4..c47f07cf1 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -11,6 +11,7 @@ import ( // ImportsBlocklistRule lints given else constructs. type ImportsBlocklistRule struct { blocklist []*regexp.Regexp + configureOnce sync.Once } diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index 2ee42f04f..b4b1ac19e 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -15,7 +15,8 @@ import ( // LineLengthLimitRule lints given else constructs. type LineLengthLimitRule struct { - max int + max int + configureOnce sync.Once } diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 0672a6223..cb1b92078 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -11,7 +11,8 @@ import ( // MaxControlNestingRule lints given else constructs. type MaxControlNestingRule struct { - max int64 + max int64 + configureOnce sync.Once } diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index eaa58e334..bf3d08600 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -12,7 +12,8 @@ import ( // MaxPublicStructsRule lints given else constructs. type MaxPublicStructsRule struct { - max int64 + max int64 + configureOnce sync.Once } diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 35ad80b0f..b810a38d6 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -14,6 +14,7 @@ import ( // StructTagRule lints struct tags. type StructTagRule struct { userDefined map[string][]string // map: key -> []option + configureOnce sync.Once } diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index a5266f3d0..716d056f3 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -15,6 +15,7 @@ import ( // UnhandledErrorRule lints given else constructs. type UnhandledErrorRule struct { ignoreList []*regexp.Regexp + configureOnce sync.Once } diff --git a/rule/unused_param.go b/rule/unused_param.go index 61b4015e9..e068a22a2 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -12,8 +12,9 @@ import ( // UnusedParamRule lints unused params in functions. type UnusedParamRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default - allowRegex *regexp.Regexp - failureMsg string + allowRegex *regexp.Regexp + failureMsg string + configureOnce sync.Once } diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 43387a013..f26f67f0b 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -12,8 +12,9 @@ import ( // UnusedReceiverRule lints unused params in functions. type UnusedReceiverRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default - allowRegex *regexp.Regexp - failureMsg string + allowRegex *regexp.Regexp + failureMsg string + configureOnce sync.Once } From a451c028d60a49e1a38e9b886ab9eacffd44036e Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Tue, 19 Nov 2024 19:07:50 +0100 Subject: [PATCH 12/36] cleanup code --- lint/linter.go | 7 +------ lint/package.go | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lint/linter.go b/lint/linter.go index 4c5aa5ef8..1f993f216 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -152,13 +152,8 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS return nil } - err := pkg.lint(ruleSet, config, failures) + return pkg.lint(ruleSet, config, failures) - if err != nil { - return err - } - - return nil } func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) { diff --git a/lint/package.go b/lint/package.go index f471131be..703335839 100644 --- a/lint/package.go +++ b/lint/package.go @@ -192,7 +192,7 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error go (func(file *File) { err := file.lint(rules, config, failures) if err != nil { - fmt.Fprintln(os.Stderr, err) + fmt.Fprintln(os.Stderr, "Error during linting:", err) os.Exit(1) } wg.Done() From 1c6e8e7e6d00692ef3515211838c6200f6f61319 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Tue, 19 Nov 2024 19:11:07 +0100 Subject: [PATCH 13/36] cleanup code --- lint/linter.go | 1 - 1 file changed, 1 deletion(-) diff --git a/lint/linter.go b/lint/linter.go index 1f993f216..0da195bb0 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -153,7 +153,6 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS } return pkg.lint(ruleSet, config, failures) - } func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) { From f17c9b1f5045be637d3497cfdc0387ac8f9df4c6 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Wed, 20 Nov 2024 15:12:21 +0100 Subject: [PATCH 14/36] replace configureOnce with sync.OnceValue --- rule/add_constant.go | 9 +++------ rule/argument_limit.go | 9 +++------ rule/banned_characters.go | 8 +++----- rule/cognitive_complexity.go | 11 ++++------- rule/comment_spacings.go | 9 +++------ rule/comments_density.go | 9 ++++----- rule/context_as_argument.go | 9 +++------ rule/cyclomatic.go | 9 +++------ rule/defer.go | 10 ++++------ rule/dot_imports.go | 9 +++------ rule/enforce_map_style.go | 8 +++----- rule/enforce_repeated_arg_type_style.go | 9 ++++----- rule/enforce_slice_style.go | 8 +++----- rule/error_strings.go | 12 +++++------- rule/exported.go | 8 +++----- rule/file_header.go | 9 +++------ rule/file_length_limit.go | 8 +++----- rule/filename_format.go | 9 +++------ rule/function_length.go | 9 +++------ rule/function_result_limit.go | 9 +++------ rule/import_alias_naming.go | 9 +++------ rule/imports_blocklist.go | 11 ++++------- rule/line_length_limit.go | 9 +++------ rule/max_control_nesting.go | 9 +++------ rule/max_public_structs.go | 9 +++------ rule/receiver_naming.go | 8 +++----- rule/struct_tag.go | 11 ++++------- rule/unchecked_type_assertion.go | 8 +++----- rule/unhandled_error.go | 9 +++------ rule/unused_param.go | 11 ++++------- rule/unused_receiver.go | 9 +++------ rule/var_naming.go | 8 +++----- 32 files changed, 105 insertions(+), 187 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index 6fc4f2f8b..162841107 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -37,16 +37,13 @@ type AddConstantRule struct { allowList allowList ignoreFunctions []*regexp.Regexp strLitLimit int - - configureOnce sync.Once } // Apply applies the rule to given file. func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error {return r.configure(arguments)}) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/argument_limit.go b/rule/argument_limit.go index 69a6b52af..0cf699161 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -12,8 +12,6 @@ import ( // ArgumentsLimitRule lints given else constructs. type ArgumentsLimitRule struct { max int - - configureOnce sync.Once } const defaultArgumentsLimit = 8 @@ -34,10 +32,9 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 7aadec457..922b0c8e1 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -12,7 +12,6 @@ import ( // BannedCharsRule checks if a file contains banned characters. type BannedCharsRule struct { bannedCharList []string - configureOnce sync.Once } const bannedCharsRuleName = "banned-characters" @@ -35,10 +34,9 @@ func (r *BannedCharsRule) configure(arguments lint.Arguments) error { // Apply applied the rule to the given file. func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index c4bd1b52f..280587271 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -13,8 +13,6 @@ import ( // CognitiveComplexityRule lints given else constructs. type CognitiveComplexityRule struct { maxComplexity int - - configureOnce sync.Once } const defaultMaxCognitiveComplexity = 7 @@ -36,11 +34,10 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr - } + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err + } var failures []lint.Failure diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index 25736f2de..7f6e967a5 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -12,8 +12,6 @@ import ( // the comment symbol( // ) and the start of the comment text type CommentSpacingsRule struct { allowList []string - - configureOnce sync.Once } func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { @@ -30,10 +28,9 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { // Apply the rule. func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/comments_density.go b/rule/comments_density.go index e96c78c12..6a97128fe 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -12,7 +12,6 @@ import ( // CommentsDensityRule lints given else constructs. type CommentsDensityRule struct { minimumCommentsDensity int64 - configureOnce sync.Once } const defaultMinimumCommentsPercentage = 0 @@ -33,11 +32,11 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } + commentsLines := countDocLines(file.AST.Comments) statementsCount := countStatements(file.AST) density := (float32(commentsLines) / float32(statementsCount+commentsLines)) * 100 diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index e848a4e29..521ea5d80 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -12,16 +12,13 @@ import ( // ContextAsArgumentRule lints given else constructs. type ContextAsArgumentRule struct { allowTypesLUT map[string]struct{} - - configureOnce sync.Once } // Apply applies the rule to given file. func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 4ebd27522..b14f53cc1 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -14,8 +14,6 @@ import ( // CyclomaticRule lints given else constructs. type CyclomaticRule struct { maxComplexity int - - configureOnce sync.Once } const defaultMaxCyclomaticComplexity = 10 @@ -36,10 +34,9 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/defer.go b/rule/defer.go index b6a3d8b0f..f51797cf1 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -11,8 +11,6 @@ import ( // DeferRule lints unused params in functions. type DeferRule struct { allow map[string]bool - - configureOnce sync.Once } func (r *DeferRule) configure(arguments lint.Arguments) error { @@ -26,11 +24,11 @@ func (r *DeferRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } + var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 36e470246..8cb2c84f3 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -11,16 +11,13 @@ import ( // DotImportsRule lints given else constructs. type DotImportsRule struct { allowedPackages allowPackages - - configureOnce sync.Once } // Apply applies the rule to given file. func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index 8a0951163..4b64bb907 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -40,7 +40,6 @@ func mapStyleFromString(s string) (enforceMapStyleType, error) { // EnforceMapStyleRule implements a rule to enforce `make(map[type]type)` over `map[type]type{}`. type EnforceMapStyleRule struct { enforceMapStyle enforceMapStyleType - configureOnce sync.Once } func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { @@ -65,10 +64,9 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } if r.enforceMapStyle == enforceMapStyleTypeAny { diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index 7e7291822..1acadbfeb 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -43,7 +43,6 @@ func repeatedArgTypeStyleFromString(s string) (enforceRepeatedArgTypeStyleType, type EnforceRepeatedArgTypeStyleRule struct { funcArgStyle enforceRepeatedArgTypeStyleType funcRetValStyle enforceRepeatedArgTypeStyleType - configureOnce sync.Once } func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) error { @@ -101,11 +100,11 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er // Apply applies the rule to a given file. func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } + if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. return nil, nil diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 2ea3db076..f05c74463 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -44,7 +44,6 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { // EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`. type EnforceSliceStyleRule struct { enforceSliceStyle enforceSliceStyleType - configureOnce sync.Once } func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { @@ -68,10 +67,9 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } if r.enforceSliceStyle == enforceSliceStyleTypeAny { diff --git a/rule/error_strings.go b/rule/error_strings.go index 2cec8a872..db66c75a5 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -16,8 +16,6 @@ import ( // ErrorStringsRule lints given else constructs. type ErrorStringsRule struct { errorFunctions map[string]map[string]struct{} - - configureOnce sync.Once } func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { @@ -54,13 +52,13 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var failures []lint.Failure - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } + var failures []lint.Failure + fileAst := file.AST walker := lintErrorStrings{ file: file, diff --git a/rule/exported.go b/rule/exported.go index 98204196a..62e907e69 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -57,7 +57,6 @@ func (dc *disabledChecks) isDisabled(checkName string) bool { type ExportedRule struct { stuttersMsg string disabledChecks disabledChecks - configureOnce sync.Once } func (r *ExportedRule) configure(arguments lint.Arguments) error { @@ -98,10 +97,9 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/file_header.go b/rule/file_header.go index 5e904312a..682315aaa 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -11,8 +11,6 @@ import ( // FileHeaderRule lints given else constructs. type FileHeaderRule struct { header string - - configureOnce sync.Once } var ( @@ -35,10 +33,9 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } if r.header == "" { diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index b8a70b0f3..826be656d 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -20,15 +20,13 @@ type FileLengthLimitRule struct { skipComments bool // skipBlankLines indicates whether to skip blank lines when counting lines. skipBlankLines bool - configureOnce sync.Once } // Apply applies the rule to given file. func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } if r.max <= 0 { diff --git a/rule/filename_format.go b/rule/filename_format.go index 4060d01d7..d3348fd57 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -13,16 +13,13 @@ import ( // FilenameFormatRule lints source filenames according to a set of regular expressions given as arguments type FilenameFormatRule struct { format *regexp.Regexp - - configureOnce sync.Once } // Apply applies the rule to the given file. func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } filename := filepath.Base(file.Name) diff --git a/rule/function_length.go b/rule/function_length.go index 599fd9e43..83fa48d46 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -13,8 +13,6 @@ import ( type FunctionLength struct { maxStmt int maxLines int - - configureOnce sync.Once } func (r *FunctionLength) configure(arguments lint.Arguments) error { @@ -29,10 +27,9 @@ func (r *FunctionLength) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 58032d3e7..159b44959 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -12,8 +12,6 @@ import ( // FunctionResultsLimitRule lints given else constructs. type FunctionResultsLimitRule struct { max int - - configureOnce sync.Once } const defaultResultsLimit = 3 @@ -38,10 +36,9 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index d871a9943..5fcf49841 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -12,8 +12,6 @@ import ( type ImportAliasNamingRule struct { allowRegexp *regexp.Regexp denyRegexp *regexp.Regexp - - configureOnce sync.Once } const defaultImportAliasNamingAllowRule = "^[a-z][a-z0-9]{0,}$" @@ -62,10 +60,9 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index c47f07cf1..c89b59a7e 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -10,9 +10,7 @@ import ( // ImportsBlocklistRule lints given else constructs. type ImportsBlocklistRule struct { - blocklist []*regexp.Regexp - - configureOnce sync.Once + blocklist []*regexp.Regexp } var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`) @@ -44,10 +42,9 @@ func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { // Apply applies the rule to given file. func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configureErr error - r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) - if configureErr != nil { - return nil, configureErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index b4b1ac19e..d8f038109 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -16,8 +16,6 @@ import ( // LineLengthLimitRule lints given else constructs. type LineLengthLimitRule struct { max int - - configureOnce sync.Once } const defaultLineLengthLimit = 80 @@ -39,10 +37,9 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index cb1b92078..952001aea 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -12,18 +12,15 @@ import ( // MaxControlNestingRule lints given else constructs. type MaxControlNestingRule struct { max int64 - - configureOnce sync.Once } const defaultMaxControlNesting = 5 // Apply applies the rule to given file. func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index bf3d08600..033cad4d6 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -13,8 +13,6 @@ import ( // MaxPublicStructsRule lints given else constructs. type MaxPublicStructsRule struct { max int64 - - configureOnce sync.Once } const defaultMaxPublicStructs = 5 @@ -40,10 +38,9 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 33e382b32..221965061 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -12,7 +12,6 @@ import ( // ReceiverNamingRule lints given else constructs. type ReceiverNamingRule struct { receiverNameMaxLength int - configureOnce sync.Once } const defaultReceiverNameMaxLength = -1 // thus will not check @@ -45,10 +44,9 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/struct_tag.go b/rule/struct_tag.go index b810a38d6..39c49e26a 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -13,9 +13,7 @@ import ( // StructTagRule lints struct tags. type StructTagRule struct { - userDefined map[string][]string // map: key -> []option - - configureOnce sync.Once + userDefined map[string][]string // map: key -> []option } func (r *StructTagRule) configure(arguments lint.Arguments) error { @@ -48,10 +46,9 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index d90cba333..28ef56bfa 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -17,7 +17,6 @@ const ( // UncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. type UncheckedTypeAssertionRule struct { acceptIgnoredAssertionResult bool - configureOnce sync.Once } func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { @@ -46,10 +45,9 @@ func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 716d056f3..76f14089a 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -15,8 +15,6 @@ import ( // UnhandledErrorRule lints given else constructs. type UnhandledErrorRule struct { ignoreList []*regexp.Regexp - - configureOnce sync.Once } func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { @@ -43,10 +41,9 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/unused_param.go b/rule/unused_param.go index e068a22a2..8c080482d 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -12,10 +12,8 @@ import ( // UnusedParamRule lints unused params in functions. type UnusedParamRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default - allowRegex *regexp.Regexp + allowRegex *regexp.Regexp failureMsg string - - configureOnce sync.Once } func (r *UnusedParamRule) configure(args lint.Arguments) error { @@ -51,10 +49,9 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index f26f67f0b..2864a05ac 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -14,8 +14,6 @@ type UnusedReceiverRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default allowRegex *regexp.Regexp failureMsg string - - configureOnce sync.Once } func (r *UnusedReceiverRule) configure(args lint.Arguments) error { @@ -50,10 +48,9 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure diff --git a/rule/var_naming.go b/rule/var_naming.go index de5447014..5d349aab5 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -22,7 +22,6 @@ type VarNamingRule struct { blockList []string allowUpperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants skipPackageNameChecks bool - configureOnce sync.Once } func (r *VarNamingRule) configure(arguments lint.Arguments) error { @@ -84,10 +83,9 @@ func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { // Apply applies the rule to given file. func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - var configErr error - r.configureOnce.Do(func() { configErr = r.configure(arguments) }) - if configErr != nil { - return nil, configErr + check := sync.OnceValue(func() error { return r.configure(arguments) }) + if err := check(); err != nil { + return nil, err } var failures []lint.Failure From ae29706c108271f3ed26b91602b5eaf013657c4c Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Wed, 20 Nov 2024 15:53:00 +0100 Subject: [PATCH 15/36] lint: remove error return, exit is last step --- lint/linter.go | 4 +++- lint/package.go | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lint/linter.go b/lint/linter.go index 0da195bb0..5dedfa4c9 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -152,7 +152,9 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS return nil } - return pkg.lint(ruleSet, config, failures) + pkg.lint(ruleSet, config, failures) + + return nil } func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) { diff --git a/lint/package.go b/lint/package.go index 703335839..aaf3f0baa 100644 --- a/lint/package.go +++ b/lint/package.go @@ -184,7 +184,7 @@ func (p *Package) scanSortable() { } } -func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error { +func (p *Package) lint(rules []Rule, config Config, failures chan Failure) { p.scanSortable() var wg sync.WaitGroup for _, file := range p.files { @@ -199,7 +199,6 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error })(file) } wg.Wait() - return nil } // IsAtLeastGo121 returns true if the Go version for this package is 1.21 or higher, false otherwise From fdcaa688c9a04be1e8af47b3e4e2d3a3a9d96c53 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 7 Dec 2024 02:01:53 +0100 Subject: [PATCH 16/36] back to r.configureOnce.Do() variant with local error variable --- lint/linter.go | 3 +-- lint/package.go | 3 ++- rule/add_constant.go | 16 +++++++++++++--- rule/argument_limit.go | 10 +++++++--- rule/banned_characters.go | 10 +++++++--- rule/cognitive_complexity.go | 12 ++++++++---- rule/comment_spacings.go | 10 +++++++--- rule/comments_density.go | 10 +++++++--- rule/context_as_argument.go | 10 +++++++--- rule/cyclomatic.go | 10 +++++++--- rule/defer.go | 10 +++++++--- rule/dot_imports.go | 10 +++++++--- rule/enforce_map_style.go | 10 +++++++--- rule/enforce_repeated_arg_type_style.go | 10 +++++++--- rule/enforce_slice_style.go | 10 +++++++--- rule/error_strings.go | 12 ++++++++---- rule/exported.go | 10 +++++++--- rule/file_header.go | 10 +++++++--- rule/file_length_limit.go | 10 +++++++--- rule/filename_format.go | 10 +++++++--- rule/function_length.go | 12 ++++++++---- rule/function_result_limit.go | 10 +++++++--- rule/import_alias_naming.go | 10 +++++++--- rule/imports_blocklist.go | 10 +++++++--- rule/line_length_limit.go | 10 +++++++--- rule/max_control_nesting.go | 10 +++++++--- rule/max_public_structs.go | 10 +++++++--- rule/receiver_naming.go | 10 +++++++--- rule/struct_tag.go | 10 +++++++--- rule/unchecked_type_assertion.go | 10 +++++++--- rule/unhandled_error.go | 12 ++++++++---- rule/unused_param.go | 10 +++++++--- rule/unused_receiver.go | 10 +++++++--- rule/var_naming.go | 10 +++++++--- 34 files changed, 237 insertions(+), 103 deletions(-) diff --git a/lint/linter.go b/lint/linter.go index 5dedfa4c9..c7230a44f 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -152,9 +152,8 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS return nil } - pkg.lint(ruleSet, config, failures) + return pkg.lint(ruleSet, config, failures) - return nil } func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) { diff --git a/lint/package.go b/lint/package.go index aaf3f0baa..703335839 100644 --- a/lint/package.go +++ b/lint/package.go @@ -184,7 +184,7 @@ func (p *Package) scanSortable() { } } -func (p *Package) lint(rules []Rule, config Config, failures chan Failure) { +func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error { p.scanSortable() var wg sync.WaitGroup for _, file := range p.files { @@ -199,6 +199,7 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) { })(file) } wg.Wait() + return nil } // IsAtLeastGo121 returns true if the Go version for this package is 1.21 or higher, false otherwise diff --git a/rule/add_constant.go b/rule/add_constant.go index 162841107..3832fff6f 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -37,13 +37,17 @@ type AddConstantRule struct { allowList allowList ignoreFunctions []*regexp.Regexp strLitLimit int + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error {return r.configure(arguments)}) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure @@ -268,3 +272,9 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) error { return nil } + +func configOnce(r *AddConstantRule, arguments lint.Arguments) func() error { + return sync.OnceValue(func() error { + return r.configure(arguments) + }) +} diff --git a/rule/argument_limit.go b/rule/argument_limit.go index 0cf699161..adadd71b5 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -12,6 +12,8 @@ import ( // ArgumentsLimitRule lints given else constructs. type ArgumentsLimitRule struct { max int + + configureOnce sync.Once } const defaultArgumentsLimit = 8 @@ -32,9 +34,11 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 922b0c8e1..1d8a67753 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -12,6 +12,8 @@ import ( // BannedCharsRule checks if a file contains banned characters. type BannedCharsRule struct { bannedCharList []string + + configureOnce sync.Once } const bannedCharsRuleName = "banned-characters" @@ -34,9 +36,11 @@ func (r *BannedCharsRule) configure(arguments lint.Arguments) error { // Apply applied the rule to the given file. func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index 280587271..73f1bdb62 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -13,6 +13,8 @@ import ( // CognitiveComplexityRule lints given else constructs. type CognitiveComplexityRule struct { maxComplexity int + + configureOnce sync.Once } const defaultMaxCognitiveComplexity = 7 @@ -34,10 +36,12 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err - } + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr + } var failures []lint.Failure diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index 7f6e967a5..7baca2147 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -12,6 +12,8 @@ import ( // the comment symbol( // ) and the start of the comment text type CommentSpacingsRule struct { allowList []string + + configureOnce sync.Once } func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { @@ -28,9 +30,11 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { // Apply the rule. func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/comments_density.go b/rule/comments_density.go index 6a97128fe..ec20d0fdf 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -12,6 +12,8 @@ import ( // CommentsDensityRule lints given else constructs. type CommentsDensityRule struct { minimumCommentsDensity int64 + + configureOnce sync.Once } const defaultMinimumCommentsPercentage = 0 @@ -32,9 +34,11 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } commentsLines := countDocLines(file.AST.Comments) diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index 521ea5d80..599546efd 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -12,13 +12,17 @@ import ( // ContextAsArgumentRule lints given else constructs. type ContextAsArgumentRule struct { allowTypesLUT map[string]struct{} + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index b14f53cc1..00292c183 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -14,6 +14,8 @@ import ( // CyclomaticRule lints given else constructs. type CyclomaticRule struct { maxComplexity int + + configureOnce sync.Once } const defaultMaxCyclomaticComplexity = 10 @@ -34,9 +36,11 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/defer.go b/rule/defer.go index f51797cf1..11d3be68b 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -11,6 +11,8 @@ import ( // DeferRule lints unused params in functions. type DeferRule struct { allow map[string]bool + + configureOnce sync.Once } func (r *DeferRule) configure(arguments lint.Arguments) error { @@ -24,9 +26,11 @@ func (r *DeferRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 8cb2c84f3..a87fc38ff 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -11,13 +11,17 @@ import ( // DotImportsRule lints given else constructs. type DotImportsRule struct { allowedPackages allowPackages + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index 4b64bb907..bc16080fe 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -40,6 +40,8 @@ func mapStyleFromString(s string) (enforceMapStyleType, error) { // EnforceMapStyleRule implements a rule to enforce `make(map[type]type)` over `map[type]type{}`. type EnforceMapStyleRule struct { enforceMapStyle enforceMapStyleType + + configureOnce sync.Once } func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { @@ -64,9 +66,11 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } if r.enforceMapStyle == enforceMapStyleTypeAny { diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index 1acadbfeb..cd3d10e9f 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -43,6 +43,8 @@ func repeatedArgTypeStyleFromString(s string) (enforceRepeatedArgTypeStyleType, type EnforceRepeatedArgTypeStyleRule struct { funcArgStyle enforceRepeatedArgTypeStyleType funcRetValStyle enforceRepeatedArgTypeStyleType + + configureOnce sync.Once } func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) error { @@ -100,9 +102,11 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er // Apply applies the rule to a given file. func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index f05c74463..1e7c8eeaf 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -44,6 +44,8 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { // EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`. type EnforceSliceStyleRule struct { enforceSliceStyle enforceSliceStyleType + + configureOnce sync.Once } func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { @@ -67,9 +69,11 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } if r.enforceSliceStyle == enforceSliceStyleTypeAny { diff --git a/rule/error_strings.go b/rule/error_strings.go index db66c75a5..c21eca41f 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -16,6 +16,8 @@ import ( // ErrorStringsRule lints given else constructs. type ErrorStringsRule struct { errorFunctions map[string]map[string]struct{} + + configureOnce sync.Once } func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { @@ -52,13 +54,15 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure - + fileAst := file.AST walker := lintErrorStrings{ file: file, diff --git a/rule/exported.go b/rule/exported.go index 62e907e69..b864adcfa 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -57,6 +57,8 @@ func (dc *disabledChecks) isDisabled(checkName string) bool { type ExportedRule struct { stuttersMsg string disabledChecks disabledChecks + + configureOnce sync.Once } func (r *ExportedRule) configure(arguments lint.Arguments) error { @@ -97,9 +99,11 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/file_header.go b/rule/file_header.go index 682315aaa..976e43352 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -11,6 +11,8 @@ import ( // FileHeaderRule lints given else constructs. type FileHeaderRule struct { header string + + configureOnce sync.Once } var ( @@ -33,9 +35,11 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } if r.header == "" { diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index 826be656d..6e157c595 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -20,13 +20,17 @@ type FileLengthLimitRule struct { skipComments bool // skipBlankLines indicates whether to skip blank lines when counting lines. skipBlankLines bool + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } if r.max <= 0 { diff --git a/rule/filename_format.go b/rule/filename_format.go index d3348fd57..4394e7435 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -13,13 +13,17 @@ import ( // FilenameFormatRule lints source filenames according to a set of regular expressions given as arguments type FilenameFormatRule struct { format *regexp.Regexp + + configureOnce sync.Once } // Apply applies the rule to the given file. func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } filename := filepath.Base(file.Name) diff --git a/rule/function_length.go b/rule/function_length.go index 83fa48d46..6eeffecf6 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -13,6 +13,8 @@ import ( type FunctionLength struct { maxStmt int maxLines int + + configureOnce sync.Once } func (r *FunctionLength) configure(arguments lint.Arguments) error { @@ -27,11 +29,13 @@ func (r *FunctionLength) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err - } + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + if configureErr != nil { + return nil, configureErr + } + var failures []lint.Failure walker := lintFuncLength{ diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 159b44959..168f814a4 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -12,6 +12,8 @@ import ( // FunctionResultsLimitRule lints given else constructs. type FunctionResultsLimitRule struct { max int + + configureOnce sync.Once } const defaultResultsLimit = 3 @@ -36,9 +38,11 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 5fcf49841..2c76eb1a5 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -12,6 +12,8 @@ import ( type ImportAliasNamingRule struct { allowRegexp *regexp.Regexp denyRegexp *regexp.Regexp + + configureOnce sync.Once } const defaultImportAliasNamingAllowRule = "^[a-z][a-z0-9]{0,}$" @@ -60,9 +62,11 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index c89b59a7e..5d10fb83a 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -11,6 +11,8 @@ import ( // ImportsBlocklistRule lints given else constructs. type ImportsBlocklistRule struct { blocklist []*regexp.Regexp + + configureOnce sync.Once } var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`) @@ -42,9 +44,11 @@ func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { // Apply applies the rule to given file. func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index d8f038109..abaf105f2 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -16,6 +16,8 @@ import ( // LineLengthLimitRule lints given else constructs. type LineLengthLimitRule struct { max int + + configureOnce sync.Once } const defaultLineLengthLimit = 80 @@ -37,9 +39,11 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 952001aea..be1d8ca07 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -12,15 +12,19 @@ import ( // MaxControlNestingRule lints given else constructs. type MaxControlNestingRule struct { max int64 + + configureOnce sync.Once } const defaultMaxControlNesting = 5 // Apply applies the rule to given file. func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 033cad4d6..aa61aa772 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -13,6 +13,8 @@ import ( // MaxPublicStructsRule lints given else constructs. type MaxPublicStructsRule struct { max int64 + + configureOnce sync.Once } const defaultMaxPublicStructs = 5 @@ -38,9 +40,11 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 221965061..302d14709 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -12,6 +12,8 @@ import ( // ReceiverNamingRule lints given else constructs. type ReceiverNamingRule struct { receiverNameMaxLength int + + configureOnce sync.Once } const defaultReceiverNameMaxLength = -1 // thus will not check @@ -44,9 +46,11 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 39c49e26a..3810c23bc 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -14,6 +14,8 @@ import ( // StructTagRule lints struct tags. type StructTagRule struct { userDefined map[string][]string // map: key -> []option + + configureOnce sync.Once } func (r *StructTagRule) configure(arguments lint.Arguments) error { @@ -46,9 +48,11 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index 28ef56bfa..6d1540f3e 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -17,6 +17,8 @@ const ( // UncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. type UncheckedTypeAssertionRule struct { acceptIgnoredAssertionResult bool + + configureOnce sync.Once } func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { @@ -45,9 +47,11 @@ func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 76f14089a..d007178da 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -14,7 +14,9 @@ import ( // UnhandledErrorRule lints given else constructs. type UnhandledErrorRule struct { - ignoreList []*regexp.Regexp + ignoreList []*regexp.Regexp + + configureOnce sync.Once } func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { @@ -41,9 +43,11 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { // Apply applies the rule to given file. func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/unused_param.go b/rule/unused_param.go index 8c080482d..4b1354270 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -14,6 +14,8 @@ type UnusedParamRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default allowRegex *regexp.Regexp failureMsg string + + configureOnce sync.Once } func (r *UnusedParamRule) configure(args lint.Arguments) error { @@ -49,9 +51,11 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 2864a05ac..fdb9a77b6 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -14,6 +14,8 @@ type UnusedReceiverRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default allowRegex *regexp.Regexp failureMsg string + + configureOnce sync.Once } func (r *UnusedReceiverRule) configure(args lint.Arguments) error { @@ -48,9 +50,11 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { // Apply applies the rule to given file. func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure diff --git a/rule/var_naming.go b/rule/var_naming.go index 5d349aab5..cc954088d 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -22,6 +22,8 @@ type VarNamingRule struct { blockList []string allowUpperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants skipPackageNameChecks bool + + configureOnce sync.Once } func (r *VarNamingRule) configure(arguments lint.Arguments) error { @@ -83,9 +85,11 @@ func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { // Apply applies the rule to given file. func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - check := sync.OnceValue(func() error { return r.configure(arguments) }) - if err := check(); err != nil { - return nil, err + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return nil, configureErr } var failures []lint.Failure From 0022083a989302766610212eff1c2e372b4cb1a6 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 7 Dec 2024 08:50:05 +0100 Subject: [PATCH 17/36] cleanup after merge with master --- rule/context_as_argument.go | 11 ++++++++--- rule/cyclomatic.go | 2 +- rule/function_length.go | 2 +- rule/function_result_limit.go | 4 ++-- rule/modifies_value_receiver.go | 2 +- rule/redundant_build_tag.go | 6 +++--- rule/unused_param.go | 5 +++-- rule/unused_receiver.go | 7 ++++--- rule/use_errors_new.go | 4 ++-- 9 files changed, 25 insertions(+), 18 deletions(-) diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index 8d2a0c762..64cc3f50b 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -56,7 +56,7 @@ func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) } } - return failures + return failures, nil } // Name returns the rule name. @@ -64,8 +64,13 @@ func (*ContextAsArgumentRule) Name() string { return "context-as-argument" } -func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) { - r.allowTypes = r.getAllowTypesFromArguments(arguments) +func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) error { + types, err := r.getAllowTypesFromArguments(arguments) + if err != nil { + return err + } + r.allowTypes = types + return nil } func (r *ContextAsArgumentRule) getAllowTypesFromArguments(args lint.Arguments) (map[string]struct{}, error) { diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index aac01a18c..69cfa96e5 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -62,7 +62,7 @@ func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lin } } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/function_length.go b/rule/function_length.go index 387108b01..6a14bdf32 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -46,7 +46,7 @@ func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lin body := funcDecl.Body emptyBody := body == nil || len(body.List) == 0 if emptyBody { - return nil + return nil, nil } if r.maxStmt > 0 { diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index dac996961..10f4f7016 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -17,7 +17,7 @@ type FunctionResultsLimitRule struct { } // Apply applies the rule to given file. -func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure @@ -44,7 +44,7 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen }) } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/modifies_value_receiver.go b/rule/modifies_value_receiver.go index 253548cf0..3fe2956f6 100644 --- a/rule/modifies_value_receiver.go +++ b/rule/modifies_value_receiver.go @@ -48,7 +48,7 @@ func (r *ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fa } } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/redundant_build_tag.go b/rule/redundant_build_tag.go index 52412b9b3..26f248a17 100644 --- a/rule/redundant_build_tag.go +++ b/rule/redundant_build_tag.go @@ -12,7 +12,7 @@ type RedundantBuildTagRule struct{} // Apply triggers if an old build tag `// +build` is found after a new one `//go:build`. // `//go:build` comments are automatically added by gofmt when Go 1.17+ is used. // See https://pkg.go.dev/cmd/go#hdr-Build_constraints -func (*RedundantBuildTagRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*RedundantBuildTagRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { for _, group := range file.AST.Comments { hasGoBuild := false for _, comment := range group.List { @@ -27,12 +27,12 @@ func (*RedundantBuildTagRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa Confidence: 1, Node: comment, Failure: `The build tag "// +build" is redundant since Go 1.17 and can be removed`, - }} + }}, nil } } } - return []lint.Failure{} + return []lint.Failure{}, nil } // Name returns the rule name. diff --git a/rule/unused_param.go b/rule/unused_param.go index 183d99d05..db29cb580 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -26,14 +26,14 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { r.allowRegex = allowBlankIdentifierRegex r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it as _" if len(args) == 0 { - return + return nil } // Arguments = [{}] options := args[0].(map[string]any) allowRegexParam, ok := options["allowRegex"] if !ok { - return + return nil } // Arguments = [{allowRegex="^_"}] allowRegexStr, ok := allowRegexParam.(string) @@ -46,6 +46,7 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { return fmt.Errorf("error configuring %s rule: allowRegex is not valid regex [%s]: %w", r.Name(), allowRegexStr, err) } r.failureMsg = "parameter '%s' seems to be unused, consider removing or renaming it to match " + r.allowRegex.String() + return nil } // Apply applies the rule to given file. diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 359dd6ddb..7e5ad80d4 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -24,14 +24,14 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { r.allowRegex = allowBlankIdentifierRegex r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it as _" if len(args) == 0 { - return + return nil } // Arguments = [{}] options := args[0].(map[string]any) allowRegexParam, ok := options["allowRegex"] if !ok { - return + return nil } // Arguments = [{allowRegex="^_"}] allowRegexStr, ok := allowRegexParam.(string) @@ -44,6 +44,7 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { return fmt.Errorf("error configuring [unused-receiver] rule: allowRegex is not valid regex [%s]: %w", allowRegexStr, err) } r.failureMsg = "method receiver '%s' is not referenced in method's body, consider removing or renaming it to match " + r.allowRegex.String() + return nil } // Apply applies the rule to given file. @@ -98,7 +99,7 @@ func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([ }) } - return failures + return failures, nil } // Name returns the rule name. diff --git a/rule/use_errors_new.go b/rule/use_errors_new.go index d21bbf7d8..1905d1843 100644 --- a/rule/use_errors_new.go +++ b/rule/use_errors_new.go @@ -10,7 +10,7 @@ import ( type UseErrorsNewRule struct{} // Apply applies the rule to given file. -func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { +func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { var failures []lint.Failure walker := lintFmtErrorf{ @@ -21,7 +21,7 @@ func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure ast.Walk(walker, file.AST) - return failures + return failures, nil } // Name returns the rule name. From 9c9e05e9f3eb12deb8716fc44cb21a94aefaef21 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sat, 7 Dec 2024 14:33:37 +0100 Subject: [PATCH 18/36] package: handle errgroup.Group --- go.mod | 1 + go.sum | 2 ++ lint/linter.go | 3 +-- lint/package.go | 20 ++++++++++---------- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 2c48d4b92..ca23cba6c 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/afero v1.11.0 golang.org/x/mod v0.22.0 + golang.org/x/sync v0.10.0 golang.org/x/tools v0.28.0 ) diff --git a/go.sum b/go.sum index 1cd6350b3..696de223e 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= diff --git a/lint/linter.go b/lint/linter.go index c7230a44f..e8d0a963b 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -106,7 +106,7 @@ func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-cha wg.Add(1) go func(pkg []string, gover *goversion.Version) { if err := l.lintPackage(pkg, gover, ruleSet, config, failures); err != nil { - fmt.Fprintln(os.Stderr, err) + fmt.Fprintln(os.Stderr, "Error during linting:"+err.Error()) os.Exit(1) } wg.Done() @@ -153,7 +153,6 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS } return pkg.lint(ruleSet, config, failures) - } func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) { diff --git a/lint/package.go b/lint/package.go index dfefdaba4..56bc5726f 100644 --- a/lint/package.go +++ b/lint/package.go @@ -1,16 +1,15 @@ package lint import ( - "fmt" "errors" "go/ast" "go/importer" "go/token" "go/types" - "os" "sync" goversion "github.com/hashicorp/go-version" + "golang.org/x/sync/errgroup" "github.com/mgechev/revive/internal/astutils" "github.com/mgechev/revive/internal/typeparams" @@ -185,19 +184,20 @@ func (p *Package) scanSortable() { func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error { p.scanSortable() - var wg sync.WaitGroup + var eg errgroup.Group for _, file := range p.files { - wg.Add(1) - go (func(file *File) { + eg.Go(func() error { err := file.lint(rules, config, failures) if err != nil { - fmt.Fprintln(os.Stderr, "Error during linting:", err) - os.Exit(1) + return err } - wg.Done() - })(file) + return nil + }) } - wg.Wait() + if err := eg.Wait(); err != nil { + return err + } + return nil } From 2d6cafadb15419e13d46275872728f6f25af805b Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 01:06:49 +0100 Subject: [PATCH 19/36] after review changes --- lint/package.go | 12 ++---------- rule/add_constant.go | 6 ------ rule/banned_characters.go | 10 +++++----- rule/receiver_naming.go | 2 +- 4 files changed, 8 insertions(+), 22 deletions(-) diff --git a/lint/package.go b/lint/package.go index 56bc5726f..9e8dee880 100644 --- a/lint/package.go +++ b/lint/package.go @@ -187,18 +187,10 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error var eg errgroup.Group for _, file := range p.files { eg.Go(func() error { - err := file.lint(rules, config, failures) - if err != nil { - return err - } - return nil + return file.lint(rules, config, failures) }) } - if err := eg.Wait(); err != nil { - return err - } - - return nil + return eg.Wait() } // IsAtLeastGo121 returns true if the Go version for this package is 1.21 or higher, false otherwise diff --git a/rule/add_constant.go b/rule/add_constant.go index 2b737a7fd..f5ed2df56 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -272,9 +272,3 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) error { return nil } - -func configOnce(r *AddConstantRule, arguments lint.Arguments) func() error { - return sync.OnceValue(func() error { - return r.configure(arguments) - }) -} diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 1d8a67753..2c803e413 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -12,17 +12,17 @@ import ( // BannedCharsRule checks if a file contains banned characters. type BannedCharsRule struct { bannedCharList []string - - configureOnce sync.Once + + configureOnce sync.Once } const bannedCharsRuleName = "banned-characters" func (r *BannedCharsRule) configure(arguments lint.Arguments) error { if len(arguments) > 0 { - check := checkNumberOfArguments(1, arguments, bannedCharsRuleName) - if check != nil { - return check + err := checkNumberOfArguments(1, arguments, bannedCharsRuleName) + if err != nil { + return err } list, err := r.getBannedCharsList(arguments) if err != nil { diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index d4eba1f12..f9952c759 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -111,7 +111,7 @@ func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([ typeReceiver[recv] = name } - return failures + return failures, nil } // Name returns the rule name. From d6f17ffeaaf470c11688061ad7f61dc69795947d Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 20:44:05 +0100 Subject: [PATCH 20/36] cleanup rules, remove error from return signature --- lint/file.go | 6 ++---- lint/rule.go | 2 +- revivelib/core_internal_test.go | 4 ++-- revivelib/core_test.go | 4 ++-- rule/argument_limit.go | 6 +++--- rule/atomic.go | 4 ++-- rule/banned_characters.go | 6 +++--- rule/bare_return.go | 4 ++-- rule/blank_imports.go | 6 +++--- rule/bool_literal_in_expr.go | 4 ++-- rule/call_to_gc.go | 4 ++-- rule/cognitive_complexity.go | 6 +++--- rule/comment_spacings.go | 6 +++--- rule/comments_density.go | 8 ++++---- rule/confusing_naming.go | 4 ++-- rule/confusing_results.go | 4 ++-- rule/constant_logical_expr.go | 4 ++-- rule/context_as_argument.go | 6 +++--- rule/context_keys_type.go | 4 ++-- rule/cyclomatic.go | 6 +++--- rule/datarace.go | 4 ++-- rule/deep_exit.go | 4 ++-- rule/defer.go | 6 +++--- rule/dot_imports.go | 6 +++--- rule/duplicated_imports.go | 4 ++-- rule/early_return.go | 4 ++-- rule/empty_block.go | 4 ++-- rule/empty_lines.go | 4 ++-- rule/enforce_map_style.go | 8 ++++---- rule/enforce_repeated_arg_type_style.go | 8 ++++---- rule/enforce_slice_style.go | 8 ++++---- rule/error_naming.go | 4 ++-- rule/error_return.go | 4 ++-- rule/error_strings.go | 6 +++--- rule/errorf.go | 4 ++-- rule/exported.go | 8 ++++---- rule/file_header.go | 16 ++++++++-------- rule/file_length_limit.go | 12 ++++++------ rule/filename_format.go | 8 ++++---- rule/flag_param.go | 4 ++-- rule/function_length.go | 8 ++++---- rule/function_result_limit.go | 11 ++++++++--- rule/get_return.go | 4 ++-- rule/identical_branches.go | 4 ++-- rule/if_return.go | 4 ++-- rule/import_alias_naming.go | 6 +++--- rule/import_shadowing.go | 4 ++-- rule/imports_blocklist.go | 6 +++--- rule/increment_decrement.go | 4 ++-- rule/indent_error_flow.go | 4 ++-- rule/line_length_limit.go | 6 +++--- rule/max_control_nesting.go | 6 +++--- rule/max_public_structs.go | 8 ++++---- rule/modifies_param.go | 4 ++-- rule/modifies_value_receiver.go | 4 ++-- rule/nested_structs.go | 4 ++-- rule/optimize_operands_order.go | 4 ++-- rule/package_comments.go | 6 +++--- rule/range.go | 4 ++-- rule/range_val_address.go | 6 +++--- rule/range_val_in_closure.go | 6 +++--- rule/receiver_naming.go | 6 +++--- rule/redefines_builtin_id.go | 4 ++-- rule/redundant_build_tag.go | 6 +++--- rule/redundant_import_alias.go | 4 ++-- rule/string_format.go | 8 ++++---- rule/string_of_int.go | 4 ++-- rule/struct_tag.go | 6 +++--- rule/superfluous_else.go | 4 ++-- rule/time_equal.go | 6 +++--- rule/time_naming.go | 4 ++-- rule/unchecked_type_assertion.go | 6 +++--- rule/unconditional_recursion.go | 4 ++-- rule/unexported_naming.go | 4 ++-- rule/unexported_return.go | 4 ++-- rule/unhandled_error.go | 6 +++--- rule/unnecessary_stmt.go | 4 ++-- rule/unreachable_code.go | 4 ++-- rule/unused_param.go | 6 +++--- rule/unused_receiver.go | 6 +++--- rule/use_any.go | 4 ++-- rule/use_errors_new.go | 4 ++-- rule/useless_break.go | 4 ++-- rule/var_declarations.go | 4 ++-- rule/var_naming.go | 6 +++--- rule/waitgroup_by_value.go | 4 ++-- test/file_filter_test.go | 4 ++-- 87 files changed, 233 insertions(+), 230 deletions(-) diff --git a/lint/file.go b/lint/file.go index bbc92db42..be0b4d517 100644 --- a/lint/file.go +++ b/lint/file.go @@ -106,10 +106,8 @@ func (f *File) lint(rules []Rule, config Config, failures chan Failure) error { if ruleConfig.MustExclude(f.Name) { continue } - currentFailures, err := currentRule.Apply(f, ruleConfig.Arguments) - if err != nil { - return err - } + currentFailures := currentRule.Apply(f, ruleConfig.Arguments) + for idx, failure := range currentFailures { if failure.IsInternal() { return errors.New(failure.Failure) diff --git a/lint/rule.go b/lint/rule.go index ee25abb48..4d9542303 100644 --- a/lint/rule.go +++ b/lint/rule.go @@ -14,7 +14,7 @@ type DisabledInterval struct { // Rule defines an abstract rule interface type Rule interface { Name() string - Apply(*File, Arguments) ([]Failure, error) + Apply(*File, Arguments) ([]Failure) } // ToFailurePosition returns the failure position. diff --git a/revivelib/core_internal_test.go b/revivelib/core_internal_test.go index 298346c1d..a6dcd846a 100644 --- a/revivelib/core_internal_test.go +++ b/revivelib/core_internal_test.go @@ -43,8 +43,8 @@ func (*mockRule) Name() string { return "mock-rule" } -func (*mockRule) Apply(_ *lint.File, _ lint.Arguments) ([]lint.Failure, error) { - return nil, nil +func (*mockRule) Apply(_ *lint.File, _ lint.Arguments) []lint.Failure { + return nil } func getMockRevive(t *testing.T) *Revive { diff --git a/revivelib/core_test.go b/revivelib/core_test.go index 4fdabe369..60ed94bd1 100644 --- a/revivelib/core_test.go +++ b/revivelib/core_test.go @@ -78,8 +78,8 @@ func (r *mockRule) Name() string { return "mock-rule" } -func (r *mockRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - return nil, nil +func (r *mockRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { + return nil } func getMockRevive(t *testing.T) *revivelib.Revive { diff --git a/rule/argument_limit.go b/rule/argument_limit.go index c256dcb38..c8822ab20 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -33,12 +33,12 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -65,7 +65,7 @@ func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([ }) } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/atomic.go b/rule/atomic.go index cc7bee9e7..61219765f 100644 --- a/rule/atomic.go +++ b/rule/atomic.go @@ -12,7 +12,7 @@ import ( type AtomicRule struct{} // Apply applies the rule to given file. -func (*AtomicRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*AtomicRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure walker := atomic{ pkgTypesInfo: file.Pkg.TypesInfo(), @@ -23,7 +23,7 @@ func (*AtomicRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, err ast.Walk(walker, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 2c803e413..06a38364e 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -35,12 +35,12 @@ func (r *BannedCharsRule) configure(arguments lint.Arguments) error { } // Apply applied the rule to the given file. -func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -54,7 +54,7 @@ func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) ([]li } ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name diff --git a/rule/bare_return.go b/rule/bare_return.go index a59b3cab8..d81807b88 100644 --- a/rule/bare_return.go +++ b/rule/bare_return.go @@ -10,7 +10,7 @@ import ( type BareReturnRule struct{} // Apply applies the rule to given file. -func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +19,7 @@ func (*BareReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, w := lintBareReturnRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/blank_imports.go b/rule/blank_imports.go index cfa2cf04f..f578ad32b 100644 --- a/rule/blank_imports.go +++ b/rule/blank_imports.go @@ -16,9 +16,9 @@ func (*BlankImportsRule) Name() string { } // Apply applies the rule to given file. -func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { if file.Pkg.IsMain() || file.IsTest() { - return nil, nil + return nil } const ( @@ -59,7 +59,7 @@ func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fail } } - return failures, nil + return failures } func (*BlankImportsRule) fileHasValidEmbedComment(fileAst *ast.File) bool { diff --git a/rule/bool_literal_in_expr.go b/rule/bool_literal_in_expr.go index 631f465a3..9e193f619 100644 --- a/rule/bool_literal_in_expr.go +++ b/rule/bool_literal_in_expr.go @@ -11,7 +11,7 @@ import ( type BoolLiteralRule struct{} // Apply applies the rule to given file. -func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -22,7 +22,7 @@ func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure w := &lintBoolLiteral{astFile, onFailure} ast.Walk(w, astFile) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/call_to_gc.go b/rule/call_to_gc.go index ca4a00704..9c68380a4 100644 --- a/rule/call_to_gc.go +++ b/rule/call_to_gc.go @@ -10,7 +10,7 @@ import ( type CallToGCRule struct{} // Apply applies the rule to given file. -func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -23,7 +23,7 @@ func (*CallToGCRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, e w := lintCallToGC{onFailure, gcTriggeringFunctions} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index 13397ed03..efbb70f4a 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -35,12 +35,12 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -55,7 +55,7 @@ func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Argument linter.lintCognitiveComplexity() - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index 7baca2147..132cc104d 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -29,12 +29,12 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { } // Apply the rule. -func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -69,7 +69,7 @@ func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) ( }) } } - return failures, nil + return failures } // Name yields this rule name. diff --git a/rule/comments_density.go b/rule/comments_density.go index 5ee398a22..fd36df92b 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -33,12 +33,12 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } commentsLines := countDocLines(file.AST.Comments) @@ -53,10 +53,10 @@ func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) ( Failure: fmt.Sprintf("the file has a comment density of %2.f%% (%d comment lines for %d code lines) but expected a minimum of %d%%", density, commentsLines, statementsCount, r.minimumCommentsDensity), }, - }, nil + } } - return nil, nil + return nil } // Name returns the rule name. diff --git a/rule/confusing_naming.go b/rule/confusing_naming.go index 2cdcbd458..6a99793e1 100644 --- a/rule/confusing_naming.go +++ b/rule/confusing_naming.go @@ -47,7 +47,7 @@ var allPkgs = packages{pkgs: make([]pkgMethods, 1)} type ConfusingNamingRule struct{} // Apply applies the rule to given file. -func (*ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST pkgm := allPkgs.methodNames(file.Pkg) @@ -61,7 +61,7 @@ func (*ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fai ast.Walk(&walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/confusing_results.go b/rule/confusing_results.go index 33e766a00..1f5c16ba4 100644 --- a/rule/confusing_results.go +++ b/rule/confusing_results.go @@ -10,7 +10,7 @@ import ( type ConfusingResultsRule struct{} // Apply applies the rule to given file. -func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { @@ -45,7 +45,7 @@ func (*ConfusingResultsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fa } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/constant_logical_expr.go b/rule/constant_logical_expr.go index aa4d54159..9e34d3d16 100644 --- a/rule/constant_logical_expr.go +++ b/rule/constant_logical_expr.go @@ -11,7 +11,7 @@ import ( type ConstantLogicalExprRule struct{} // Apply applies the rule to given file. -func (*ConstantLogicalExprRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ConstantLogicalExprRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,7 +21,7 @@ func (*ConstantLogicalExprRule) Apply(file *lint.File, _ lint.Arguments) ([]lint astFile := file.AST w := &lintConstantLogicalExpr{astFile, onFailure} ast.Walk(w, astFile) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index 64cc3f50b..960dbfb5d 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -17,12 +17,12 @@ type ContextAsArgumentRule struct { } // Apply applies the rule to given file. -func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -56,7 +56,7 @@ func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/context_keys_type.go b/rule/context_keys_type.go index 8208ae101..43369e70b 100644 --- a/rule/context_keys_type.go +++ b/rule/context_keys_type.go @@ -12,7 +12,7 @@ import ( type ContextKeysType struct{} // Apply applies the rule to given file. -func (*ContextKeysType) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ContextKeysType) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST @@ -27,7 +27,7 @@ func (*ContextKeysType) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure file.Pkg.TypeCheck() ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 69cfa96e5..84624193a 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -35,12 +35,12 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -62,7 +62,7 @@ func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) ([]lin } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/datarace.go b/rule/datarace.go index 589aca6f0..e6b4dcf46 100644 --- a/rule/datarace.go +++ b/rule/datarace.go @@ -11,7 +11,7 @@ import ( type DataRaceRule struct{} // Apply applies the rule to given file. -func (r *DataRaceRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (r *DataRaceRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { isGo122 := file.Pkg.IsAtLeastGo122() var failures []lint.Failure for _, decl := range file.AST.Decls { @@ -41,7 +41,7 @@ func (r *DataRaceRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, ast.Walk(fl, funcDecl.Body) } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/deep_exit.go b/rule/deep_exit.go index 988754309..bc0960433 100644 --- a/rule/deep_exit.go +++ b/rule/deep_exit.go @@ -14,7 +14,7 @@ import ( type DeepExitRule struct{} // Apply applies the rule to given file. -func (*DeepExitRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*DeepExitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -22,7 +22,7 @@ func (*DeepExitRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, e w := &lintDeepExit{onFailure: onFailure, isTestFile: file.IsTest()} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/defer.go b/rule/defer.go index 6fcd9116f..612bdfdd3 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -25,12 +25,12 @@ func (r *DeferRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -41,7 +41,7 @@ func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Fai ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 8b1f6bdc8..7f5e48ca4 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -16,12 +16,12 @@ type DotImportsRule struct { } // Apply applies the rule to given file. -func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -38,7 +38,7 @@ func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lin ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/duplicated_imports.go b/rule/duplicated_imports.go index 79c29fde9..55ecb5547 100644 --- a/rule/duplicated_imports.go +++ b/rule/duplicated_imports.go @@ -10,7 +10,7 @@ import ( type DuplicatedImportsRule struct{} // Apply applies the rule to given file. -func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure impPaths := map[string]struct{}{} @@ -30,7 +30,7 @@ func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.F impPaths[path] = struct{}{} } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/early_return.go b/rule/early_return.go index ae5686b0c..c6c23212d 100644 --- a/rule/early_return.go +++ b/rule/early_return.go @@ -12,8 +12,8 @@ import ( type EarlyReturnRule struct{} // Apply applies the rule to given file. -func (e *EarlyReturnRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetIf, args), nil +func (e *EarlyReturnRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { + return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetIf, args) } // Name returns the rule name. diff --git a/rule/empty_block.go b/rule/empty_block.go index 19a8c7fa6..8d2d9129f 100644 --- a/rule/empty_block.go +++ b/rule/empty_block.go @@ -10,7 +10,7 @@ import ( type EmptyBlockRule struct{} // Apply applies the rule to given file. -func (*EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +19,7 @@ func (*EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, w := lintEmptyBlock{map[*ast.BlockStmt]bool{}, onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/empty_lines.go b/rule/empty_lines.go index 0c904d034..2710a8979 100644 --- a/rule/empty_lines.go +++ b/rule/empty_lines.go @@ -11,7 +11,7 @@ import ( type EmptyLinesRule struct{} // Apply applies the rule to given file. -func (r *EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (r *EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -20,7 +20,7 @@ func (r *EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failur w := lintEmptyLines{file, r.commentLines(file.CommentMap(), file), onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index bc16080fe..53a1abe67 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -65,17 +65,17 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } if r.enforceMapStyle == enforceMapStyleTypeAny { // this linter is not configured - return nil, nil + return nil } var failures []lint.Failure astFile := file.AST @@ -133,7 +133,7 @@ func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) ( return true }) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index cd3d10e9f..b9677a9ba 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -101,17 +101,17 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er } // Apply applies the rule to a given file. -func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. - return nil, nil + return nil } var failures []lint.Failure @@ -191,7 +191,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint. return true }) - return failures, nil + return failures } // Name returns the name of the linter rule. diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 1e7c8eeaf..bb8f15f6a 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -68,17 +68,17 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } if r.enforceSliceStyle == enforceSliceStyleTypeAny { // this linter is not configured - return nil, nil + return nil } var failures []lint.Failure @@ -179,7 +179,7 @@ func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) return true }) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/error_naming.go b/rule/error_naming.go index 5b1e17197..e64fdf2d7 100644 --- a/rule/error_naming.go +++ b/rule/error_naming.go @@ -13,7 +13,7 @@ import ( type ErrorNamingRule struct{} // Apply applies the rule to given file. -func (*ErrorNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ErrorNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST @@ -27,7 +27,7 @@ func (*ErrorNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/error_return.go b/rule/error_return.go index f661927fa..c5d3f46c4 100644 --- a/rule/error_return.go +++ b/rule/error_return.go @@ -10,7 +10,7 @@ import ( type ErrorReturnRule struct{} // Apply applies the rule to given file. -func (*ErrorReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ErrorReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { @@ -42,7 +42,7 @@ func (*ErrorReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/error_strings.go b/rule/error_strings.go index e484b9aa1..dc4e83ce0 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -53,12 +53,12 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -75,7 +75,7 @@ func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) ([]l ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/errorf.go b/rule/errorf.go index 20a8658b3..228a3c7ed 100644 --- a/rule/errorf.go +++ b/rule/errorf.go @@ -13,7 +13,7 @@ import ( type ErrorfRule struct{} // Apply applies the rule to given file. -func (*ErrorfRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ErrorfRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST @@ -28,7 +28,7 @@ func (*ErrorfRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, err file.Pkg.TypeCheck() ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/exported.go b/rule/exported.go index 42e506058..c999ac6d3 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -107,17 +107,17 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure if file.IsTest() { - return failures, nil + return failures } fileAst := file.AST @@ -135,7 +135,7 @@ func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint. ast.Walk(&walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/file_header.go b/rule/file_header.go index 93e33c923..9bf790a6d 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -34,16 +34,16 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } if r.header == "" { - return nil, nil + return nil } failure := []lint.Failure{ @@ -55,12 +55,12 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lin } if len(file.AST.Comments) == 0 { - return failure, nil + return failure } g := file.AST.Comments[0] if g == nil { - return failure, nil + return failure } comment := "" for _, c := range g.List { @@ -75,13 +75,13 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) ([]lin regex, err := regexp.Compile(r.header) if err != nil { - return nil, err + return []lint.Failure{lint.NewInternalFailure(err.Error())} } if !regex.MatchString(comment) { - return failure, nil + return failure } - return nil, nil + return nil } // Name returns the rule name. diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index 6e157c595..7beb98bb7 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -25,17 +25,17 @@ type FileLengthLimitRule struct { } // Apply applies the rule to given file. -func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } if r.max <= 0 { // when max is negative or 0 the rule is disabled - return nil, nil + return nil } all := 0 @@ -49,7 +49,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ( } if err := scanner.Err(); err != nil { - return nil, err + return []lint.Failure{lint.NewInternalFailure(err.Error())} } lines := all @@ -62,7 +62,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ( } if lines <= r.max { - return nil, nil + return nil } return []lint.Failure{ @@ -77,7 +77,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ( }, Failure: fmt.Sprintf("file length is %d lines, which exceeds the limit of %d", lines, r.max), }, - }, nil + } } func (r *FileLengthLimitRule) configure(arguments lint.Arguments) error { diff --git a/rule/filename_format.go b/rule/filename_format.go index 4394e7435..20d207c85 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -18,17 +18,17 @@ type FilenameFormatRule struct { } // Apply applies the rule to the given file. -func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } filename := filepath.Base(file.Name) if r.format.MatchString(filename) { - return nil, nil + return nil } failureMsg := fmt.Sprintf("Filename %s is not of the format %s.%s", filename, r.format.String(), r.getMsgForNonASCIIChars(filename)) @@ -37,7 +37,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([ Failure: failureMsg, RuleName: r.Name(), Node: file.AST.Name, - }}, nil + }} } func (r *FilenameFormatRule) getMsgForNonASCIIChars(str string) string { diff --git a/rule/flag_param.go b/rule/flag_param.go index 8d495cfed..0a67f3322 100644 --- a/rule/flag_param.go +++ b/rule/flag_param.go @@ -11,7 +11,7 @@ import ( type FlagParamRule struct{} // Apply applies the rule to given file. -func (*FlagParamRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*FlagParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -43,7 +43,7 @@ func (*FlagParamRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, ast.Walk(cv, fd.Body) } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/function_length.go b/rule/function_length.go index 6a14bdf32..4e30d071d 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -28,12 +28,12 @@ func (r *FunctionLength) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -46,7 +46,7 @@ func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lin body := funcDecl.Body emptyBody := body == nil || len(body.List) == 0 if emptyBody { - return nil, nil + return nil } if r.maxStmt > 0 { @@ -72,7 +72,7 @@ func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) ([]lin } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 10f4f7016..c2d7cfcc9 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -17,8 +17,13 @@ type FunctionResultsLimitRule struct { } // Apply applies the rule to given file. -func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { - r.configureOnce.Do(func() { r.configure(arguments) }) +func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { + var configureErr error + r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) + + if configureErr != nil { + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + } var failures []lint.Failure for _, decl := range file.AST.Decls { @@ -44,7 +49,7 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen }) } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/get_return.go b/rule/get_return.go index 0e27cdf4a..b16e2712a 100644 --- a/rule/get_return.go +++ b/rule/get_return.go @@ -12,7 +12,7 @@ import ( type GetReturnRule struct{} // Apply applies the rule to given file. -func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { @@ -37,7 +37,7 @@ func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, }) } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/identical_branches.go b/rule/identical_branches.go index 4144c44cb..c6008925f 100644 --- a/rule/identical_branches.go +++ b/rule/identical_branches.go @@ -10,7 +10,7 @@ import ( type IdenticalBranchesRule struct{} // Apply applies the rule to given file. -func (*IdenticalBranchesRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*IdenticalBranchesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -20,7 +20,7 @@ func (*IdenticalBranchesRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.F astFile := file.AST w := &lintIdenticalBranches{astFile, onFailure} ast.Walk(w, astFile) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/if_return.go b/rule/if_return.go index cb0aba042..f9e5ef233 100644 --- a/rule/if_return.go +++ b/rule/if_return.go @@ -12,7 +12,7 @@ import ( type IfReturnRule struct{} // Apply applies the rule to given file. -func (*IfReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*IfReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -22,7 +22,7 @@ func (*IfReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, e astFile := file.AST w := &lintElseError{astFile, onFailure} ast.Walk(w, astFile) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 2c76eb1a5..84b11ef01 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -61,12 +61,12 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -101,7 +101,7 @@ func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/import_shadowing.go b/rule/import_shadowing.go index c15a594a0..474d5b85f 100644 --- a/rule/import_shadowing.go +++ b/rule/import_shadowing.go @@ -13,7 +13,7 @@ import ( type ImportShadowingRule struct{} // Apply applies the rule to given file. -func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure importNames := map[string]struct{}{} @@ -34,7 +34,7 @@ func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fai ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 37b9d0734..185a62409 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -43,12 +43,12 @@ func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { } // Apply applies the rule to given file. -func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -65,7 +65,7 @@ func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/increment_decrement.go b/rule/increment_decrement.go index d55f246ed..8158cca09 100644 --- a/rule/increment_decrement.go +++ b/rule/increment_decrement.go @@ -12,7 +12,7 @@ import ( type IncrementDecrementRule struct{} // Apply applies the rule to given file. -func (*IncrementDecrementRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*IncrementDecrementRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST @@ -25,7 +25,7 @@ func (*IncrementDecrementRule) Apply(file *lint.File, _ lint.Arguments) ([]lint. ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/indent_error_flow.go b/rule/indent_error_flow.go index f0d02c41a..5e554d5de 100644 --- a/rule/indent_error_flow.go +++ b/rule/indent_error_flow.go @@ -9,8 +9,8 @@ import ( type IndentErrorFlowRule struct{} // Apply applies the rule to given file. -func (e *IndentErrorFlowRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetElse, args), nil +func (e *IndentErrorFlowRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { + return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetElse, args) } // Name returns the rule name. diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index c8ff21c95..c55b8706e 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -38,12 +38,12 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -58,7 +58,7 @@ func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) ( checker.check() - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index ec6c43de5..a3755dcb5 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -19,12 +19,12 @@ type MaxControlNestingRule struct { const defaultMaxControlNesting = 5 // Apply applies the rule to given file. -func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -40,7 +40,7 @@ func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 2d2cff6f2..40c140399 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -39,18 +39,18 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure if r.max < 1 { - return failures, nil + return failures } fileAst := file.AST @@ -73,7 +73,7 @@ func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) }) } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/modifies_param.go b/rule/modifies_param.go index acfba982c..caaebe674 100644 --- a/rule/modifies_param.go +++ b/rule/modifies_param.go @@ -11,7 +11,7 @@ import ( type ModifiesParamRule struct{} // Apply applies the rule to given file. -func (*ModifiesParamRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*ModifiesParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -20,7 +20,7 @@ func (*ModifiesParamRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failu w := lintModifiesParamRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/modifies_value_receiver.go b/rule/modifies_value_receiver.go index b24fd9f3b..a33a79ac2 100644 --- a/rule/modifies_value_receiver.go +++ b/rule/modifies_value_receiver.go @@ -12,7 +12,7 @@ import ( type ModifiesValRecRule struct{} // Apply applies the rule to given file. -func (r *ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (r *ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure file.Pkg.TypeCheck() @@ -48,7 +48,7 @@ func (r *ModifiesValRecRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fa } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/nested_structs.go b/rule/nested_structs.go index c58c089c8..147bd482b 100644 --- a/rule/nested_structs.go +++ b/rule/nested_structs.go @@ -10,7 +10,7 @@ import ( type NestedStructs struct{} // Apply applies the rule to given file. -func (*NestedStructs) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*NestedStructs) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure walker := &lintNestedStructs{ @@ -21,7 +21,7 @@ func (*NestedStructs) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, ast.Walk(walker, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/optimize_operands_order.go b/rule/optimize_operands_order.go index 140acdc63..3c8f30f8a 100644 --- a/rule/optimize_operands_order.go +++ b/rule/optimize_operands_order.go @@ -12,7 +12,7 @@ import ( type OptimizeOperandsOrderRule struct{} // Apply applies the rule to given file. -func (*OptimizeOperandsOrderRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*OptimizeOperandsOrderRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -22,7 +22,7 @@ func (*OptimizeOperandsOrderRule) Apply(file *lint.File, _ lint.Arguments) ([]li onFailure: onFailure, } ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/package_comments.go b/rule/package_comments.go index afa17912a..f1e5462fe 100644 --- a/rule/package_comments.go +++ b/rule/package_comments.go @@ -20,11 +20,11 @@ type PackageCommentsRule struct { } // Apply applies the rule to given file. -func (r *PackageCommentsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (r *PackageCommentsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure if file.IsTest() { - return failures, nil + return failures } onFailure := func(failure lint.Failure) { @@ -34,7 +34,7 @@ func (r *PackageCommentsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.F fileAst := file.AST w := &lintPackageComments{fileAst, file, onFailure, r} ast.Walk(w, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/range.go b/rule/range.go index 39b693f57..b54078e4d 100644 --- a/rule/range.go +++ b/rule/range.go @@ -12,7 +12,7 @@ import ( type RangeRule struct{} // Apply applies the rule to given file. -func (*RangeRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*RangeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,7 +21,7 @@ func (*RangeRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, erro w := &lintRanges{file, onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/range_val_address.go b/rule/range_val_address.go index 327c5afbb..d2ab0392a 100644 --- a/rule/range_val_address.go +++ b/rule/range_val_address.go @@ -13,11 +13,11 @@ import ( type RangeValAddress struct{} // Apply applies the rule to given file. -func (*RangeValAddress) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*RangeValAddress) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure if file.Pkg.IsAtLeastGo122() { - return failures, nil + return failures } walker := rangeValAddress{ @@ -30,7 +30,7 @@ func (*RangeValAddress) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure file.Pkg.TypeCheck() ast.Walk(walker, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/range_val_in_closure.go b/rule/range_val_in_closure.go index 5a34011ce..92078288f 100644 --- a/rule/range_val_in_closure.go +++ b/rule/range_val_in_closure.go @@ -11,11 +11,11 @@ import ( type RangeValInClosureRule struct{} // Apply applies the rule to given file. -func (*RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure if file.Pkg.IsAtLeastGo122() { - return failures, nil + return failures } walker := rangeValInClosure{ @@ -26,7 +26,7 @@ func (*RangeValInClosureRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.F ast.Walk(walker, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index f9952c759..0dd415b72 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -45,12 +45,12 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } typeReceiver := map[string]string{} @@ -111,7 +111,7 @@ func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([ typeReceiver[recv] = name } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/redefines_builtin_id.go b/rule/redefines_builtin_id.go index 097432504..10ea16ae1 100644 --- a/rule/redefines_builtin_id.go +++ b/rule/redefines_builtin_id.go @@ -68,7 +68,7 @@ var builtInTypes = map[string]bool{ type RedefinesBuiltinIDRule struct{} // Apply applies the rule to given file. -func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -89,7 +89,7 @@ func (*RedefinesBuiltinIDRule) Apply(file *lint.File, _ lint.Arguments) ([]lint. } ast.Walk(w, astFile) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/redundant_build_tag.go b/rule/redundant_build_tag.go index 26f248a17..52412b9b3 100644 --- a/rule/redundant_build_tag.go +++ b/rule/redundant_build_tag.go @@ -12,7 +12,7 @@ type RedundantBuildTagRule struct{} // Apply triggers if an old build tag `// +build` is found after a new one `//go:build`. // `//go:build` comments are automatically added by gofmt when Go 1.17+ is used. // See https://pkg.go.dev/cmd/go#hdr-Build_constraints -func (*RedundantBuildTagRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*RedundantBuildTagRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { for _, group := range file.AST.Comments { hasGoBuild := false for _, comment := range group.List { @@ -27,12 +27,12 @@ func (*RedundantBuildTagRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.F Confidence: 1, Node: comment, Failure: `The build tag "// +build" is redundant since Go 1.17 and can be removed`, - }}, nil + }} } } } - return []lint.Failure{}, nil + return []lint.Failure{} } // Name returns the rule name. diff --git a/rule/redundant_import_alias.go b/rule/redundant_import_alias.go index 59e45fb65..93f89366b 100644 --- a/rule/redundant_import_alias.go +++ b/rule/redundant_import_alias.go @@ -12,7 +12,7 @@ import ( type RedundantImportAlias struct{} // Apply applies the rule to given file. -func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, imp := range file.AST.Imports { @@ -30,7 +30,7 @@ func (*RedundantImportAlias) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fa } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/string_format.go b/rule/string_format.go index 55027d823..92a5b8cef 100644 --- a/rule/string_format.go +++ b/rule/string_format.go @@ -17,7 +17,7 @@ import ( type StringFormatRule struct{} // Apply applies the rule to the given file. -func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -27,12 +27,12 @@ func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) ([]lin w := lintStringFormatRule{onFailure: onFailure} err := w.parseArguments(arguments) if err != nil { - return failures, err + return []lint.Failure{lint.NewInternalFailure(err.Error())} } ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. @@ -49,7 +49,7 @@ func (StringFormatRule) ParseArgumentsTest(arguments lint.Arguments) *string { defer func() { err := w.parseArguments(arguments) c <- err - }() + }() }() err := <-c if err != nil { diff --git a/rule/string_of_int.go b/rule/string_of_int.go index a01017b69..3bec1d6ac 100644 --- a/rule/string_of_int.go +++ b/rule/string_of_int.go @@ -11,7 +11,7 @@ import ( type StringOfIntRule struct{} // Apply applies the rule to given file. -func (*StringOfIntRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*StringOfIntRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -24,7 +24,7 @@ func (*StringOfIntRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure w := &lintStringInt{file, onFailure} ast.Walk(w, astFile) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 12c354414..c76b5fa1c 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -47,12 +47,12 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -67,7 +67,7 @@ func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/superfluous_else.go b/rule/superfluous_else.go index 7d3836f81..2e8cfeb44 100644 --- a/rule/superfluous_else.go +++ b/rule/superfluous_else.go @@ -11,8 +11,8 @@ import ( type SuperfluousElseRule struct{} // Apply applies the rule to given file. -func (e *SuperfluousElseRule) Apply(file *lint.File, args lint.Arguments) ([]lint.Failure, error) { - return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetElse, args), nil +func (e *SuperfluousElseRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { + return ifelse.Apply(e.checkIfElse, file.AST, ifelse.TargetElse, args) } // Name returns the rule name. diff --git a/rule/time_equal.go b/rule/time_equal.go index 4aa8bc384..a4fab88b3 100644 --- a/rule/time_equal.go +++ b/rule/time_equal.go @@ -12,7 +12,7 @@ import ( type TimeEqualRule struct{} // Apply applies the rule to given file. -func (*TimeEqualRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*TimeEqualRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,11 +21,11 @@ func (*TimeEqualRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, w := &lintTimeEqual{file, onFailure} if w.file.Pkg.TypeCheck() != nil { - return nil, nil + return nil } ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/time_naming.go b/rule/time_naming.go index 99a22f010..67e79eab0 100644 --- a/rule/time_naming.go +++ b/rule/time_naming.go @@ -13,7 +13,7 @@ import ( type TimeNamingRule struct{} // Apply applies the rule to given file. -func (*TimeNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*TimeNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -24,7 +24,7 @@ func (*TimeNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, file.Pkg.TypeCheck() ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index 6d1540f3e..dc14afc47 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -46,12 +46,12 @@ func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -65,7 +65,7 @@ func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Argum ast.Walk(walker, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unconditional_recursion.go b/rule/unconditional_recursion.go index d0c973480..9cdee38b2 100644 --- a/rule/unconditional_recursion.go +++ b/rule/unconditional_recursion.go @@ -10,7 +10,7 @@ import ( type UnconditionalRecursionRule struct{} // Apply applies the rule to given file. -func (*UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +19,7 @@ func (*UnconditionalRecursionRule) Apply(file *lint.File, _ lint.Arguments) ([]l w := lintUnconditionalRecursionRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unexported_naming.go b/rule/unexported_naming.go index 7c95a61c1..0c2b39d41 100644 --- a/rule/unexported_naming.go +++ b/rule/unexported_naming.go @@ -12,7 +12,7 @@ import ( type UnexportedNamingRule struct{} // Apply applies the rule to given file. -func (*UnexportedNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UnexportedNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -21,7 +21,7 @@ func (*UnexportedNamingRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fa ba := &unexportablenamingLinter{onFailure} ast.Walk(ba, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unexported_return.go b/rule/unexported_return.go index e10f215bb..448b4c720 100644 --- a/rule/unexported_return.go +++ b/rule/unexported_return.go @@ -13,7 +13,7 @@ import ( type UnexportedReturnRule struct{} // Apply applies the rule to given file. -func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { @@ -58,7 +58,7 @@ func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fa } } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 582e8d318..518488d79 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -42,12 +42,12 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -63,7 +63,7 @@ func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) ([ file.Pkg.TypeCheck() ast.Walk(walker, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unnecessary_stmt.go b/rule/unnecessary_stmt.go index ede7b0a47..8e0784ba4 100644 --- a/rule/unnecessary_stmt.go +++ b/rule/unnecessary_stmt.go @@ -11,7 +11,7 @@ import ( type UnnecessaryStmtRule struct{} // Apply applies the rule to given file. -func (*UnnecessaryStmtRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UnnecessaryStmtRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -19,7 +19,7 @@ func (*UnnecessaryStmtRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fai w := lintUnnecessaryStmtRule{onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unreachable_code.go b/rule/unreachable_code.go index 868a0d755..dcc5b7905 100644 --- a/rule/unreachable_code.go +++ b/rule/unreachable_code.go @@ -10,7 +10,7 @@ import ( type UnreachableCodeRule struct{} // Apply applies the rule to given file. -func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -38,7 +38,7 @@ func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fai w := lintUnreachableCode{onFailure, branchingFunctions} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unused_param.go b/rule/unused_param.go index db29cb580..365802b15 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -50,12 +50,12 @@ func (r *UnusedParamRule) configure(args lint.Arguments) error { } // Apply applies the rule to given file. -func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -71,7 +71,7 @@ func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) ([]li ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 7e5ad80d4..b8fba059e 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -48,12 +48,12 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) error { } // Apply applies the rule to given file. -func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -99,7 +99,7 @@ func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) ([ }) } - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/use_any.go b/rule/use_any.go index 1de42c92e..cd01d5bb0 100644 --- a/rule/use_any.go +++ b/rule/use_any.go @@ -10,7 +10,7 @@ import ( type UseAnyRule struct{} // Apply applies the rule to given file. -func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure walker := lintUseAny{ @@ -21,7 +21,7 @@ func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, err fileAst := file.AST ast.Walk(walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/use_errors_new.go b/rule/use_errors_new.go index 1905d1843..d21bbf7d8 100644 --- a/rule/use_errors_new.go +++ b/rule/use_errors_new.go @@ -10,7 +10,7 @@ import ( type UseErrorsNewRule struct{} // Apply applies the rule to given file. -func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure walker := lintFmtErrorf{ @@ -21,7 +21,7 @@ func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failur ast.Walk(walker, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/useless_break.go b/rule/useless_break.go index b65a3fe99..8db20c9b8 100644 --- a/rule/useless_break.go +++ b/rule/useless_break.go @@ -11,7 +11,7 @@ import ( type UselessBreak struct{} // Apply applies the rule to given file. -func (*UselessBreak) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*UselessBreak) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -21,7 +21,7 @@ func (*UselessBreak) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, e astFile := file.AST w := &lintUselessBreak{onFailure, false} ast.Walk(w, astFile) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/var_declarations.go b/rule/var_declarations.go index 0211dc17a..6b30dfaab 100644 --- a/rule/var_declarations.go +++ b/rule/var_declarations.go @@ -29,7 +29,7 @@ var zeroLiteral = map[string]bool{ type VarDeclarationsRule struct{} // Apply applies the rule to given file. -func (*VarDeclarationsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*VarDeclarationsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST @@ -44,7 +44,7 @@ func (*VarDeclarationsRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fai file.Pkg.TypeCheck() ast.Walk(walker, fileAst) - return failures,nil + return failures } // Name returns the rule name. diff --git a/rule/var_naming.go b/rule/var_naming.go index c0674a786..9e40646cb 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -91,12 +91,12 @@ func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) { } // Apply applies the rule to given file. -func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint.Failure, error) { +func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return nil, configureErr + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } var failures []lint.Failure @@ -120,7 +120,7 @@ func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) ([]lint ast.Walk(&walker, fileAst) - return failures, nil + return failures } // Name returns the rule name. diff --git a/rule/waitgroup_by_value.go b/rule/waitgroup_by_value.go index 27bc56bff..a2d304ae5 100644 --- a/rule/waitgroup_by_value.go +++ b/rule/waitgroup_by_value.go @@ -10,7 +10,7 @@ import ( type WaitGroupByValueRule struct{} // Apply applies the rule to given file. -func (*WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Failure, error) { +func (*WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -19,7 +19,7 @@ func (*WaitGroupByValueRule) Apply(file *lint.File, _ lint.Arguments) ([]lint.Fa w := lintWaitGroupByValueRule{onFailure: onFailure} ast.Walk(w, file.AST) - return failures, nil + return failures } // Name returns the rule name. diff --git a/test/file_filter_test.go b/test/file_filter_test.go index 470b98730..6efdc3111 100644 --- a/test/file_filter_test.go +++ b/test/file_filter_test.go @@ -13,9 +13,9 @@ type TestFileFilterRule struct { var _ lint.Rule = (*TestFileFilterRule)(nil) func (*TestFileFilterRule) Name() string { return "test-file-filter" } -func (tfr *TestFileFilterRule) Apply(*lint.File, lint.Arguments) ([]lint.Failure, error) { +func (tfr *TestFileFilterRule) Apply(*lint.File, lint.Arguments) []lint.Failure { tfr.WasApplied = true - return nil, nil + return nil } func TestFileExcludeFilterAtRuleLevel(t *testing.T) { From 3086a32cd89022c3c683a8da7279ac0e3539ef06 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 20:50:48 +0100 Subject: [PATCH 21/36] cleanup lint/rule.go --- lint/rule.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint/rule.go b/lint/rule.go index 4d9542303..6189b9f7d 100644 --- a/lint/rule.go +++ b/lint/rule.go @@ -14,7 +14,7 @@ type DisabledInterval struct { // Rule defines an abstract rule interface type Rule interface { Name() string - Apply(*File, Arguments) ([]Failure) + Apply(*File, Arguments) []Failure } // ToFailurePosition returns the failure position. From 093971c84b062276cbb1fcea0c66f16f7465617c Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 20:53:13 +0100 Subject: [PATCH 22/36] error msg lowercase --- rule/add_constant.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index 5abda75b1..3f17b9f5b 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -233,35 +233,35 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) error { } list, ok := v.(string) if !ok { - return fmt.Errorf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v) + return fmt.Errorf("invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v) } r.allowList.add(kind, list) case "maxLitCount": sl, ok := v.(string) if !ok { - return fmt.Errorf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v) + return fmt.Errorf("invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v) } limit, err := strconv.Atoi(sl) if err != nil { - return fmt.Errorf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v) + return fmt.Errorf("invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v) } r.strLitLimit = limit case "ignoreFuncs": excludes, ok := v.(string) if !ok { - return fmt.Errorf("Invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v) + return fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v) } for _, exclude := range strings.Split(excludes, ",") { exclude = strings.Trim(exclude, " ") if exclude == "" { - return errors.New("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty") + return errors.New("invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty") } exp, err := regexp.Compile(exclude) if err != nil { - return fmt.Errorf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %w", exclude, err) + return fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %w", exclude, err) } r.ignoreFunctions = append(r.ignoreFunctions, exp) From d78563bc091064b3c19b3a58cc83a07054d6bb95 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 23:04:42 +0100 Subject: [PATCH 23/36] lowercace error msg --- rule/banned_characters.go | 2 +- rule/context_as_argument.go | 6 +++--- rule/defer.go | 4 ++-- rule/dot_imports.go | 6 +++--- rule/enforce_map_style.go | 4 ++-- rule/enforce_repeated_arg_type_style.go | 4 ++-- rule/enforce_slice_style.go | 4 ++-- rule/exported.go | 2 +- rule/import_alias_naming.go | 10 +++++----- rule/imports_blocklist.go | 4 ++-- rule/struct_tag.go | 4 ++-- rule/unhandled_error.go | 6 +++--- rule/var_naming.go | 2 +- 13 files changed, 29 insertions(+), 29 deletions(-) diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 06a38364e..571b87fb8 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -68,7 +68,7 @@ func (r *BannedCharsRule) getBannedCharsList(args lint.Arguments) ([]string, err for _, char := range args { charStr, ok := char.(string) if !ok { - return nil, fmt.Errorf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), char) + return nil, fmt.Errorf("invalid argument for the %s rule: expecting a string, got %T", r.Name(), char) } bannedChars = append(bannedChars, charStr) } diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index 960dbfb5d..02de700d3 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -78,18 +78,18 @@ func (r *ContextAsArgumentRule) getAllowTypesFromArguments(args lint.Arguments) if len(args) >= 1 { argKV, ok := args[0].(map[string]any) if !ok { - return nil, fmt.Errorf("Invalid argument to the context-as-argument rule. Expecting a k,v map, got %T", args[0]) + return nil, fmt.Errorf("invalid argument to the context-as-argument rule. Expecting a k,v map, got %T", args[0]) } for k, v := range argKV { switch k { case "allowTypesBefore": typesBefore, ok := v.(string) if !ok { - return nil, fmt.Errorf("Invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T", v) + return nil, fmt.Errorf("invalid argument to the context-as-argument.allowTypesBefore rule. Expecting a string, got %T", v) } allowTypesBefore = append(allowTypesBefore, strings.Split(typesBefore, ",")...) default: - return nil, fmt.Errorf("Invalid argument to the context-as-argument rule. Unrecognized key %s", k) + return nil, fmt.Errorf("invalid argument to the context-as-argument rule. Unrecognized key %s", k) } } } diff --git a/rule/defer.go b/rule/defer.go index 612bdfdd3..51cf9d9eb 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -65,14 +65,14 @@ func (*DeferRule) allowFromArgs(args lint.Arguments) (map[string]bool, error) { aa, ok := args[0].([]any) if !ok { - return nil, fmt.Errorf("Invalid argument '%v' for 'defer' rule. Expecting []string, got %T", args[0], args[0]) + return nil, fmt.Errorf("invalid argument '%v' for 'defer' rule. Expecting []string, got %T", args[0], args[0]) } allow := make(map[string]bool, len(aa)) for _, subcase := range aa { sc, ok := subcase.(string) if !ok { - return nil, fmt.Errorf("Invalid argument '%v' for 'defer' rule. Expecting string, got %T", subcase, subcase) + return nil, fmt.Errorf("invalid argument '%v' for 'defer' rule. Expecting string, got %T", subcase, subcase) } allow[sc] = true } diff --git a/rule/dot_imports.go b/rule/dot_imports.go index 7f5e48ca4..c39d0253b 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -54,18 +54,18 @@ func (r *DotImportsRule) configure(arguments lint.Arguments) error { args, ok := arguments[0].(map[string]any) if !ok { - return fmt.Errorf("Invalid argument to the dot-imports rule. Expecting a k,v map, got %T", arguments[0]) + return fmt.Errorf("invalid argument to the dot-imports rule. Expecting a k,v map, got %T", arguments[0]) } if allowedPkgArg, ok := args["allowedPackages"]; ok { pkgs, ok := allowedPkgArg.([]any) if !ok { - return fmt.Errorf("Invalid argument to the dot-imports rule, []string expected. Got '%v' (%T)", allowedPkgArg, allowedPkgArg) + return fmt.Errorf("invalid argument to the dot-imports rule, []string expected. Got '%v' (%T)", allowedPkgArg, allowedPkgArg) } for _, p := range pkgs { pkg, ok := p.(string) if !ok { - return fmt.Errorf("Invalid argument to the dot-imports rule, string expected. Got '%v' (%T)", p, p) + return fmt.Errorf("invalid argument to the dot-imports rule, string expected. Got '%v' (%T)", p, p) } r.allowedPackages.add(pkg) } diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index 53a1abe67..64a994595 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -52,13 +52,13 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { enforceMapStyle, ok := arguments[0].(string) if !ok { - return fmt.Errorf("Invalid argument '%v' for 'enforce-map-style' rule. Expecting string, got %T", arguments[0], arguments[0]) + return fmt.Errorf("invalid argument '%v' for 'enforce-map-style' rule. Expecting string, got %T", arguments[0], arguments[0]) } var err error r.enforceMapStyle, err = mapStyleFromString(enforceMapStyle) if err != nil { - return fmt.Errorf("Invalid argument to the enforce-map-style rule: %w", err) + return fmt.Errorf("invalid argument to the enforce-map-style rule: %w", err) } return nil diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index b9677a9ba..d03db6b81 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -35,7 +35,7 @@ func repeatedArgTypeStyleFromString(s string) (enforceRepeatedArgTypeStyleType, }, ) - return "", fmt.Errorf("Invalid argument to the enforce-repeated-arg-type-style rule: %w", err) + return "", fmt.Errorf("invalid argument to the enforce-repeated-arg-type-style rule: %w", err) } } @@ -95,7 +95,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er } } default: - return fmt.Errorf("Invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0]) + return fmt.Errorf("invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0]) } return nil } diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index bb8f15f6a..436b0e6d4 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -56,13 +56,13 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { enforceSliceStyle, ok := arguments[0].(string) if !ok { - return fmt.Errorf("Invalid argument '%v' for 'enforce-slice-style' rule. Expecting string, got %T", arguments[0], arguments[0]) + return fmt.Errorf("invalid argument '%v' for 'enforce-slice-style' rule. Expecting string, got %T", arguments[0], arguments[0]) } var err error r.enforceSliceStyle, err = sliceStyleFromString(enforceSliceStyle) if err != nil { - return fmt.Errorf("Invalid argument to the enforce-slice-style rule: %v", err) + return fmt.Errorf("invalid argument to the enforce-slice-style rule: %v", err) } return nil } diff --git a/rule/exported.go b/rule/exported.go index c999ac6d3..39929cbec 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -99,7 +99,7 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { return fmt.Errorf("Unknown configuration flag %s for %s rule", flag, r.Name()) } default: - return fmt.Errorf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), flag) + return fmt.Errorf("invalid argument for the %s rule: expecting a string, got %T", r.Name(), flag) } } diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 84b11ef01..ab5563cb0 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -51,7 +51,7 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { } } default: - return fmt.Errorf("Invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0]) + return fmt.Errorf("invalid argument '%v' for 'import-alias-naming' rule. Expecting string or map[string]string, got %T", arguments[0], arguments[0]) } if r.allowRegexp == nil && r.denyRegexp == nil { @@ -112,12 +112,12 @@ func (*ImportAliasNamingRule) Name() string { func (r *ImportAliasNamingRule) setAllowRule(value any) error { namingRule, ok := value.(string) if !ok { - return fmt.Errorf("Invalid argument '%v' for import-alias-naming allowRegexp rule. Expecting string, got %T", value, value) + return fmt.Errorf("invalid argument '%v' for import-alias-naming allowRegexp rule. Expecting string, got %T", value, value) } namingRuleRegexp, err := regexp.Compile(namingRule) if err != nil { - return fmt.Errorf("Invalid argument to the import-alias-naming allowRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) + return fmt.Errorf("invalid argument to the import-alias-naming allowRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) } r.allowRegexp = namingRuleRegexp return nil @@ -126,12 +126,12 @@ func (r *ImportAliasNamingRule) setAllowRule(value any) error { func (r *ImportAliasNamingRule) setDenyRule(value any) error { namingRule, ok := value.(string) if !ok { - return fmt.Errorf("Invalid argument '%v' for import-alias-naming denyRegexp rule. Expecting string, got %T", value, value) + return fmt.Errorf("invalid argument '%v' for import-alias-naming denyRegexp rule. Expecting string, got %T", value, value) } namingRuleRegexp, err := regexp.Compile(namingRule) if err != nil { - return fmt.Errorf("Invalid argument to the import-alias-naming denyRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) + return fmt.Errorf("invalid argument to the import-alias-naming denyRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) } r.denyRegexp = namingRuleRegexp return nil diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 185a62409..3e147f1aa 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -22,11 +22,11 @@ func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) error { for _, arg := range arguments { argStr, ok := arg.(string) if !ok { - return fmt.Errorf("Invalid argument to the imports-blocklist rule. Expecting a string, got %T", arg) + return fmt.Errorf("invalid argument to the imports-blocklist rule. Expecting a string, got %T", arg) } regStr, err := regexp.Compile(fmt.Sprintf(`(?m)"%s"$`, replaceImportRegexp.ReplaceAllString(argStr, `(\W|\w)*`))) if err != nil { - return fmt.Errorf("Invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %w", argStr, err) + return fmt.Errorf("invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %w", argStr, err) } r.blocklist = append(r.blocklist, regStr) } diff --git a/rule/struct_tag.go b/rule/struct_tag.go index c76b5fa1c..a7d0939c3 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -31,11 +31,11 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { for _, arg := range arguments { item, ok := arg.(string) if !ok { - return fmt.Errorf("Invalid argument to the %s rule. Expecting a string, got %v (of type %T)", r.Name(), arg, arg) + return fmt.Errorf("invalid argument to the %s rule. Expecting a string, got %v (of type %T)", r.Name(), arg, arg) } parts := strings.Split(item, ",") if len(parts) < 2 { - return fmt.Errorf("Invalid argument to the %s rule. Expecting a string of the form key[,option]+, got %s", r.Name(), item) + return fmt.Errorf("invalid argument to the %s rule. Expecting a string of the form key[,option]+, got %s", r.Name(), item) } key := strings.TrimSpace(parts[0]) for i := 1; i < len(parts); i++ { diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 518488d79..9ab865e64 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -23,17 +23,17 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { for _, arg := range arguments { argStr, ok := arg.(string) if !ok { - return fmt.Errorf("Invalid argument to the unhandled-error rule. Expecting a string, got %T", arg) + return fmt.Errorf("invalid argument to the unhandled-error rule. Expecting a string, got %T", arg) } argStr = strings.Trim(argStr, " ") if argStr == "" { - return errors.New("Invalid argument to the unhandled-error rule, expected regular expression must not be empty") + return errors.New("invalid argument to the unhandled-error rule, expected regular expression must not be empty") } exp, err := regexp.Compile(argStr) if err != nil { - return fmt.Errorf("Invalid argument to the unhandled-error rule: regexp %q does not compile: %w", argStr, err) + return fmt.Errorf("invalid argument to the unhandled-error rule: regexp %q does not compile: %w", argStr, err) } r.ignoreList = append(r.ignoreList, exp) diff --git a/rule/var_naming.go b/rule/var_naming.go index 9e40646cb..a8e15756c 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -280,7 +280,7 @@ func (w *lintNames) Visit(n ast.Node) ast.Visitor { func getList(arg any, argName string) ([]string, error) { args, ok := arg.([]any) if !ok { - return nil, fmt.Errorf("Invalid argument to the var-naming rule. Expecting a %s of type slice with initialisms, got %T", argName, arg) + return nil, fmt.Errorf("invalid argument to the var-naming rule. Expecting a %s of type slice with initialisms, got %T", argName, arg) } var list []string for _, v := range args { From 5685fad056fec9d7a21bdbe005bd498536dfa502 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 23:14:10 +0100 Subject: [PATCH 24/36] .gitignore: add .idea dir --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 71f016325..796788388 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ vendor *.swp dist/ *.log +.idea/ From 68e556a26f21a6b0bd6d4612e2a4cefcd5917b2d Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 23:16:13 +0100 Subject: [PATCH 25/36] merge with upstream --- lint/file.go | 1 - 1 file changed, 1 deletion(-) diff --git a/lint/file.go b/lint/file.go index be0b4d517..51595441c 100644 --- a/lint/file.go +++ b/lint/file.go @@ -107,7 +107,6 @@ func (f *File) lint(rules []Rule, config Config, failures chan Failure) error { continue } currentFailures := currentRule.Apply(f, ruleConfig.Arguments) - for idx, failure := range currentFailures { if failure.IsInternal() { return errors.New(failure.Failure) From 31917972586052c22f9c1fcec6945abea3a102ea Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 23:18:31 +0100 Subject: [PATCH 26/36] merge with upstream --- lint/linter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint/linter.go b/lint/linter.go index dfcdb41ef..a07b36b94 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -106,7 +106,7 @@ func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-cha wg.Add(1) go func(pkg []string, gover *goversion.Version) { if err := l.lintPackage(pkg, gover, ruleSet, config, failures); err != nil { - fmt.Fprintln(os.Stderr, "Error during linting:"+err.Error()) + fmt.Fprintln(os.Stderr, "error during linting:"+err.Error()) os.Exit(1) } wg.Done() From f6c7ee075a32241c4e48b138a021675e8fc965f4 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 23:39:01 +0100 Subject: [PATCH 27/36] lowercase in error msg --- rule/enforce_repeated_arg_type_style.go | 6 ++-- rule/exported.go | 2 +- rule/import_alias_naming.go | 2 +- rule/receiver_naming.go | 6 ++-- rule/unchecked_type_assertion.go | 42 ++++++++++++------------- rule/var_naming.go | 8 ++--- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index d03db6b81..25f9cdf2d 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -73,7 +73,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er case "funcArgStyle": val, ok := v.(string) if !ok { - return fmt.Errorf("Invalid map value type for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v) + return fmt.Errorf("invalid map value type for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v) } valstyle, err := repeatedArgTypeStyleFromString(val) if err != nil { @@ -83,7 +83,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er case "funcRetValStyle": val, ok := v.(string) if !ok { - return fmt.Errorf("Invalid map value '%v' for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v, v) + return fmt.Errorf("invalid map value '%v' for 'enforce-repeated-arg-type-style' rule. Expecting string, got %T", v, v) } argstyle, err := repeatedArgTypeStyleFromString(val) if err != nil { @@ -91,7 +91,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) er } r.funcRetValStyle = argstyle default: - return fmt.Errorf("Invalid map key for 'enforce-repeated-arg-type-style' rule. Expecting 'funcArgStyle' or 'funcRetValStyle', got %v", k) + return fmt.Errorf("invalid map key for 'enforce-repeated-arg-type-style' rule. Expecting 'funcArgStyle' or 'funcRetValStyle', got %v", k) } } default: diff --git a/rule/exported.go b/rule/exported.go index 39929cbec..87bdd4e20 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -96,7 +96,7 @@ func (r *ExportedRule) configure(arguments lint.Arguments) error { case "disableChecksOnVariables": r.disabledChecks.Var = true default: - return fmt.Errorf("Unknown configuration flag %s for %s rule", flag, r.Name()) + return fmt.Errorf("unknown configuration flag %s for %s rule", flag, r.Name()) } default: return fmt.Errorf("invalid argument for the %s rule: expecting a string, got %T", r.Name(), flag) diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index ab5563cb0..43bf9d545 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -47,7 +47,7 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { } default: - return fmt.Errorf("Invalid map key for 'import-alias-naming' rule. Expecting 'allowRegex' or 'denyRegex', got %v", k) + return fmt.Errorf("invalid map key for 'import-alias-naming' rule. Expecting 'allowRegex' or 'denyRegex', got %v", k) } } default: diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 0dd415b72..54eee4eff 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -26,7 +26,7 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { args, ok := arguments[0].(map[string]any) if !ok { - return fmt.Errorf("Unable to get arguments for rule %s. Expected object of key-value-pairs", r.Name()) + return fmt.Errorf("unable to get arguments for rule %s. Expected object of key-value-pairs", r.Name()) } for k, v := range args { @@ -34,11 +34,11 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { case "maxLength": value, ok := v.(int64) if !ok { - return fmt.Errorf("Invalid value %v for argument %s of rule %s, expected integer value got %T", v, k, r.Name(), v) + return fmt.Errorf("invalid value %v for argument %s of rule %s, expected integer value got %T", v, k, r.Name(), v) } r.receiverNameMaxLength = int(value) default: - return fmt.Errorf("Unknown argument %s for %s rule", k, r.Name()) + return fmt.Errorf("unknown argument %s for %s rule", k, r.Name()) } } return nil diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index dc14afc47..2f25a9a95 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -14,21 +14,21 @@ const ( ruleUTAMessageIgnored = "type assertion result ignored" ) -// UncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. -type UncheckedTypeAssertionRule struct { +// uncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. +type uncheckedTypeAssertionRule struct { acceptIgnoredAssertionResult bool configureOnce sync.Once } -func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { +func (r *uncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { if len(arguments) == 0 { return nil } args, ok := arguments[0].(map[string]any) if !ok { - return errors.New("Unable to get arguments. Expected object of key-value-pairs") + return errors.New("unable to get arguments. Expected object of key-value-pairs") } for k, v := range args { @@ -36,17 +36,17 @@ func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { case "acceptIgnoredAssertionResult": r.acceptIgnoredAssertionResult, ok = v.(bool) if !ok { - return fmt.Errorf("Unable to parse argument '%s'. Expected boolean", k) + return fmt.Errorf("unable to parse argument '%s'. Expected boolean", k) } default: - return fmt.Errorf("Unknown argument: %s", k) + return fmt.Errorf("unknown argument: %s", k) } } return nil } // Apply applies the rule to given file. -func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *uncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) @@ -56,7 +56,7 @@ func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Argum var failures []lint.Failure - walker := &lintUncheckedTypeAssertion{ + walker := &lintuncheckedTypeAssertion{ onFailure: func(failure lint.Failure) { failures = append(failures, failure) }, @@ -69,11 +69,11 @@ func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Argum } // Name returns the rule name. -func (*UncheckedTypeAssertionRule) Name() string { +func (*uncheckedTypeAssertionRule) Name() string { return "unchecked-type-assertion" } -type lintUncheckedTypeAssertion struct { +type lintuncheckedTypeAssertion struct { onFailure func(lint.Failure) acceptIgnoredTypeAssertionResult bool } @@ -91,14 +91,14 @@ func isTypeSwitch(e *ast.TypeAssertExpr) bool { return e.Type == nil } -func (w *lintUncheckedTypeAssertion) requireNoTypeAssert(expr ast.Expr) { +func (w *lintuncheckedTypeAssertion) requireNoTypeAssert(expr ast.Expr) { e, ok := expr.(*ast.TypeAssertExpr) if ok && !isTypeSwitch(e) { w.addFailure(e, ruleUTAMessagePanic) } } -func (w *lintUncheckedTypeAssertion) handleIfStmt(n *ast.IfStmt) { +func (w *lintuncheckedTypeAssertion) handleIfStmt(n *ast.IfStmt) { ifCondition, ok := n.Cond.(*ast.BinaryExpr) if ok { w.requireNoTypeAssert(ifCondition.X) @@ -106,7 +106,7 @@ func (w *lintUncheckedTypeAssertion) handleIfStmt(n *ast.IfStmt) { } } -func (w *lintUncheckedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion(expr ast.Expr) { +func (w *lintuncheckedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion(expr ast.Expr) { binaryExpr, ok := expr.(*ast.BinaryExpr) if ok { w.requireNoTypeAssert(binaryExpr.X) @@ -114,19 +114,19 @@ func (w *lintUncheckedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion } } -func (w *lintUncheckedTypeAssertion) handleCaseClause(n *ast.CaseClause) { +func (w *lintuncheckedTypeAssertion) handleCaseClause(n *ast.CaseClause) { for _, expr := range n.List { w.requireNoTypeAssert(expr) w.requireBinaryExpressionWithoutTypeAssertion(expr) } } -func (w *lintUncheckedTypeAssertion) handleSwitch(n *ast.SwitchStmt) { +func (w *lintuncheckedTypeAssertion) handleSwitch(n *ast.SwitchStmt) { w.requireNoTypeAssert(n.Tag) w.requireBinaryExpressionWithoutTypeAssertion(n.Tag) } -func (w *lintUncheckedTypeAssertion) handleAssignment(n *ast.AssignStmt) { +func (w *lintuncheckedTypeAssertion) handleAssignment(n *ast.AssignStmt) { if len(n.Rhs) == 0 { return } @@ -150,21 +150,21 @@ func (w *lintUncheckedTypeAssertion) handleAssignment(n *ast.AssignStmt) { } // handles "return foo(.*bar)" - one of them is enough to fail as golang does not forward the type cast tuples in return statements -func (w *lintUncheckedTypeAssertion) handleReturn(n *ast.ReturnStmt) { +func (w *lintuncheckedTypeAssertion) handleReturn(n *ast.ReturnStmt) { for _, r := range n.Results { w.requireNoTypeAssert(r) } } -func (w *lintUncheckedTypeAssertion) handleRange(n *ast.RangeStmt) { +func (w *lintuncheckedTypeAssertion) handleRange(n *ast.RangeStmt) { w.requireNoTypeAssert(n.X) } -func (w *lintUncheckedTypeAssertion) handleChannelSend(n *ast.SendStmt) { +func (w *lintuncheckedTypeAssertion) handleChannelSend(n *ast.SendStmt) { w.requireNoTypeAssert(n.Value) } -func (w *lintUncheckedTypeAssertion) Visit(node ast.Node) ast.Visitor { +func (w *lintuncheckedTypeAssertion) Visit(node ast.Node) ast.Visitor { switch n := node.(type) { case *ast.RangeStmt: w.handleRange(n) @@ -185,7 +185,7 @@ func (w *lintUncheckedTypeAssertion) Visit(node ast.Node) ast.Visitor { return w } -func (w *lintUncheckedTypeAssertion) addFailure(n *ast.TypeAssertExpr, why string) { +func (w *lintuncheckedTypeAssertion) addFailure(n *ast.TypeAssertExpr, why string) { s := fmt.Sprintf("type cast result is unchecked in %v - %s", gofmt(n), why) w.onFailure(lint.Failure{ Category: "bad practice", diff --git a/rule/var_naming.go b/rule/var_naming.go index a8e15756c..5d3bf1ad7 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -55,14 +55,14 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) error { thirdArgument := arguments[2] asSlice, ok := thirdArgument.([]any) if !ok { - return fmt.Errorf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, got %T", "options", arguments[2]) + return fmt.Errorf("invalid third argument to the var-naming rule. Expecting a %s of type slice, got %T", "options", arguments[2]) } if len(asSlice) != 1 { - return fmt.Errorf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, but %d", "options", len(asSlice)) + return fmt.Errorf("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]any) if !ok { - return fmt.Errorf("Invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, with map, but %T", "options", asSlice[0]) + return fmt.Errorf("invalid third argument to the var-naming rule. Expecting a %s of type slice, of len==1, with map, but %T", "options", asSlice[0]) } r.allowUpperCaseConst = fmt.Sprint(args["upperCaseConst"]) == "true" r.skipPackageNameChecks = fmt.Sprint(args["skipPackageNameChecks"]) == "true" @@ -286,7 +286,7 @@ func getList(arg any, argName string) ([]string, error) { for _, v := range args { val, ok := v.(string) if !ok { - return nil, fmt.Errorf("Invalid %s values of the var-naming rule. Expecting slice of strings but got element of type %T", val, arg) + return nil, fmt.Errorf("invalid %s values of the var-naming rule. Expecting slice of strings but got element of type %T", val, arg) } list = append(list, val) } From 0e4a8c4ee9cb06276927646c9a0fac9a8f4b597f Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Sun, 8 Dec 2024 23:55:58 +0100 Subject: [PATCH 28/36] fix rule name --- rule/unchecked_type_assertion.go | 36 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index 2f25a9a95..a4962201e 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -14,14 +14,14 @@ const ( ruleUTAMessageIgnored = "type assertion result ignored" ) -// uncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. -type uncheckedTypeAssertionRule struct { +// UncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. +type UncheckedTypeAssertionRule struct { acceptIgnoredAssertionResult bool configureOnce sync.Once } -func (r *uncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { +func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { if len(arguments) == 0 { return nil } @@ -46,7 +46,7 @@ func (r *uncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { } // Apply applies the rule to given file. -func (r *uncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) @@ -56,7 +56,7 @@ func (r *uncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Argum var failures []lint.Failure - walker := &lintuncheckedTypeAssertion{ + walker := &lintUncheckedTypeAssertion{ onFailure: func(failure lint.Failure) { failures = append(failures, failure) }, @@ -69,11 +69,11 @@ func (r *uncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Argum } // Name returns the rule name. -func (*uncheckedTypeAssertionRule) Name() string { +func (*UncheckedTypeAssertionRule) Name() string { return "unchecked-type-assertion" } -type lintuncheckedTypeAssertion struct { +type lintUncheckedTypeAssertion struct { onFailure func(lint.Failure) acceptIgnoredTypeAssertionResult bool } @@ -91,14 +91,14 @@ func isTypeSwitch(e *ast.TypeAssertExpr) bool { return e.Type == nil } -func (w *lintuncheckedTypeAssertion) requireNoTypeAssert(expr ast.Expr) { +func (w *lintUncheckedTypeAssertion) requireNoTypeAssert(expr ast.Expr) { e, ok := expr.(*ast.TypeAssertExpr) if ok && !isTypeSwitch(e) { w.addFailure(e, ruleUTAMessagePanic) } } -func (w *lintuncheckedTypeAssertion) handleIfStmt(n *ast.IfStmt) { +func (w *lintUncheckedTypeAssertion) handleIfStmt(n *ast.IfStmt) { ifCondition, ok := n.Cond.(*ast.BinaryExpr) if ok { w.requireNoTypeAssert(ifCondition.X) @@ -106,7 +106,7 @@ func (w *lintuncheckedTypeAssertion) handleIfStmt(n *ast.IfStmt) { } } -func (w *lintuncheckedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion(expr ast.Expr) { +func (w *lintUncheckedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion(expr ast.Expr) { binaryExpr, ok := expr.(*ast.BinaryExpr) if ok { w.requireNoTypeAssert(binaryExpr.X) @@ -114,19 +114,19 @@ func (w *lintuncheckedTypeAssertion) requireBinaryExpressionWithoutTypeAssertion } } -func (w *lintuncheckedTypeAssertion) handleCaseClause(n *ast.CaseClause) { +func (w *lintUncheckedTypeAssertion) handleCaseClause(n *ast.CaseClause) { for _, expr := range n.List { w.requireNoTypeAssert(expr) w.requireBinaryExpressionWithoutTypeAssertion(expr) } } -func (w *lintuncheckedTypeAssertion) handleSwitch(n *ast.SwitchStmt) { +func (w *lintUncheckedTypeAssertion) handleSwitch(n *ast.SwitchStmt) { w.requireNoTypeAssert(n.Tag) w.requireBinaryExpressionWithoutTypeAssertion(n.Tag) } -func (w *lintuncheckedTypeAssertion) handleAssignment(n *ast.AssignStmt) { +func (w *lintUncheckedTypeAssertion) handleAssignment(n *ast.AssignStmt) { if len(n.Rhs) == 0 { return } @@ -150,21 +150,21 @@ func (w *lintuncheckedTypeAssertion) handleAssignment(n *ast.AssignStmt) { } // handles "return foo(.*bar)" - one of them is enough to fail as golang does not forward the type cast tuples in return statements -func (w *lintuncheckedTypeAssertion) handleReturn(n *ast.ReturnStmt) { +func (w *lintUncheckedTypeAssertion) handleReturn(n *ast.ReturnStmt) { for _, r := range n.Results { w.requireNoTypeAssert(r) } } -func (w *lintuncheckedTypeAssertion) handleRange(n *ast.RangeStmt) { +func (w *lintUncheckedTypeAssertion) handleRange(n *ast.RangeStmt) { w.requireNoTypeAssert(n.X) } -func (w *lintuncheckedTypeAssertion) handleChannelSend(n *ast.SendStmt) { +func (w *lintUncheckedTypeAssertion) handleChannelSend(n *ast.SendStmt) { w.requireNoTypeAssert(n.Value) } -func (w *lintuncheckedTypeAssertion) Visit(node ast.Node) ast.Visitor { +func (w *lintUncheckedTypeAssertion) Visit(node ast.Node) ast.Visitor { switch n := node.(type) { case *ast.RangeStmt: w.handleRange(n) @@ -185,7 +185,7 @@ func (w *lintuncheckedTypeAssertion) Visit(node ast.Node) ast.Visitor { return w } -func (w *lintuncheckedTypeAssertion) addFailure(n *ast.TypeAssertExpr, why string) { +func (w *lintUncheckedTypeAssertion) addFailure(n *ast.TypeAssertExpr, why string) { s := fmt.Sprintf("type cast result is unchecked in %v - %s", gofmt(n), why) w.onFailure(lint.Failure{ Category: "bad practice", From ef674f626c8fb8353fc717dcd7161f471256bdb2 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Mon, 9 Dec 2024 02:03:08 +0100 Subject: [PATCH 29/36] helper: newInternalFailureError --- rule/add_constant.go | 3 ++- rule/argument_limit.go | 2 +- rule/banned_characters.go | 2 +- rule/cognitive_complexity.go | 2 +- rule/comment_spacings.go | 2 +- rule/comments_density.go | 2 +- rule/context_as_argument.go | 2 +- rule/cyclomatic.go | 2 +- rule/defer.go | 2 +- rule/dot_imports.go | 2 +- rule/enforce_map_style.go | 2 +- rule/enforce_repeated_arg_type_style.go | 2 +- rule/enforce_slice_style.go | 2 +- rule/error_strings.go | 2 +- rule/exported.go | 2 +- rule/file_header.go | 2 +- rule/file_length_limit.go | 2 +- rule/filename_format.go | 2 +- rule/function_length.go | 2 +- rule/function_result_limit.go | 2 +- rule/import_alias_naming.go | 2 +- rule/imports_blocklist.go | 2 +- rule/line_length_limit.go | 2 +- rule/max_control_nesting.go | 2 +- rule/max_public_structs.go | 2 +- rule/receiver_naming.go | 2 +- rule/struct_tag.go | 2 +- rule/unchecked_type_assertion.go | 2 +- rule/unhandled_error.go | 2 +- rule/unused_param.go | 2 +- rule/unused_receiver.go | 2 +- rule/utils.go | 5 +++++ rule/var_naming.go | 2 +- 33 files changed, 38 insertions(+), 32 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index 3f17b9f5b..58e2885ef 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -46,7 +46,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin var configureErr error r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure @@ -69,6 +69,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin return failures } + // Name returns the rule name. func (*AddConstantRule) Name() string { return "add-constant" diff --git a/rule/argument_limit.go b/rule/argument_limit.go index c8822ab20..045de3284 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -38,7 +38,7 @@ func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) [] r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 571b87fb8..5f90a24f2 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -40,7 +40,7 @@ func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lin r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index efbb70f4a..524d45e27 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -40,7 +40,7 @@ func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Argument r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index 132cc104d..540c2c5e0 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -34,7 +34,7 @@ func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) [ r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/comments_density.go b/rule/comments_density.go index fd36df92b..e60cff547 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -38,7 +38,7 @@ func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) [ r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } commentsLines := countDocLines(file.AST.Comments) diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index 02de700d3..3c4b656fd 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -22,7 +22,7 @@ func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 84624193a..ba20bd0d2 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -40,7 +40,7 @@ func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/defer.go b/rule/defer.go index 51cf9d9eb..a784fa45a 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -30,7 +30,7 @@ func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Fail r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/dot_imports.go b/rule/dot_imports.go index c39d0253b..a0fc2a2ab 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -21,7 +21,7 @@ func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index 64a994595..e85efdf16 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -70,7 +70,7 @@ func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) [ r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } if r.enforceMapStyle == enforceMapStyleTypeAny { diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index 25f9cdf2d..c5dcba8d7 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -106,7 +106,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint. r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 436b0e6d4..bb530ac19 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -73,7 +73,7 @@ func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } if r.enforceSliceStyle == enforceSliceStyleTypeAny { diff --git a/rule/error_strings.go b/rule/error_strings.go index dc4e83ce0..514365dec 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -58,7 +58,7 @@ func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) []li r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/exported.go b/rule/exported.go index 87bdd4e20..3cd8452b7 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -112,7 +112,7 @@ func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) []lint.F r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/file_header.go b/rule/file_header.go index 9bf790a6d..2b6284366 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -39,7 +39,7 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } if r.header == "" { diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index 7beb98bb7..c3b6096da 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -30,7 +30,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } if r.max <= 0 { diff --git a/rule/filename_format.go b/rule/filename_format.go index 20d207c85..f6a3da144 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -23,7 +23,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) [] r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } filename := filepath.Base(file.Name) diff --git a/rule/function_length.go b/rule/function_length.go index 4e30d071d..693625fdb 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -33,7 +33,7 @@ func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) []lint r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index c2d7cfcc9..17c4eae76 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -22,7 +22,7 @@ func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Argumen r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 43bf9d545..202a95a1f 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -66,7 +66,7 @@ func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 3e147f1aa..032681df1 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -48,7 +48,7 @@ func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index c55b8706e..74002d103 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -43,7 +43,7 @@ func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index a3755dcb5..2f8838428 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -24,7 +24,7 @@ func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 40c140399..daf1f8843 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -44,7 +44,7 @@ func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 54eee4eff..3e0cc561c 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -50,7 +50,7 @@ func (r *ReceiverNamingRule) Apply(file *lint.File, arguments lint.Arguments) [] r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } typeReceiver := map[string]string{} diff --git a/rule/struct_tag.go b/rule/struct_tag.go index a7d0939c3..be1222df5 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -52,7 +52,7 @@ func (r *StructTagRule) Apply(file *lint.File, arguments lint.Arguments) []lint. r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index a4962201e..987769ef7 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -51,7 +51,7 @@ func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, arguments lint.Argum r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 9ab865e64..3a0753c37 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -47,7 +47,7 @@ func (r *UnhandledErrorRule) Apply(file *lint.File, arguments lint.Arguments) [] r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/unused_param.go b/rule/unused_param.go index 365802b15..374e899e2 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -55,7 +55,7 @@ func (r *UnusedParamRule) Apply(file *lint.File, arguments lint.Arguments) []lin r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index b8fba059e..29860684d 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -53,7 +53,7 @@ func (r *UnusedReceiverRule) Apply(file *lint.File, arguments lint.Arguments) [] r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure diff --git a/rule/utils.go b/rule/utils.go index 55390e2fb..43a2b1b32 100644 --- a/rule/utils.go +++ b/rule/utils.go @@ -122,3 +122,8 @@ func isDirectiveComment(line string) bool { func isCallToExitFunction(pkgName, functionName string) bool { return exitFunctions[pkgName] != nil && exitFunctions[pkgName][functionName] } +// newInternalFailureError returns single internal failure +func newInternalFailureError(e error) []lint.Failure { + return []lint.Failure{lint.NewInternalFailure(e.Error())} +} + diff --git a/rule/var_naming.go b/rule/var_naming.go index 5d3bf1ad7..002f72279 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -96,7 +96,7 @@ func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint. r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } var failures []lint.Failure From 5ebe4593d05b4cde7751d34134d5e3bece7e257a Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Mon, 9 Dec 2024 10:10:47 +0100 Subject: [PATCH 30/36] add space in msg --- lint/linter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint/linter.go b/lint/linter.go index a07b36b94..441f230ce 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -106,7 +106,7 @@ func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-cha wg.Add(1) go func(pkg []string, gover *goversion.Version) { if err := l.lintPackage(pkg, gover, ruleSet, config, failures); err != nil { - fmt.Fprintln(os.Stderr, "error during linting:"+err.Error()) + fmt.Fprintln(os.Stderr, "error during linting: "+err.Error()) os.Exit(1) } wg.Done() From 953b2a5251a6de550b9ae0405090417946a96ba7 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Mon, 9 Dec 2024 10:15:03 +0100 Subject: [PATCH 31/36] newInternalFailureError: update helper comment --- rule/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rule/utils.go b/rule/utils.go index 43a2b1b32..e17821dc5 100644 --- a/rule/utils.go +++ b/rule/utils.go @@ -122,8 +122,8 @@ func isDirectiveComment(line string) bool { func isCallToExitFunction(pkgName, functionName string) bool { return exitFunctions[pkgName] != nil && exitFunctions[pkgName][functionName] } -// newInternalFailureError returns single internal failure + +// newInternalFailureError returns an slice of Failure with a single internal failure in it func newInternalFailureError(e error) []lint.Failure { return []lint.Failure{lint.NewInternalFailure(e.Error())} } - From ed5e1491729371886eba61fa043839b894774400 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Mon, 9 Dec 2024 15:49:32 +0100 Subject: [PATCH 32/36] .gitignore: remove idea entry --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 796788388..71f016325 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ vendor *.swp dist/ *.log -.idea/ From 1628b5ca9451ffb544edcad84fb1c00c29c02f26 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Tue, 10 Dec 2024 10:19:38 +0100 Subject: [PATCH 33/36] cleanup after review --- rule/add_constant.go | 1 - rule/enforce_slice_style.go | 2 +- rule/file_header.go | 4 ++-- rule/file_length_limit.go | 2 +- rule/import_alias_naming.go | 4 ++-- rule/max_public_structs.go | 6 +++--- rule/string_format.go | 2 +- rule/struct_tag.go | 6 +++--- 8 files changed, 13 insertions(+), 14 deletions(-) diff --git a/rule/add_constant.go b/rule/add_constant.go index 58e2885ef..9df1db701 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -69,7 +69,6 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin return failures } - // Name returns the rule name. func (*AddConstantRule) Name() string { return "add-constant" diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index bb530ac19..b09c25217 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -62,7 +62,7 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { var err error r.enforceSliceStyle, err = sliceStyleFromString(enforceSliceStyle) if err != nil { - return fmt.Errorf("invalid argument to the enforce-slice-style rule: %v", err) + return fmt.Errorf("invalid argument to the enforce-slice-style rule: %w", err) } return nil } diff --git a/rule/file_header.go b/rule/file_header.go index 2b6284366..bfb3eddda 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -39,7 +39,7 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return newInternalFailureError(configureErr) + return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} } if r.header == "" { @@ -75,7 +75,7 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint regex, err := regexp.Compile(r.header) if err != nil { - return []lint.Failure{lint.NewInternalFailure(err.Error())} + return newInternalFailureError(err) } if !regex.MatchString(comment) { diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index c3b6096da..e451e926b 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -49,7 +49,7 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ } if err := scanner.Err(); err != nil { - return []lint.Failure{lint.NewInternalFailure(err.Error())} + return newInternalFailureError(err) } lines := all diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 202a95a1f..b00f829b3 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -117,7 +117,7 @@ func (r *ImportAliasNamingRule) setAllowRule(value any) error { namingRuleRegexp, err := regexp.Compile(namingRule) if err != nil { - return fmt.Errorf("invalid argument to the import-alias-naming allowRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) + return fmt.Errorf("invalid argument to the import-alias-naming allowRegexp rule. Expecting %q to be a valid regular expression, got: %w", namingRule, err) } r.allowRegexp = namingRuleRegexp return nil @@ -131,7 +131,7 @@ func (r *ImportAliasNamingRule) setDenyRule(value any) error { namingRuleRegexp, err := regexp.Compile(namingRule) if err != nil { - return fmt.Errorf("invalid argument to the import-alias-naming denyRegexp rule. Expecting %q to be a valid regular expression, got: %v", namingRule, err) + return fmt.Errorf("invalid argument to the import-alias-naming denyRegexp rule. Expecting %q to be a valid regular expression, got: %w", namingRule, err) } r.denyRegexp = namingRuleRegexp return nil diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index daf1f8843..00dcf8ce3 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -25,9 +25,9 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { return nil } - check := checkNumberOfArguments(1, arguments, r.Name()) - if check != nil { - return check + err := checkNumberOfArguments(1, arguments, r.Name()) + if err != nil { + return err } maxStructs, ok := arguments[0].(int64) // Alt. non panicking version diff --git a/rule/string_format.go b/rule/string_format.go index 92a5b8cef..2a608b500 100644 --- a/rule/string_format.go +++ b/rule/string_format.go @@ -27,7 +27,7 @@ func (*StringFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint w := lintStringFormatRule{onFailure: onFailure} err := w.parseArguments(arguments) if err != nil { - return []lint.Failure{lint.NewInternalFailure(err.Error())} + return newInternalFailureError(err) } ast.Walk(w, file.AST) diff --git a/rule/struct_tag.go b/rule/struct_tag.go index be1222df5..b0fc32704 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -23,9 +23,9 @@ func (r *StructTagRule) configure(arguments lint.Arguments) error { return nil } - check := checkNumberOfArguments(1, arguments, r.Name()) - if check != nil { - return check + err := checkNumberOfArguments(1, arguments, r.Name()) + if err != nil { + return err } r.userDefined = make(map[string][]string, len(arguments)) for _, arg := range arguments { From bad6ed8d00e6f504f243b25d33a85e4ea40764b7 Mon Sep 17 00:00:00 2001 From: Marcin Federowicz <marcin.federowicz@gmail.com> Date: Tue, 10 Dec 2024 11:54:51 +0100 Subject: [PATCH 34/36] cleanup after review --- rule/file_header.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rule/file_header.go b/rule/file_header.go index bfb3eddda..b70655732 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -39,7 +39,7 @@ func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint r.configureOnce.Do(func() { configureErr = r.configure(arguments) }) if configureErr != nil { - return []lint.Failure{lint.NewInternalFailure(configureErr.Error())} + return newInternalFailureError(configureErr) } if r.header == "" { From 634fa48a3d85bc65c46a362a9fea6d9a1fc6f483 Mon Sep 17 00:00:00 2001 From: chavacava <salvadorcavadini+github@gmail.com> Date: Wed, 11 Dec 2024 19:19:44 +0100 Subject: [PATCH 35/36] Apply suggestions from code review Co-authored-by: Oleksandr Redko <oleksandr.red+github@gmail.com> --- rule/filename_format.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rule/filename_format.go b/rule/filename_format.go index f6a3da144..4c4805f49 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -79,7 +79,7 @@ func (r *FilenameFormatRule) configure(arguments lint.Arguments) error { format, err := regexp.Compile(str) if err != nil { - return fmt.Errorf("rule %q expects a valid regexp argument, got %w for %s", r.Name(), err, arg) + return fmt.Errorf("rule %q expects a valid regexp argument, got error for %s: %w", r.Name(), err) } r.format = format From e56fa62539699c62c808448a4400674ca1015345 Mon Sep 17 00:00:00 2001 From: chavacava <salvador.cavadini@gmail.com> Date: Wed, 11 Dec 2024 19:33:01 +0100 Subject: [PATCH 36/36] fix printf parameters --- rule/filename_format.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rule/filename_format.go b/rule/filename_format.go index 4c4805f49..4a5af8058 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -79,7 +79,7 @@ func (r *FilenameFormatRule) configure(arguments lint.Arguments) error { format, err := regexp.Compile(str) if err != nil { - return fmt.Errorf("rule %q expects a valid regexp argument, got error for %s: %w", r.Name(), err) + return fmt.Errorf("rule %q expects a valid regexp argument, got error for %s: %w", r.Name(), str, err) } r.format = format