Skip to content

Commit 3378f70

Browse files
authored
refactor: replace mutex with sync.Once for rule configuration (#1118)
1 parent 7ee4500 commit 3378f70

32 files changed

+118
-297
lines changed

rule/add_constant.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ type AddConstantRule struct {
3636
allowList allowList
3737
ignoreFunctions []*regexp.Regexp
3838
strLitLimit int
39-
sync.Mutex
39+
40+
configureOnce sync.Once
4041
}
4142

4243
// Apply applies the rule to given file.
4344
func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
44-
r.configure(arguments)
45+
r.configureOnce.Do(func() { r.configure(arguments) })
4546

4647
var failures []lint.Failure
4748

@@ -201,9 +202,6 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool {
201202
}
202203

203204
func (r *AddConstantRule) configure(arguments lint.Arguments) {
204-
r.Lock()
205-
defer r.Unlock()
206-
207205
if r.allowList == nil {
208206
r.strLitLimit = defaultStrLitLimit
209207
r.allowList = newAllowList()

rule/argument_limit.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@ import (
1111
// ArgumentsLimitRule lints given else constructs.
1212
type ArgumentsLimitRule struct {
1313
max int
14-
sync.Mutex
14+
15+
configureOnce sync.Once
1516
}
1617

1718
const defaultArgumentsLimit = 8
1819

1920
func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) {
20-
r.Lock()
21-
defer r.Unlock()
22-
if r.max != 0 {
23-
return
24-
}
25-
2621
if len(arguments) < 1 {
2722
r.max = defaultArgumentsLimit
2823
return
@@ -37,7 +32,7 @@ func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) {
3732

3833
// Apply applies the rule to given file.
3934
func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
40-
r.configure(arguments)
35+
r.configureOnce.Do(func() { r.configure(arguments) })
4136

4237
var failures []lint.Failure
4338
onFailure := func(failure lint.Failure) {

rule/banned_characters.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ import (
1212
// BannedCharsRule checks if a file contains banned characters.
1313
type BannedCharsRule struct {
1414
bannedCharList []string
15-
sync.Mutex
15+
16+
configureOnce sync.Once
1617
}
1718

1819
const bannedCharsRuleName = "banned-characters"
1920

2021
func (r *BannedCharsRule) configure(arguments lint.Arguments) {
21-
r.Lock()
22-
defer r.Unlock()
23-
if r.bannedCharList == nil && len(arguments) > 0 {
22+
if len(arguments) > 0 {
2423
checkNumberOfArguments(1, arguments, bannedCharsRuleName)
2524
r.bannedCharList = r.getBannedCharsList(arguments)
2625
}
2726
}
2827

2928
// Apply applied the rule to the given file.
3029
func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
31-
r.configure(arguments)
30+
r.configureOnce.Do(func() { r.configure(arguments) })
3231

3332
var failures []lint.Failure
3433
onFailure := func(failure lint.Failure) {

rule/cognitive_complexity.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@ import (
1313
// CognitiveComplexityRule lints given else constructs.
1414
type CognitiveComplexityRule struct {
1515
maxComplexity int
16-
sync.Mutex
16+
17+
configureOnce sync.Once
1718
}
1819

1920
const defaultMaxCognitiveComplexity = 7
2021

2122
func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) {
22-
r.Lock()
23-
defer r.Unlock()
24-
if r.maxComplexity != 0 {
25-
return // already configured
26-
}
27-
2823
if len(arguments) < 1 {
2924
r.maxComplexity = defaultMaxCognitiveComplexity
3025
return
@@ -40,7 +35,7 @@ func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) {
4035

4136
// Apply applies the rule to given file.
4237
func (r *CognitiveComplexityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
43-
r.configure(arguments)
38+
r.configureOnce.Do(func() { r.configure(arguments) })
4439

4540
var failures []lint.Failure
4641

rule/comment_spacings.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,11 @@ import (
1212
// the comment symbol( // ) and the start of the comment text
1313
type CommentSpacingsRule struct {
1414
allowList []string
15-
sync.Mutex
15+
16+
configureOnce sync.Once
1617
}
1718

1819
func (r *CommentSpacingsRule) configure(arguments lint.Arguments) {
19-
r.Lock()
20-
defer r.Unlock()
21-
if r.allowList != nil {
22-
return // already configured
23-
}
24-
2520
r.allowList = []string{}
2621
for _, arg := range arguments {
2722
allow, ok := arg.(string) // Alt. non panicking version
@@ -34,7 +29,7 @@ func (r *CommentSpacingsRule) configure(arguments lint.Arguments) {
3429

3530
// Apply the rule.
3631
func (r *CommentSpacingsRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
37-
r.configure(args)
32+
r.configureOnce.Do(func() { r.configure(args) })
3833

3934
var failures []lint.Failure
4035

rule/comments_density.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@ import (
1212
// CommentsDensityRule lints given else constructs.
1313
type CommentsDensityRule struct {
1414
minimumCommentsDensity int64
15-
configured bool
16-
sync.Mutex
15+
16+
configureOnce sync.Once
1717
}
1818

1919
const defaultMinimumCommentsPercentage = 0
2020

2121
func (r *CommentsDensityRule) configure(arguments lint.Arguments) {
22-
r.Lock()
23-
defer r.Unlock()
24-
25-
if r.configured {
26-
return
27-
}
28-
29-
r.configured = true
30-
3122
if len(arguments) < 1 {
3223
r.minimumCommentsDensity = defaultMinimumCommentsPercentage
3324
return
@@ -42,7 +33,7 @@ func (r *CommentsDensityRule) configure(arguments lint.Arguments) {
4233

4334
// Apply applies the rule to given file.
4435
func (r *CommentsDensityRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
45-
r.configure(arguments)
36+
r.configureOnce.Do(func() { r.configure(arguments) })
4637

4738
commentsLines := countDocLines(file.AST.Comments)
4839
statementsCount := countStatements(file.AST)

rule/context_as_argument.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,31 @@ import (
1212
// ContextAsArgumentRule lints given else constructs.
1313
type ContextAsArgumentRule struct {
1414
allowTypesLUT map[string]struct{}
15-
sync.Mutex
15+
16+
configureOnce sync.Once
1617
}
1718

1819
// Apply applies the rule to given file.
1920
func (r *ContextAsArgumentRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
20-
r.Lock()
21-
if r.allowTypesLUT == nil {
22-
r.allowTypesLUT = getAllowTypesFromArguments(args)
23-
}
24-
r.Unlock()
21+
r.configureOnce.Do(func() { r.configure(args) })
2522

2623
var failures []lint.Failure
27-
r.Lock()
2824
walker := lintContextArguments{
2925
allowTypesLUT: r.allowTypesLUT,
3026
onFailure: func(failure lint.Failure) {
3127
failures = append(failures, failure)
3228
},
3329
}
34-
r.Unlock()
3530

3631
ast.Walk(walker, file.AST)
3732

3833
return failures
3934
}
4035

36+
func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) {
37+
r.allowTypesLUT = getAllowTypesFromArguments(arguments)
38+
}
39+
4140
// Name returns the rule name.
4241
func (*ContextAsArgumentRule) Name() string {
4342
return "context-as-argument"

rule/cyclomatic.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ import (
1414
// CyclomaticRule lints given else constructs.
1515
type CyclomaticRule struct {
1616
maxComplexity int
17-
sync.Mutex
17+
18+
configureOnce sync.Once
1819
}
1920

2021
const defaultMaxCyclomaticComplexity = 10
2122

2223
func (r *CyclomaticRule) configure(arguments lint.Arguments) {
23-
r.Lock()
24-
defer r.Unlock()
25-
if r.maxComplexity != 0 {
26-
return // already configured
27-
}
28-
2924
if len(arguments) < 1 {
3025
r.maxComplexity = defaultMaxCyclomaticComplexity
3126
return
@@ -40,7 +35,7 @@ func (r *CyclomaticRule) configure(arguments lint.Arguments) {
4035

4136
// Apply applies the rule to given file.
4237
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
43-
r.configure(arguments)
38+
r.configureOnce.Do(func() { r.configure(arguments) })
4439

4540
var failures []lint.Failure
4641
fileAst := file.AST

rule/defer.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,17 @@ import (
1111
// DeferRule lints unused params in functions.
1212
type DeferRule struct {
1313
allow map[string]bool
14-
sync.Mutex
14+
15+
configureOnce sync.Once
1516
}
1617

1718
func (r *DeferRule) configure(arguments lint.Arguments) {
18-
r.Lock()
19-
defer r.Unlock()
20-
if r.allow != nil {
21-
return // already configured
22-
}
23-
2419
r.allow = r.allowFromArgs(arguments)
2520
}
2621

2722
// Apply applies the rule to given file.
2823
func (r *DeferRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
29-
r.configure(arguments)
24+
r.configureOnce.Do(func() { r.configure(arguments) })
3025

3126
var failures []lint.Failure
3227
onFailure := func(failure lint.Failure) {

rule/dot_imports.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010

1111
// DotImportsRule lints given else constructs.
1212
type DotImportsRule struct {
13-
sync.Mutex
1413
allowedPackages allowPackages
14+
15+
configureOnce sync.Once
1516
}
1617

1718
// Apply applies the rule to given file.
1819
func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
19-
r.configure(arguments)
20+
r.configureOnce.Do(func() { r.configure(arguments) })
2021

2122
var failures []lint.Failure
2223

@@ -41,13 +42,6 @@ func (*DotImportsRule) Name() string {
4142
}
4243

4344
func (r *DotImportsRule) configure(arguments lint.Arguments) {
44-
r.Lock()
45-
defer r.Unlock()
46-
47-
if r.allowedPackages != nil {
48-
return
49-
}
50-
5145
r.allowedPackages = make(allowPackages)
5246
if len(arguments) == 0 {
5347
return

rule/enforce_map_style.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,12 @@ func mapStyleFromString(s string) (enforceMapStyleType, error) {
3939

4040
// EnforceMapStyleRule implements a rule to enforce `make(map[type]type)` over `map[type]type{}`.
4141
type EnforceMapStyleRule struct {
42-
configured bool
4342
enforceMapStyle enforceMapStyleType
44-
sync.Mutex
43+
44+
configureOnce sync.Once
4545
}
4646

4747
func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) {
48-
r.Lock()
49-
defer r.Unlock()
50-
51-
if r.configured {
52-
return
53-
}
54-
r.configured = true
55-
5648
if len(arguments) < 1 {
5749
r.enforceMapStyle = enforceMapStyleTypeAny
5850
return
@@ -72,7 +64,7 @@ func (r *EnforceMapStyleRule) configure(arguments lint.Arguments) {
7264

7365
// Apply applies the rule to given file.
7466
func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
75-
r.configure(arguments)
67+
r.configureOnce.Do(func() { r.configure(arguments) })
7668

7769
if r.enforceMapStyle == enforceMapStyleTypeAny {
7870
// this linter is not configured

rule/enforce_repeated_arg_type_style.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,13 @@ func repeatedArgTypeStyleFromString(s string) enforceRepeatedArgTypeStyleType {
4141

4242
// EnforceRepeatedArgTypeStyleRule implements a rule to enforce repeated argument type style.
4343
type EnforceRepeatedArgTypeStyleRule struct {
44-
configured bool
4544
funcArgStyle enforceRepeatedArgTypeStyleType
4645
funcRetValStyle enforceRepeatedArgTypeStyleType
4746

48-
sync.Mutex
47+
configureOnce sync.Once
4948
}
5049

5150
func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) {
52-
r.Lock()
53-
defer r.Unlock()
54-
55-
if r.configured {
56-
return
57-
}
58-
r.configured = true
59-
6051
r.funcArgStyle = enforceRepeatedArgTypeStyleTypeAny
6152
r.funcRetValStyle = enforceRepeatedArgTypeStyleTypeAny
6253

@@ -94,7 +85,7 @@ func (r *EnforceRepeatedArgTypeStyleRule) configure(arguments lint.Arguments) {
9485

9586
// Apply applies the rule to a given file.
9687
func (r *EnforceRepeatedArgTypeStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
97-
r.configure(arguments)
88+
r.configureOnce.Do(func() { r.configure(arguments) })
9889

9990
if r.funcArgStyle == enforceRepeatedArgTypeStyleTypeAny && r.funcRetValStyle == enforceRepeatedArgTypeStyleTypeAny {
10091
// This linter is not configured, return no failures.

rule/enforce_slice_style.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,12 @@ func sliceStyleFromString(s string) (enforceSliceStyleType, error) {
4343

4444
// EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`.
4545
type EnforceSliceStyleRule struct {
46-
configured bool
4746
enforceSliceStyle enforceSliceStyleType
48-
sync.Mutex
47+
48+
configureOnce sync.Once
4949
}
5050

5151
func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) {
52-
r.Lock()
53-
defer r.Unlock()
54-
55-
if r.configured {
56-
return
57-
}
58-
r.configured = true
59-
6052
if len(arguments) < 1 {
6153
r.enforceSliceStyle = enforceSliceStyleTypeAny
6254
return
@@ -76,7 +68,7 @@ func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) {
7668

7769
// Apply applies the rule to given file.
7870
func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
79-
r.configure(arguments)
71+
r.configureOnce.Do(func() { r.configure(arguments) })
8072

8173
if r.enforceSliceStyle == enforceSliceStyleTypeAny {
8274
// this linter is not configured

0 commit comments

Comments
 (0)