From e04c9cd4a8c32668e35a70cac4f6101f76663d68 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:30:37 +0100 Subject: [PATCH] refactor: remove deep-exit in lint/linter.go --- config/config.go | 6 +++++ lint/linter.go | 21 ++++++++------- lint/rule.go | 5 ++++ rule/add_constant.go | 16 ++++-------- rule/argument_limit.go | 16 ++++-------- rule/banned_characters.go | 16 ++++-------- rule/cognitive_complexity.go | 16 ++++-------- rule/comment_spacings.go | 16 ++++-------- rule/comments_density.go | 16 ++++-------- rule/context_as_argument.go | 16 ++++-------- rule/cyclomatic.go | 16 ++++-------- rule/defer.go | 16 ++++-------- rule/dot_imports.go | 16 ++++-------- rule/enforce_map_style.go | 16 ++++-------- rule/enforce_repeated_arg_type_style.go | 16 ++++-------- rule/enforce_slice_style.go | 16 ++++-------- rule/error_strings.go | 16 ++++-------- rule/exported.go | 24 ++++++++--------- rule/file_header.go | 16 ++++-------- rule/file_length_limit.go | 16 ++++-------- rule/filename_format.go | 16 ++++-------- rule/function_length.go | 22 +++++++--------- rule/function_result_limit.go | 16 ++++-------- rule/import_alias_naming.go | 16 ++++-------- rule/imports_blocklist.go | 16 ++++-------- rule/line_length_limit.go | 16 ++++-------- rule/max_control_nesting.go | 16 ++++-------- rule/max_public_structs.go | 16 ++++-------- rule/receiver_naming.go | 17 ++++--------- rule/struct_tag.go | 34 +++++++++++-------------- rule/unchecked_type_assertion.go | 16 ++++-------- rule/unhandled_error.go | 16 ++++-------- rule/unused_param.go | 16 ++++-------- rule/unused_receiver.go | 16 ++++-------- rule/var_naming.go | 16 ++++-------- 35 files changed, 202 insertions(+), 375 deletions(-) diff --git a/config/config.go b/config/config.go index b88627411..162e39479 100644 --- a/config/config.go +++ b/config/config.go @@ -149,6 +149,12 @@ func GetLintingRules(config *lint.Config, extraRules []lint.Rule) ([]lint.Rule, continue // skip disabled rules } + if r, ok := r.(lint.ConfigurableRule); ok { + if err := r.Configure(ruleConfig.Arguments); err != nil { + return nil, fmt.Errorf("cannot configure rule: %s", name) + } + } + lintingRules = append(lintingRules, r) } diff --git a/lint/linter.go b/lint/linter.go index 4f9440181..6a13b7a8a 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -10,10 +10,10 @@ import ( "regexp" "strconv" "strings" - "sync" goversion "github.com/hashicorp/go-version" "golang.org/x/mod/modfile" + "golang.org/x/sync/errgroup" ) // ReadFile defines an abstraction for reading files. @@ -101,20 +101,23 @@ func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-cha perPkgVersions[n] = v } - var wg sync.WaitGroup - wg.Add(len(packages)) + var wg errgroup.Group for n := range packages { - go func(pkg []string, gover *goversion.Version) { + wg.Go(func() error { + pkg := packages[n] + gover := perPkgVersions[n] if err := l.lintPackage(pkg, gover, ruleSet, config, failures); err != nil { - fmt.Fprintln(os.Stderr, "error during linting: "+err.Error()) - os.Exit(1) + return fmt.Errorf("error during linting: %w", err) } - wg.Done() - }(packages[n], perPkgVersions[n]) + return nil + }) } go func() { - wg.Wait() + err := wg.Wait() + if err != nil { + failures <- NewInternalFailure(err.Error()) + } close(failures) }() diff --git a/lint/rule.go b/lint/rule.go index 6189b9f7d..cc424e96a 100644 --- a/lint/rule.go +++ b/lint/rule.go @@ -17,6 +17,11 @@ type Rule interface { Apply(*File, Arguments) []Failure } +// ConfigurableRule defines an abstract configurable rule interface. +type ConfigurableRule interface { + Configure(Arguments) error +} + // ToFailurePosition returns the failure position. func ToFailurePosition(start, end token.Pos, file *File) FailurePosition { return FailurePosition{ diff --git a/rule/add_constant.go b/rule/add_constant.go index 7ea43566e..6466707ac 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -7,7 +7,6 @@ import ( "regexp" "strconv" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -37,18 +36,10 @@ type AddConstantRule struct { allowList allowList ignoreFunctions []*regexp.Regexp strLitLimit int - - configureOnce sync.Once - configureErr error } // Apply applies the rule to given file. -func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *AddConstantRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { @@ -206,7 +197,10 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool { return ok } -func (r *AddConstantRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *AddConstantRule) Configure(arguments lint.Arguments) error { r.strLitLimit = defaultStrLitLimit r.allowList = newAllowList() if len(arguments) == 0 { diff --git a/rule/argument_limit.go b/rule/argument_limit.go index 42cfccb92..7fd6a382d 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,14 +11,14 @@ import ( // ArgumentsLimitRule lints the number of arguments a function can receive. type ArgumentsLimitRule struct { max int - - configureOnce sync.Once - configureErr error } const defaultArgumentsLimit = 8 -func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *ArgumentsLimitRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.max = defaultArgumentsLimit return nil @@ -34,12 +33,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *ArgumentsLimitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 6d7b211be..7eb026b03 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,14 +11,14 @@ import ( // BannedCharsRule checks if a file contains banned characters. type BannedCharsRule struct { bannedCharList []string - - configureOnce sync.Once - configureErr error } const bannedCharsRuleName = "banned-characters" -func (r *BannedCharsRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *BannedCharsRule) Configure(arguments lint.Arguments) error { if len(arguments) > 0 { err := checkNumberOfArguments(1, arguments, bannedCharsRuleName) if err != nil { @@ -36,12 +35,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *BannedCharsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index fb4e7a852..3844ebf4c 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "go/token" - "sync" "github.com/mgechev/revive/lint" "golang.org/x/tools/go/ast/astutil" @@ -13,14 +12,14 @@ import ( // CognitiveComplexityRule sets restriction for maximum cognitive complexity. type CognitiveComplexityRule struct { maxComplexity int - - configureOnce sync.Once - configureErr error } const defaultMaxCognitiveComplexity = 7 -func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *CognitiveComplexityRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.maxComplexity = defaultMaxCognitiveComplexity return nil @@ -36,12 +35,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *CognitiveComplexityRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure linter := cognitiveComplexityLinter{ diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index fade0369b..929248d76 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,12 +11,12 @@ import ( // the comment symbol( // ) and the start of the comment text type CommentSpacingsRule struct { allowList []string - - configureOnce sync.Once - configureErr error } -func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *CommentSpacingsRule) Configure(arguments lint.Arguments) error { r.allowList = []string{} for _, arg := range arguments { allow, ok := arg.(string) // Alt. non panicking version @@ -30,12 +29,7 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) error { } // Apply the rule. -func (r *CommentSpacingsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *CommentSpacingsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, cg := range file.AST.Comments { diff --git a/rule/comments_density.go b/rule/comments_density.go index 79be4c7f9..e83c20add 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,14 +11,14 @@ import ( // CommentsDensityRule enforces a minimum comment / code relation. type CommentsDensityRule struct { minimumCommentsDensity int64 - - configureOnce sync.Once - configureErr error } const defaultMinimumCommentsPercentage = 0 -func (r *CommentsDensityRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *CommentsDensityRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.minimumCommentsDensity = defaultMinimumCommentsPercentage return nil @@ -34,12 +33,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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *CommentsDensityRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { 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 a845532db..458ba4489 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,18 +11,10 @@ import ( // ContextAsArgumentRule suggests that `context.Context` should be the first argument of a function. type ContextAsArgumentRule struct { allowTypes map[string]struct{} - - configureOnce sync.Once - configureErr error } // Apply applies the rule to given file. -func (r *ContextAsArgumentRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *ContextAsArgumentRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { fn, ok := decl.(*ast.FuncDecl) @@ -63,7 +54,10 @@ func (*ContextAsArgumentRule) Name() string { return "context-as-argument" } -func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *ContextAsArgumentRule) Configure(arguments lint.Arguments) error { types, err := r.getAllowTypesFromArguments(arguments) if err != nil { return err diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index f16962a5b..aff041415 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "go/token" - "sync" "github.com/mgechev/revive/lint" ) @@ -14,14 +13,14 @@ import ( // CyclomaticRule sets restriction for maximum cyclomatic complexity. type CyclomaticRule struct { maxComplexity int - - configureOnce sync.Once - configureErr error } const defaultMaxCyclomaticComplexity = 10 -func (r *CyclomaticRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *CyclomaticRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.maxComplexity = defaultMaxCyclomaticComplexity return nil @@ -36,12 +35,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *CyclomaticRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { fn, ok := decl.(*ast.FuncDecl) diff --git a/rule/defer.go b/rule/defer.go index 4b11776ad..21da33e8d 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -11,12 +10,12 @@ import ( // DeferRule lints gotchas in defer statements. type DeferRule struct { allow map[string]bool - - configureOnce sync.Once - configureErr error } -func (r *DeferRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *DeferRule) Configure(arguments lint.Arguments) error { list, err := r.allowFromArgs(arguments) if err != nil { return err @@ -26,12 +25,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *DeferRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { 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 db2e88279..d076a85c6 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -11,18 +10,10 @@ import ( // DotImportsRule forbids . imports. type DotImportsRule struct { allowedPackages allowPackages - - configureOnce sync.Once - configureErr error } // Apply applies the rule to given file. -func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *DotImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST @@ -45,7 +36,10 @@ func (*DotImportsRule) Name() string { return "dot-imports" } -func (r *DotImportsRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +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 5d3702f81..85d5f99f5 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -40,12 +39,12 @@ 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 - configureErr error } -func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *EnforceMapStyleRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.enforceMapStyle = enforceMapStyleTypeAny return nil @@ -66,12 +65,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *EnforceMapStyleRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { if r.enforceMapStyle == enforceMapStyleTypeAny { // this linter is not configured return nil diff --git a/rule/enforce_repeated_arg_type_style.go b/rule/enforce_repeated_arg_type_style.go index 36d6b77cf..c9db4683f 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -43,12 +42,12 @@ func repeatedArgTypeStyleFromString(s string) (enforceRepeatedArgTypeStyleType, type EnforceRepeatedArgTypeStyleRule struct { funcArgStyle enforceRepeatedArgTypeStyleType funcRetValStyle enforceRepeatedArgTypeStyleType - - configureOnce sync.Once - configureErr error } -func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *EnforceRepeatedArgTypeStyleRule) Configure(arguments lint.Arguments) error { r.funcArgStyle = enforceRepeatedArgTypeStyleTypeAny r.funcRetValStyle = enforceRepeatedArgTypeStyleTypeAny @@ -102,12 +101,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. return nil diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index ec72af212..71537549b 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -44,12 +43,12 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { // EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`. type EnforceSliceStyleRule struct { enforceSliceStyle enforceSliceStyleType - - configureOnce sync.Once - configureErr error } -func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *EnforceSliceStyleRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.enforceSliceStyle = enforceSliceStyleTypeAny return nil @@ -69,12 +68,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *EnforceSliceStyleRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { if r.enforceSliceStyle == enforceSliceStyleTypeAny { // this linter is not configured return nil diff --git a/rule/error_strings.go b/rule/error_strings.go index 914565fcc..ea7f8a49c 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -6,7 +6,6 @@ import ( "go/token" "strconv" "strings" - "sync" "unicode" "unicode/utf8" @@ -16,12 +15,12 @@ import ( // ErrorStringsRule lints error strings. type ErrorStringsRule struct { errorFunctions map[string]map[string]struct{} - - configureOnce sync.Once - configureErr error } -func (r *ErrorStringsRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *ErrorStringsRule) Configure(arguments lint.Arguments) error { r.errorFunctions = map[string]map[string]struct{}{ "fmt": { "Errorf": {}, @@ -54,12 +53,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *ErrorStringsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST diff --git a/rule/exported.go b/rule/exported.go index 6254ed53e..db47e5a0c 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -5,7 +5,6 @@ import ( "go/ast" "go/token" "strings" - "sync" "unicode" "unicode/utf8" @@ -25,9 +24,11 @@ type disabledChecks struct { Var bool } -const checkNamePrivateReceivers = "privateReceivers" -const checkNamePublicInterfaces = "publicInterfaces" -const checkNameStuttering = "stuttering" +const ( + checkNamePrivateReceivers = "privateReceivers" + checkNamePublicInterfaces = "publicInterfaces" + checkNameStuttering = "stuttering" +) // isDisabled returns true if the given check is disabled, false otherwise func (dc *disabledChecks) isDisabled(checkName string) bool { @@ -66,12 +67,12 @@ var commonMethods = map[string]bool{ type ExportedRule struct { stuttersMsg string disabledChecks disabledChecks - - configureOnce sync.Once - configureErr error } -func (r *ExportedRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *ExportedRule) Configure(arguments lint.Arguments) error { r.disabledChecks = disabledChecks{PrivateReceivers: true, PublicInterfaces: true} r.stuttersMsg = "stutters" for _, flag := range arguments { @@ -108,12 +109,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *ExportedRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure if file.IsTest() { return failures diff --git a/rule/file_header.go b/rule/file_header.go index ced23a2a1..53d7ea9d0 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "regexp" - "sync" "github.com/mgechev/revive/lint" ) @@ -11,9 +10,6 @@ import ( // FileHeaderRule lints the header that each file should have. type FileHeaderRule struct { header string - - configureOnce sync.Once - configureErr error } var ( @@ -21,7 +17,10 @@ var ( singleRegexp = regexp.MustCompile("^//") ) -func (r *FileHeaderRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *FileHeaderRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { return nil } @@ -35,12 +34,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *FileHeaderRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { if r.header == "" { return nil } diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index 3974d6ca8..e2b79c42c 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -7,7 +7,6 @@ import ( "go/ast" "go/token" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -20,18 +19,10 @@ type FileLengthLimitRule struct { skipComments bool // skipBlankLines indicates whether to skip blank lines when counting lines. skipBlankLines bool - - configureOnce sync.Once - configureErr error } // Apply applies the rule to given file. -func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *FileLengthLimitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { if r.max <= 0 { // when max is negative or 0 the rule is disabled return nil @@ -79,7 +70,10 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ } } -func (r *FileLengthLimitRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *FileLengthLimitRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { return nil // use default } diff --git a/rule/filename_format.go b/rule/filename_format.go index 0861dadeb..6d4905f18 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -4,7 +4,6 @@ import ( "fmt" "path/filepath" "regexp" - "sync" "unicode" "github.com/mgechev/revive/lint" @@ -13,18 +12,10 @@ import ( // FilenameFormatRule lints source filenames according to a set of regular expressions given as arguments type FilenameFormatRule struct { format *regexp.Regexp - - configureOnce sync.Once - configureErr error } // Apply applies the rule to the given file. -func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *FilenameFormatRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { filename := filepath.Base(file.Name) if r.format.MatchString(filename) { return nil @@ -59,7 +50,10 @@ func (*FilenameFormatRule) Name() string { var defaultFormat = regexp.MustCompile(`^[_A-Za-z0-9][_A-Za-z0-9-]*\.go$`) -func (r *FilenameFormatRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *FilenameFormatRule) Configure(arguments lint.Arguments) error { argsCount := len(arguments) if argsCount == 0 { r.format = defaultFormat diff --git a/rule/function_length.go b/rule/function_length.go index e5e713912..53cb6827c 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "reflect" - "sync" "github.com/mgechev/revive/lint" ) @@ -13,12 +12,12 @@ import ( type FunctionLength struct { maxStmt int maxLines int - - configureOnce sync.Once - configureErr error } -func (r *FunctionLength) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *FunctionLength) Configure(arguments lint.Arguments) error { maxStmt, maxLines, err := r.parseArguments(arguments) if err != nil { return err @@ -29,12 +28,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *FunctionLength) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { funcDecl, ok := decl.(*ast.FuncDecl) @@ -79,8 +73,10 @@ func (*FunctionLength) Name() string { return "function-length" } -const defaultFuncStmtsLimit = 50 -const defaultFuncLinesLimit = 75 +const ( + defaultFuncStmtsLimit = 50 + defaultFuncLinesLimit = 75 +) func (*FunctionLength) parseArguments(arguments lint.Arguments) (maxStmt, maxLines int64, err error) { if len(arguments) == 0 { diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index e43ed9aba..b5508f368 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,18 +11,10 @@ import ( // FunctionResultsLimitRule limits the maximum number of results a function can return. type FunctionResultsLimitRule struct { max int - - configureOnce sync.Once - configureErr error } // Apply applies the rule to given file. -func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *FunctionResultsLimitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { funcDecl, ok := decl.(*ast.FuncDecl) @@ -58,7 +49,10 @@ func (*FunctionResultsLimitRule) Name() string { const defaultResultsLimit = 3 -func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *FunctionResultsLimitRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.max = defaultResultsLimit return nil diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index a426d30ed..8c389fe5b 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "regexp" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,16 +11,16 @@ import ( type ImportAliasNamingRule struct { allowRegexp *regexp.Regexp denyRegexp *regexp.Regexp - - configureOnce sync.Once - configureErr error } const defaultImportAliasNamingAllowRule = "^[a-z][a-z0-9]{0,}$" var defaultImportAliasNamingAllowRegexp = regexp.MustCompile(defaultImportAliasNamingAllowRule) -func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *ImportAliasNamingRule) Configure(arguments lint.Arguments) error { if len(arguments) == 0 { r.allowRegexp = defaultImportAliasNamingAllowRegexp return nil @@ -62,12 +61,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *ImportAliasNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, is := range file.AST.Imports { diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 1b009a233..b510fe9ae 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "regexp" - "sync" "github.com/mgechev/revive/lint" ) @@ -11,14 +10,14 @@ import ( // ImportsBlocklistRule disallows importing the specified packages. type ImportsBlocklistRule struct { blocklist []*regexp.Regexp - - configureOnce sync.Once - configureErr error } var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`) -func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *ImportsBlocklistRule) Configure(arguments lint.Arguments) error { r.blocklist = []*regexp.Regexp{} for _, arg := range arguments { argStr, ok := arg.(string) @@ -44,12 +43,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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *ImportsBlocklistRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, is := range file.AST.Imports { diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index 865e14de3..ad2da89a9 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -7,7 +7,6 @@ import ( "fmt" "go/token" "strings" - "sync" "unicode/utf8" "github.com/mgechev/revive/lint" @@ -16,14 +15,14 @@ import ( // LineLengthLimitRule lints number of characters in a line. type LineLengthLimitRule struct { max int - - configureOnce sync.Once - configureErr error } const defaultLineLengthLimit = 80 -func (r *LineLengthLimitRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *LineLengthLimitRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.max = defaultLineLengthLimit return nil @@ -39,12 +38,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *LineLengthLimitRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure checker := lintLineLengthNum{ diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 92d7c3b71..78ba964bc 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -12,20 +11,12 @@ import ( // MaxControlNestingRule sets restriction for maximum nesting of control structures. type MaxControlNestingRule struct { max int64 - - configureOnce sync.Once - configureErr error } const defaultMaxControlNesting = 5 // Apply applies the rule to given file. -func (r *MaxControlNestingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *MaxControlNestingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST @@ -112,7 +103,10 @@ func (w *lintMaxControlNesting) walkControlledBlock(b ast.Node) { w.nestingLevelAcc = oldNestingLevel } -func (r *MaxControlNestingRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *MaxControlNestingRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.max = defaultMaxControlNesting return nil diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index fbe9de843..685401536 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -5,7 +5,6 @@ import ( "fmt" "go/ast" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -13,14 +12,14 @@ import ( // MaxPublicStructsRule lints the number of public structs in a file. type MaxPublicStructsRule struct { max int64 - - configureOnce sync.Once - configureErr error } const defaultMaxPublicStructs = 5 -func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *MaxPublicStructsRule) Configure(arguments lint.Arguments) error { if len(arguments) < 1 { r.max = defaultMaxPublicStructs return nil @@ -40,12 +39,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *MaxPublicStructsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure if r.max < 1 { diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index 7561b375a..6f5832e1e 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -3,7 +3,6 @@ package rule import ( "fmt" "go/ast" - "sync" "github.com/mgechev/revive/internal/typeparams" "github.com/mgechev/revive/lint" @@ -12,14 +11,13 @@ import ( // ReceiverNamingRule lints a receiver name. type ReceiverNamingRule struct { receiverNameMaxLength int - - configureOnce sync.Once - configureErr error } const defaultReceiverNameMaxLength = -1 // thus will not check - -func (r *ReceiverNamingRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *ReceiverNamingRule) Configure(arguments lint.Arguments) error { r.receiverNameMaxLength = defaultReceiverNameMaxLength if len(arguments) < 1 { return nil @@ -46,12 +44,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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *ReceiverNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { typeReceiver := map[string]string{} var failures []lint.Failure for _, decl := range file.AST.Decls { diff --git a/rule/struct_tag.go b/rule/struct_tag.go index 4e98122fa..5182612b2 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -5,7 +5,6 @@ import ( "go/ast" "strconv" "strings" - "sync" "github.com/fatih/structtag" "github.com/mgechev/revive/lint" @@ -14,12 +13,12 @@ import ( // StructTagRule lints struct tags. type StructTagRule struct { userDefined map[string][]string // map: key -> []option - - configureOnce sync.Once - configureErr error } -func (r *StructTagRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *StructTagRule) Configure(arguments lint.Arguments) error { if len(arguments) == 0 { return nil } @@ -48,12 +47,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *StructTagRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) @@ -101,14 +95,16 @@ func (w lintStructTagRule) Visit(node ast.Node) ast.Visitor { return w } -const keyASN1 = "asn1" -const keyBSON = "bson" -const keyDefault = "default" -const keyJSON = "json" -const keyProtobuf = "protobuf" -const keyRequired = "required" -const keyXML = "xml" -const keyYAML = "yaml" +const ( + keyASN1 = "asn1" + keyBSON = "bson" + keyDefault = "default" + keyJSON = "json" + keyProtobuf = "protobuf" + keyRequired = "required" + keyXML = "xml" + keyYAML = "yaml" +) func (w lintStructTagRule) checkTagNameIfNeed(tag *structtag.Tag) (string, bool) { isUnnamedTag := tag.Name == "" || tag.Name == "-" diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index 25aa6db42..964fbf05c 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "go/ast" - "sync" "github.com/mgechev/revive/lint" ) @@ -17,12 +16,12 @@ const ( // UncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. type UncheckedTypeAssertionRule struct { acceptIgnoredAssertionResult bool - - configureOnce sync.Once - configureErr error } -func (r *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *UncheckedTypeAssertionRule) Configure(arguments lint.Arguments) error { if len(arguments) == 0 { return nil } @@ -47,12 +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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *UncheckedTypeAssertionRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure walker := &lintUncheckedTypeAssertion{ diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 94a1a1070..131fa5884 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -7,7 +7,6 @@ import ( "go/types" "regexp" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -15,12 +14,12 @@ import ( // UnhandledErrorRule warns on unhandled errors returned by function calls. type UnhandledErrorRule struct { ignoreList []*regexp.Regexp - - configureOnce sync.Once - configureErr error } -func (r *UnhandledErrorRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *UnhandledErrorRule) Configure(arguments lint.Arguments) error { for _, arg := range arguments { argStr, ok := arg.(string) if !ok { @@ -43,12 +42,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *UnhandledErrorRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure walker := &lintUnhandledErrors{ diff --git a/rule/unused_param.go b/rule/unused_param.go index fd3b90c53..5da1a736d 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "regexp" - "sync" "github.com/mgechev/revive/lint" ) @@ -16,12 +15,12 @@ type UnusedParamRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default allowRegex *regexp.Regexp failureMsg string - - configureOnce sync.Once - configureErr error } -func (r *UnusedParamRule) configure(args lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *UnusedParamRule) Configure(args lint.Arguments) error { // while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives // it's more compatible to JSON nature of configurations r.allowRegex = allowBlankIdentifierRegex @@ -51,12 +50,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *UnusedParamRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 00253e263..86367acc7 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -4,7 +4,6 @@ import ( "fmt" "go/ast" "regexp" - "sync" "github.com/mgechev/revive/lint" ) @@ -14,12 +13,12 @@ type UnusedReceiverRule struct { // regex to check if some name is valid for unused parameter, "^_$" by default allowRegex *regexp.Regexp failureMsg string - - configureOnce sync.Once - configureErr error } -func (r *UnusedReceiverRule) configure(args lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *UnusedReceiverRule) Configure(args lint.Arguments) error { // while by default args is an array, i think it's good to provide structures inside it by default, not arrays or primitives // it's more compatible to JSON nature of configurations r.allowRegex = allowBlankIdentifierRegex @@ -49,12 +48,7 @@ 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 { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *UnusedReceiverRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure for _, decl := range file.AST.Decls { diff --git a/rule/var_naming.go b/rule/var_naming.go index 314c2d5c4..934d5f8f3 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -6,7 +6,6 @@ import ( "go/token" "regexp" "strings" - "sync" "github.com/mgechev/revive/lint" ) @@ -29,12 +28,12 @@ type VarNamingRule struct { blockList []string allowUpperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants skipPackageNameChecks bool - - configureOnce sync.Once - configureErr error } -func (r *VarNamingRule) configure(arguments lint.Arguments) error { +// Configure validates the rule configuration, and configures the rule accordingly. +// +// Configuration implements the [lint.ConfigurableRule] interface. +func (r *VarNamingRule) Configure(arguments lint.Arguments) error { if len(arguments) >= 1 { list, err := getList(arguments[0], "allowlist") if err != nil { @@ -92,12 +91,7 @@ func (*VarNamingRule) applyPackageCheckRules(walker *lintNames) { } // Apply applies the rule to given file. -func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configureOnce.Do(func() { r.configureErr = r.configure(arguments) }) - if r.configureErr != nil { - return newInternalFailureError(r.configureErr) - } - +func (r *VarNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure fileAst := file.AST