diff --git a/rule/add_constant.go b/rule/add_constant.go index 233f1d848..399382c8b 100644 --- a/rule/add_constant.go +++ b/rule/add_constant.go @@ -36,12 +36,13 @@ type AddConstantRule struct { allowList allowList ignoreFunctions []*regexp.Regexp strLitLimit int - sync.Mutex + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure @@ -201,9 +202,6 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool { } func (r *AddConstantRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.allowList == nil { r.strLitLimit = defaultStrLitLimit r.allowList = newAllowList() diff --git a/rule/argument_limit.go b/rule/argument_limit.go index b6ce0e81a..b4d56de0e 100644 --- a/rule/argument_limit.go +++ b/rule/argument_limit.go @@ -11,18 +11,13 @@ import ( // ArgumentsLimitRule lints given else constructs. type ArgumentsLimitRule struct { max int - sync.Mutex + + configureOnce sync.Once } const defaultArgumentsLimit = 8 func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.max != 0 { - return - } - if len(arguments) < 1 { r.max = defaultArgumentsLimit return @@ -37,7 +32,7 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/banned_characters.go b/rule/banned_characters.go index 12997bae1..926b32c21 100644 --- a/rule/banned_characters.go +++ b/rule/banned_characters.go @@ -12,15 +12,14 @@ import ( // BannedCharsRule checks if a file contains banned characters. type BannedCharsRule struct { bannedCharList []string - sync.Mutex + + configureOnce sync.Once } const bannedCharsRuleName = "banned-characters" func (r *BannedCharsRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.bannedCharList == nil && len(arguments) > 0 { + if len(arguments) > 0 { checkNumberOfArguments(1, arguments, bannedCharsRuleName) r.bannedCharList = r.getBannedCharsList(arguments) } @@ -28,7 +27,7 @@ func (r *BannedCharsRule) configure(arguments lint.Arguments) { // Apply applied the rule to the given file. func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/cognitive_complexity.go b/rule/cognitive_complexity.go index 83640fd3d..ecde3882e 100644 --- a/rule/cognitive_complexity.go +++ b/rule/cognitive_complexity.go @@ -13,18 +13,13 @@ import ( // CognitiveComplexityRule lints given else constructs. type CognitiveComplexityRule struct { maxComplexity int - sync.Mutex + + configureOnce sync.Once } const defaultMaxCognitiveComplexity = 7 func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.maxComplexity != 0 { - return // already configured - } - if len(arguments) < 1 { r.maxComplexity = defaultMaxCognitiveComplexity return @@ -40,7 +35,7 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/comment_spacings.go b/rule/comment_spacings.go index f72151301..79a56d4ac 100644 --- a/rule/comment_spacings.go +++ b/rule/comment_spacings.go @@ -12,16 +12,11 @@ import ( // the comment symbol( // ) and the start of the comment text type CommentSpacingsRule struct { allowList []string - sync.Mutex + + configureOnce sync.Once } func (r *CommentSpacingsRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.allowList != nil { - return // already configured - } - r.allowList = []string{} for _, arg := range arguments { allow, ok := arg.(string) // Alt. non panicking version diff --git a/rule/comments_density.go b/rule/comments_density.go index c5298ea07..f2382b1f0 100644 --- a/rule/comments_density.go +++ b/rule/comments_density.go @@ -12,22 +12,13 @@ import ( // CommentsDensityRule lints given else constructs. type CommentsDensityRule struct { minimumCommentsDensity int64 - configured bool - sync.Mutex + + configureOnce sync.Once } const defaultMinimumCommentsPercentage = 0 func (r *CommentsDensityRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.configured { - return - } - - r.configured = true - if len(arguments) < 1 { r.minimumCommentsDensity = defaultMinimumCommentsPercentage return @@ -42,7 +33,7 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) commentsLines := countDocLines(file.AST.Comments) statementsCount := countStatements(file.AST) diff --git a/rule/context_as_argument.go b/rule/context_as_argument.go index e0c8cfa5e..8bc5f8b61 100644 --- a/rule/context_as_argument.go +++ b/rule/context_as_argument.go @@ -12,32 +12,31 @@ import ( // ContextAsArgumentRule lints given else constructs. type ContextAsArgumentRule struct { allowTypesLUT map[string]struct{} - sync.Mutex + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - r.Lock() - if r.allowTypesLUT == nil { - r.allowTypesLUT = getAllowTypesFromArguments(args) - } - r.Unlock() + r.configureOnce.Do(func() { r.configure(args) }) var failures []lint.Failure - r.Lock() walker := lintContextArguments{ allowTypesLUT: r.allowTypesLUT, onFailure: func(failure lint.Failure) { failures = append(failures, failure) }, } - r.Unlock() ast.Walk(walker, file.AST) return failures } +func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) { + r.allowTypesLUT = getAllowTypesFromArguments(arguments) +} + // Name returns the rule name. func (*ContextAsArgumentRule) Name() string { return "context-as-argument" diff --git a/rule/cyclomatic.go b/rule/cyclomatic.go index 10413de24..c1a2de97a 100644 --- a/rule/cyclomatic.go +++ b/rule/cyclomatic.go @@ -14,18 +14,13 @@ import ( // CyclomaticRule lints given else constructs. type CyclomaticRule struct { maxComplexity int - sync.Mutex + + configureOnce sync.Once } const defaultMaxCyclomaticComplexity = 10 func (r *CyclomaticRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.maxComplexity != 0 { - return // already configured - } - if len(arguments) < 1 { r.maxComplexity = defaultMaxCyclomaticComplexity return @@ -40,7 +35,7 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure fileAst := file.AST diff --git a/rule/defer.go b/rule/defer.go index 3c31d507b..f7c716eb6 100644 --- a/rule/defer.go +++ b/rule/defer.go @@ -11,22 +11,17 @@ import ( // DeferRule lints unused params in functions. type DeferRule struct { allow map[string]bool - sync.Mutex + + configureOnce sync.Once } func (r *DeferRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.allow != nil { - return // already configured - } - r.allow = r.allowFromArgs(arguments) } // Apply applies the rule to given file. func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/dot_imports.go b/rule/dot_imports.go index df0b2a7f4..f6c7fbcfb 100644 --- a/rule/dot_imports.go +++ b/rule/dot_imports.go @@ -10,13 +10,14 @@ import ( // DotImportsRule lints given else constructs. type DotImportsRule struct { - sync.Mutex allowedPackages allowPackages + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure @@ -41,13 +42,6 @@ func (*DotImportsRule) Name() string { } func (r *DotImportsRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.allowedPackages != nil { - return - } - r.allowedPackages = make(allowPackages) if len(arguments) == 0 { return diff --git a/rule/enforce_map_style.go b/rule/enforce_map_style.go index c698c40ed..7ddf31e35 100644 --- a/rule/enforce_map_style.go +++ b/rule/enforce_map_style.go @@ -39,20 +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 { - configured bool enforceMapStyle enforceMapStyleType - sync.Mutex + + configureOnce sync.Once } func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.configured { - return - } - r.configured = true - if len(arguments) < 1 { r.enforceMapStyle = enforceMapStyleTypeAny return @@ -72,7 +64,7 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) 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 a435ee186..3f9712aef 100644 --- a/rule/enforce_repeated_arg_type_style.go +++ b/rule/enforce_repeated_arg_type_style.go @@ -41,22 +41,13 @@ func repeatedArgTypeStyleFromString(s string) enforceRepeatedArgTypeStyleType { // EnforceRepeatedArgTypeStyleRule implements a rule to enforce repeated argument type style. type EnforceRepeatedArgTypeStyleRule struct { - configured bool funcArgStyle enforceRepeatedArgTypeStyleType funcRetValStyle enforceRepeatedArgTypeStyleType - sync.Mutex + configureOnce sync.Once } func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.configured { - return - } - r.configured = true - r.funcArgStyle = enforceRepeatedArgTypeStyleTypeAny r.funcRetValStyle = enforceRepeatedArgTypeStyleTypeAny @@ -94,7 +85,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) { // Apply applies the rule to a given file. func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny { // This linter is not configured, return no failures. diff --git a/rule/enforce_slice_style.go b/rule/enforce_slice_style.go index 14be25893..7170379d9 100644 --- a/rule/enforce_slice_style.go +++ b/rule/enforce_slice_style.go @@ -43,20 +43,12 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) { // EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`. type EnforceSliceStyleRule struct { - configured bool enforceSliceStyle enforceSliceStyleType - sync.Mutex + + configureOnce sync.Once } func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.configured { - return - } - r.configured = true - if len(arguments) < 1 { r.enforceSliceStyle = enforceSliceStyleTypeAny return @@ -76,7 +68,7 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) if r.enforceSliceStyle == enforceSliceStyleTypeAny { // this linter is not configured diff --git a/rule/error_strings.go b/rule/error_strings.go index 81ebda540..97a0f4d06 100644 --- a/rule/error_strings.go +++ b/rule/error_strings.go @@ -15,17 +15,11 @@ import ( // ErrorStringsRule lints given else constructs. type ErrorStringsRule struct { errorFunctions map[string]map[string]struct{} - sync.Mutex + + configureOnce sync.Once } func (r *ErrorStringsRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.errorFunctions != nil { - return - } - r.errorFunctions = map[string]map[string]struct{}{ "fmt": { "Errorf": {}, @@ -60,7 +54,7 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) { func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var failures []lint.Failure - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) fileAst := file.AST walker := lintErrorStrings{ diff --git a/rule/exported.go b/rule/exported.go index e3972d40e..7ee27b309 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -55,20 +55,13 @@ func (dc *disabledChecks) isDisabled(checkName string) bool { // ExportedRule lints given else constructs. type ExportedRule struct { - configured bool stuttersMsg string disabledChecks disabledChecks - sync.Mutex + + configureOnce sync.Once } func (r *ExportedRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.configured { - return - } - r.configured = true - r.disabledChecks = disabledChecks{PrivateReceivers: true, PublicInterfaces: true} r.stuttersMsg = "stutters" for _, flag := range arguments { @@ -104,7 +97,7 @@ func (r *ExportedRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *ExportedRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - r.configure(args) + r.configureOnce.Do(func() { r.configure(args) }) var failures []lint.Failure if file.IsTest() { diff --git a/rule/file_header.go b/rule/file_header.go index 0dcb57746..52513d8e8 100644 --- a/rule/file_header.go +++ b/rule/file_header.go @@ -11,7 +11,8 @@ import ( // FileHeaderRule lints given else constructs. type FileHeaderRule struct { header string - sync.Mutex + + configureOnce sync.Once } var ( @@ -20,12 +21,6 @@ var ( ) func (r *FileHeaderRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.header != "" { - return // already configured - } - if len(arguments) < 1 { return } @@ -39,7 +34,7 @@ func (r *FileHeaderRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) if r.header == "" { return nil diff --git a/rule/file_length_limit.go b/rule/file_length_limit.go index c5a5641f4..0fe075c56 100644 --- a/rule/file_length_limit.go +++ b/rule/file_length_limit.go @@ -20,12 +20,13 @@ type FileLengthLimitRule struct { skipComments bool // skipBlankLines indicates whether to skip blank lines when counting lines. skipBlankLines bool - sync.Mutex + + configureOnce sync.Once } // Apply applies the rule to given file. func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) if r.max <= 0 { // when max is negative or 0 the rule is disabled @@ -75,13 +76,6 @@ func (r *FileLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) [ } func (r *FileLengthLimitRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.max != 0 { - return // already configured - } - if len(arguments) < 1 { return // use default } diff --git a/rule/filename_format.go b/rule/filename_format.go index 49fdf9c3e..9d8047829 100644 --- a/rule/filename_format.go +++ b/rule/filename_format.go @@ -13,12 +13,13 @@ import ( // FilenameFormatRule lints source filenames according to a set of regular expressions given as arguments type FilenameFormatRule struct { format *regexp.Regexp - sync.Mutex + + configureOnce sync.Once } // Apply applies the rule to the given file. func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) filename := filepath.Base(file.Name) if r.format.MatchString(filename) { @@ -55,13 +56,6 @@ func (*FilenameFormatRule) Name() string { var defaultFormat = regexp.MustCompile("^[_A-Za-z0-9][_A-Za-z0-9-]*.go$") func (r *FilenameFormatRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.format != nil { - return - } - argsCount := len(arguments) if argsCount == 0 { r.format = defaultFormat diff --git a/rule/function_length.go b/rule/function_length.go index 30402313d..c58cd4c0f 100644 --- a/rule/function_length.go +++ b/rule/function_length.go @@ -11,20 +11,13 @@ import ( // FunctionLength lint. type FunctionLength struct { - maxStmt int - maxLines int - configured bool - sync.Mutex + maxStmt int + maxLines int + + configureOnce sync.Once } func (r *FunctionLength) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.configured { - return - } - - r.configured = true maxStmt, maxLines := r.parseArguments(arguments) r.maxStmt = int(maxStmt) r.maxLines = int(maxLines) @@ -32,7 +25,7 @@ func (r *FunctionLength) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/function_result_limit.go b/rule/function_result_limit.go index 23474b5ee..5b72f01ab 100644 --- a/rule/function_result_limit.go +++ b/rule/function_result_limit.go @@ -11,18 +11,13 @@ import ( // FunctionResultsLimitRule lints given else constructs. type FunctionResultsLimitRule struct { max int - sync.Mutex + + configureOnce sync.Once } const defaultResultsLimit = 3 func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.max != 0 { - return // already configured - } - if len(arguments) < 1 { r.max = defaultResultsLimit return @@ -41,7 +36,7 @@ func (r *FunctionResultsLimitRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *FunctionResultsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/import_alias_naming.go b/rule/import_alias_naming.go index 48d22566a..043bf0d76 100644 --- a/rule/import_alias_naming.go +++ b/rule/import_alias_naming.go @@ -10,10 +10,10 @@ import ( // ImportAliasNamingRule lints import alias naming. type ImportAliasNamingRule struct { - configured bool allowRegexp *regexp.Regexp denyRegexp *regexp.Regexp - sync.Mutex + + configureOnce sync.Once } const defaultImportAliasNamingAllowRule = "^[a-z][a-z0-9]{0,}$" @@ -21,12 +21,6 @@ const defaultImportAliasNamingAllowRule = "^[a-z][a-z0-9]{0,}$" var defaultImportAliasNamingAllowRegexp = regexp.MustCompile(defaultImportAliasNamingAllowRule) func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.configured { - return - } - if len(arguments) == 0 { r.allowRegexp = defaultImportAliasNamingAllowRegexp return @@ -57,7 +51,7 @@ func (r *ImportAliasNamingRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *ImportAliasNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/imports_blocklist.go b/rule/imports_blocklist.go index 431066403..18d77ca1c 100644 --- a/rule/imports_blocklist.go +++ b/rule/imports_blocklist.go @@ -11,29 +11,24 @@ import ( // ImportsBlocklistRule lints given else constructs. type ImportsBlocklistRule struct { blocklist []*regexp.Regexp - sync.Mutex + + configureOnce sync.Once } var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`) func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.blocklist == nil { - r.blocklist = make([]*regexp.Regexp, 0) - - 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)) - } - 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)) - } - r.blocklist = append(r.blocklist, regStr) + r.blocklist = []*regexp.Regexp{} + 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)) + } + 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)) } + r.blocklist = append(r.blocklist, regStr) } } @@ -48,7 +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.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/line_length_limit.go b/rule/line_length_limit.go index a154b7aec..415761e1e 100644 --- a/rule/line_length_limit.go +++ b/rule/line_length_limit.go @@ -15,18 +15,13 @@ import ( // LineLengthLimitRule lints given else constructs. type LineLengthLimitRule struct { max int - sync.Mutex + + configureOnce sync.Once } const defaultLineLengthLimit = 80 func (r *LineLengthLimitRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.max != 0 { - return // already configured - } - if len(arguments) < 1 { r.max = defaultLineLengthLimit return @@ -42,7 +37,7 @@ func (r *LineLengthLimitRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *LineLengthLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/max_control_nesting.go b/rule/max_control_nesting.go index 5dbb1eefa..b2c5af70e 100644 --- a/rule/max_control_nesting.go +++ b/rule/max_control_nesting.go @@ -11,14 +11,15 @@ import ( // MaxControlNestingRule lints given else constructs. type MaxControlNestingRule struct { max int64 - sync.Mutex + + 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 { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure @@ -107,12 +108,6 @@ func (w *lintMaxControlNesting) walkControlledBlock(b ast.Node) { } func (r *MaxControlNestingRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if !(r.max < 1) { - return // max already configured - } - if len(arguments) < 1 { r.max = defaultMaxControlNesting return diff --git a/rule/max_public_structs.go b/rule/max_public_structs.go index 70840e734..f21b636ab 100644 --- a/rule/max_public_structs.go +++ b/rule/max_public_structs.go @@ -11,18 +11,13 @@ import ( // MaxPublicStructsRule lints given else constructs. type MaxPublicStructsRule struct { max int64 - sync.Mutex + + configureOnce sync.Once } const defaultMaxPublicStructs = 5 func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.max == 0 { - return // already configured - } - if len(arguments) < 1 { r.max = defaultMaxPublicStructs return @@ -39,7 +34,7 @@ func (r *MaxPublicStructsRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure diff --git a/rule/receiver_naming.go b/rule/receiver_naming.go index afcd99b8f..c83bacc2f 100644 --- a/rule/receiver_naming.go +++ b/rule/receiver_naming.go @@ -12,18 +12,13 @@ import ( // ReceiverNamingRule lints given else constructs. type ReceiverNamingRule struct { receiverNameMaxLength int - sync.Mutex + + configureOnce sync.Once } const defaultReceiverNameMaxLength = -1 // thus will not check func (r *ReceiverNamingRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.receiverNameMaxLength != 0 { - return - } - r.receiverNameMaxLength = defaultReceiverNameMaxLength if len(arguments) < 1 { return @@ -50,7 +45,7 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - r.configure(args) + r.configureOnce.Do(func() { r.configure(args) }) var failures []lint.Failure diff --git a/rule/struct_tag.go b/rule/struct_tag.go index ec3f0c7cf..d5bc0eafb 100644 --- a/rule/struct_tag.go +++ b/rule/struct_tag.go @@ -14,13 +14,11 @@ import ( // StructTagRule lints struct tags. type StructTagRule struct { userDefined map[string][]string // map: key -> []option - sync.Mutex + + configureOnce sync.Once } func (r *StructTagRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - mustConfigure := r.userDefined == nil && len(arguments) > 0 if !mustConfigure { return @@ -47,7 +45,7 @@ func (r *StructTagRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *StructTagRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - r.configure(args) + r.configureOnce.Do(func() { r.configure(args) }) var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/unchecked_type_assertion.go b/rule/unchecked_type_assertion.go index eea344060..adbd19cf7 100644 --- a/rule/unchecked_type_assertion.go +++ b/rule/unchecked_type_assertion.go @@ -15,21 +15,16 @@ const ( // UncheckedTypeAssertionRule lints missing or ignored `ok`-value in dynamic type casts. type UncheckedTypeAssertionRule struct { - sync.Mutex acceptIgnoredAssertionResult bool - configured bool + + configureOnce sync.Once } func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) { - u.Lock() - defer u.Unlock() - - if len(arguments) == 0 || u.configured { + if len(arguments) == 0 { return } - u.configured = true - args, ok := arguments[0].(map[string]any) if !ok { panic("Unable to get arguments. Expected object of key-value-pairs.") @@ -50,7 +45,7 @@ func (u *UncheckedTypeAssertionRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (u *UncheckedTypeAssertionRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - u.configure(args) + u.configureOnce.Do(func() { u.configure(args) }) var failures []lint.Failure diff --git a/rule/unhandled_error.go b/rule/unhandled_error.go index 95ba56180..4fad8ccfc 100644 --- a/rule/unhandled_error.go +++ b/rule/unhandled_error.go @@ -14,17 +14,11 @@ import ( // UnhandledErrorRule lints given else constructs. type UnhandledErrorRule struct { ignoreList []*regexp.Regexp - sync.Mutex + + configureOnce sync.Once } func (r *UnhandledErrorRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.ignoreList != nil { - return // already configured - } - for _, arg := range arguments { argStr, ok := arg.(string) if !ok { @@ -47,7 +41,7 @@ func (r *UnhandledErrorRule) configure(arguments lint.Arguments) { // Apply applies the rule to given file. func (r *UnhandledErrorRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - r.configure(args) + r.configureOnce.Do(func() { r.configure(args) }) var failures []lint.Failure diff --git a/rule/unused_param.go b/rule/unused_param.go index 4b04ee916..a8514ac2d 100644 --- a/rule/unused_param.go +++ b/rule/unused_param.go @@ -11,22 +11,14 @@ import ( // UnusedParamRule lints unused params in functions. type UnusedParamRule struct { - configured bool // regex to check if some name is valid for unused parameter, "^_$" by default allowRegex *regexp.Regexp failureMsg string - sync.Mutex + + configureOnce sync.Once } func (r *UnusedParamRule) configure(args lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.configured { - return - } - r.configured = true - // 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 var allowedRegexStr string @@ -58,7 +50,7 @@ func (r *UnusedParamRule) configure(args lint.Arguments) { // Apply applies the rule to given file. func (r *UnusedParamRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - r.configure(args) + r.configureOnce.Do(func() { r.configure(args) }) var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/unused_receiver.go b/rule/unused_receiver.go index 715dba338..131aae5fb 100644 --- a/rule/unused_receiver.go +++ b/rule/unused_receiver.go @@ -11,22 +11,14 @@ import ( // UnusedReceiverRule lints unused params in functions. type UnusedReceiverRule struct { - configured bool // regex to check if some name is valid for unused parameter, "^_$" by default allowRegex *regexp.Regexp failureMsg string - sync.Mutex + + configureOnce sync.Once } func (r *UnusedReceiverRule) configure(args lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.configured { - return - } - r.configured = true - // 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 var allowedRegexStr string @@ -57,7 +49,7 @@ func (r *UnusedReceiverRule) configure(args lint.Arguments) { // Apply applies the rule to given file. func (r *UnusedReceiverRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure { - r.configure(args) + r.configureOnce.Do(func() { r.configure(args) }) var failures []lint.Failure onFailure := func(failure lint.Failure) { diff --git a/rule/var_naming.go b/rule/var_naming.go index 5a4d0dc24..2c2198dbd 100644 --- a/rule/var_naming.go +++ b/rule/var_naming.go @@ -18,22 +18,15 @@ var upperCaseConstRE = regexp.MustCompile(`^_?[A-Z][A-Z\d]*(_[A-Z\d]+)*$`) // VarNamingRule lints given else constructs. type VarNamingRule struct { - configured bool allowList []string blockList []string allowUpperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants skipPackageNameChecks bool - sync.Mutex + + configureOnce sync.Once } func (r *VarNamingRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - if r.configured { - return - } - - r.configured = true if len(arguments) >= 1 { r.allowList = getList(arguments[0], "allowlist") } @@ -83,7 +76,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 { - r.configure(arguments) + r.configureOnce.Do(func() { r.configure(arguments) }) var failures []lint.Failure