Skip to content

Commit ca2a32b

Browse files
authored
code cleanup (#1054)
1 parent 798ce21 commit ca2a32b

30 files changed

+315
-252
lines changed

rule/add-constant.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,15 @@ func (w *lintAddConstantRule) isIgnoredFunc(fName string) bool {
160160
}
161161

162162
func (w *lintAddConstantRule) checkStrLit(n *ast.BasicLit) {
163+
const IgnoreMarker = -1
164+
163165
if w.allowList[kindSTRING][n.Value] {
164166
return
165167
}
166168

167169
count := w.strLits[n.Value]
168-
if count >= 0 {
170+
mustCheck := count > IgnoreMarker
171+
if mustCheck {
169172
w.strLits[n.Value] = count + 1
170173
if w.strLits[n.Value] > w.strLitLimit {
171174
w.onFailure(lint.Failure{

rule/argument-limit.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// ArgumentsLimitRule lints given else constructs.
1212
type ArgumentsLimitRule struct {
13-
total int
13+
max int
1414
sync.Mutex
1515
}
1616

@@ -19,18 +19,20 @@ const defaultArgumentsLimit = 8
1919
func (r *ArgumentsLimitRule) configure(arguments lint.Arguments) {
2020
r.Lock()
2121
defer r.Unlock()
22-
if r.total == 0 {
23-
if len(arguments) < 1 {
24-
r.total = defaultArgumentsLimit
25-
return
26-
}
22+
if r.max != 0 {
23+
return
24+
}
2725

28-
total, ok := arguments[0].(int64) // Alt. non panicking version
29-
if !ok {
30-
panic(`invalid value passed as argument number to the "argument-limit" rule`)
31-
}
32-
r.total = int(total)
26+
if len(arguments) < 1 {
27+
r.max = defaultArgumentsLimit
28+
return
3329
}
30+
31+
maxArguments, ok := arguments[0].(int64) // Alt. non panicking version
32+
if !ok {
33+
panic(`invalid value passed as argument number to the "argument-limit" rule`)
34+
}
35+
r.max = int(maxArguments)
3436
}
3537

3638
// Apply applies the rule to given file.
@@ -43,7 +45,7 @@ func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []
4345
}
4446

4547
walker := lintArgsNum{
46-
total: r.total,
48+
max: r.max,
4749
onFailure: onFailure,
4850
}
4951

@@ -58,27 +60,30 @@ func (*ArgumentsLimitRule) Name() string {
5860
}
5961

6062
type lintArgsNum struct {
61-
total int
63+
max int
6264
onFailure func(lint.Failure)
6365
}
6466

6567
func (w lintArgsNum) Visit(n ast.Node) ast.Visitor {
6668
node, ok := n.(*ast.FuncDecl)
67-
if ok {
68-
num := 0
69-
for _, l := range node.Type.Params.List {
70-
for range l.Names {
71-
num++
72-
}
73-
}
74-
if num > w.total {
75-
w.onFailure(lint.Failure{
76-
Confidence: 1,
77-
Failure: fmt.Sprintf("maximum number of arguments per function exceeded; max %d but got %d", w.total, num),
78-
Node: node.Type,
79-
})
80-
return w
69+
if !ok {
70+
return w
71+
}
72+
73+
num := 0
74+
for _, l := range node.Type.Params.List {
75+
for range l.Names {
76+
num++
8177
}
8278
}
83-
return w
79+
80+
if num > w.max {
81+
w.onFailure(lint.Failure{
82+
Confidence: 1,
83+
Failure: fmt.Sprintf("maximum number of arguments per function exceeded; max %d but got %d", w.max, num),
84+
Node: node.Type,
85+
})
86+
}
87+
88+
return nil // skip visiting the body of the function
8489
}

rule/blank-imports.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failu
2222
}
2323

2424
const (
25-
message = "a blank import should be only in a main or test package, or have a comment justifying it"
26-
category = "imports"
27-
25+
message = "a blank import should be only in a main or test package, or have a comment justifying it"
26+
category = "imports"
2827
embedImportPath = `"embed"`
2928
)
3029

@@ -39,7 +38,8 @@ func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failu
3938
continue // Ignore non-blank imports.
4039
}
4140

42-
if i > 0 {
41+
isNotFirstElement := i > 0
42+
if isNotFirstElement {
4343
prev := file.AST.Imports[i-1]
4444
prevPos := file.ToPosition(prev.Pos())
4545

rule/bool-literal-in-expr.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func (w *lintBoolLiteral) Visit(node ast.Node) ast.Visitor {
4545
lexeme, ok := isExprABooleanLit(n.X)
4646
if !ok {
4747
lexeme, ok = isExprABooleanLit(n.Y)
48-
4948
if !ok {
5049
return w
5150
}

rule/cognitive-complexity.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ const defaultMaxCognitiveComplexity = 7
2121
func (r *CognitiveComplexityRule) configure(arguments lint.Arguments) {
2222
r.Lock()
2323
defer r.Unlock()
24-
if r.maxComplexity == 0 {
24+
if r.maxComplexity != 0 {
25+
return // already configured
26+
}
2527

26-
if len(arguments) < 1 {
27-
r.maxComplexity = defaultMaxCognitiveComplexity
28-
return
29-
}
28+
if len(arguments) < 1 {
29+
r.maxComplexity = defaultMaxCognitiveComplexity
30+
return
31+
}
3032

31-
complexity, ok := arguments[0].(int64)
32-
if !ok {
33-
panic(fmt.Sprintf("invalid argument type for cognitive-complexity, expected int64, got %T", arguments[0]))
34-
}
35-
r.maxComplexity = int(complexity)
33+
complexity, ok := arguments[0].(int64)
34+
if !ok {
35+
panic(fmt.Sprintf("invalid argument type for cognitive-complexity, expected int64, got %T", arguments[0]))
3636
}
37+
38+
r.maxComplexity = int(complexity)
3739
}
3840

3941
// Apply applies the rule to given file.

rule/comment-spacings.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ type CommentSpacingsRule struct {
1818
func (r *CommentSpacingsRule) configure(arguments lint.Arguments) {
1919
r.Lock()
2020
defer r.Unlock()
21+
if r.allowList != nil {
22+
return // already configured
23+
}
2124

22-
if r.allowList == nil {
23-
r.allowList = []string{}
24-
for _, arg := range arguments {
25-
allow, ok := arg.(string) // Alt. non panicking version
26-
if !ok {
27-
panic(fmt.Sprintf("invalid argument %v for %s; expected string but got %T", arg, r.Name(), arg))
28-
}
29-
r.allowList = append(r.allowList, `//`+allow)
25+
r.allowList = []string{}
26+
for _, arg := range arguments {
27+
allow, ok := arg.(string) // Alt. non panicking version
28+
if !ok {
29+
panic(fmt.Sprintf("invalid argument %v for %s; expected string but got %T", arg, r.Name(), arg))
3030
}
31+
r.allowList = append(r.allowList, `//`+allow)
3132
}
3233
}
3334

rule/constant-logical-expr.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ func (w *lintConstantLogicalExpr) Visit(node ast.Node) ast.Visitor {
4141
return w
4242
}
4343

44-
if gofmt(n.X) != gofmt(n.Y) { // check if subexpressions are the same
45-
return w
44+
subExpressionsAreNotEqual := gofmt(n.X) != gofmt(n.Y)
45+
if subExpressionsAreNotEqual {
46+
return w // nothing to say
4647
}
4748

4849
// Handles cases like: a <= a, a == a, a >= a

rule/cyclomatic.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ const defaultMaxCyclomaticComplexity = 10
2222
func (r *CyclomaticRule) configure(arguments lint.Arguments) {
2323
r.Lock()
2424
defer r.Unlock()
25-
if r.maxComplexity == 0 {
26-
if len(arguments) < 1 {
27-
r.maxComplexity = defaultMaxCyclomaticComplexity
28-
return
29-
}
25+
if r.maxComplexity != 0 {
26+
return // already configured
27+
}
3028

31-
complexity, ok := arguments[0].(int64) // Alt. non panicking version
32-
if !ok {
33-
panic(fmt.Sprintf("invalid argument for cyclomatic complexity; expected int but got %T", arguments[0]))
34-
}
35-
r.maxComplexity = int(complexity)
29+
if len(arguments) < 1 {
30+
r.maxComplexity = defaultMaxCyclomaticComplexity
31+
return
32+
}
33+
34+
complexity, ok := arguments[0].(int64) // Alt. non panicking version
35+
if !ok {
36+
panic(fmt.Sprintf("invalid argument for cyclomatic complexity; expected int but got %T", arguments[0]))
3637
}
38+
r.maxComplexity = int(complexity)
3739
}
3840

3941
// Apply applies the rule to given file.
@@ -70,31 +72,35 @@ type lintCyclomatic struct {
7072
func (w lintCyclomatic) Visit(_ ast.Node) ast.Visitor {
7173
f := w.file
7274
for _, decl := range f.AST.Decls {
73-
if fn, ok := decl.(*ast.FuncDecl); ok {
74-
c := complexity(fn)
75-
if c > w.complexity {
76-
w.onFailure(lint.Failure{
77-
Confidence: 1,
78-
Category: "maintenance",
79-
Failure: fmt.Sprintf("function %s has cyclomatic complexity %d (> max enabled %d)",
80-
funcName(fn), c, w.complexity),
81-
Node: fn,
82-
})
83-
}
75+
fn, ok := decl.(*ast.FuncDecl)
76+
if !ok {
77+
continue
78+
}
79+
80+
c := complexity(fn)
81+
if c > w.complexity {
82+
w.onFailure(lint.Failure{
83+
Confidence: 1,
84+
Category: "maintenance",
85+
Failure: fmt.Sprintf("function %s has cyclomatic complexity %d (> max enabled %d)",
86+
funcName(fn), c, w.complexity),
87+
Node: fn,
88+
})
8489
}
8590
}
91+
8692
return nil
8793
}
8894

8995
// funcName returns the name representation of a function or method:
9096
// "(Type).Name" for methods or simply "Name" for functions.
9197
func funcName(fn *ast.FuncDecl) string {
92-
if fn.Recv != nil {
93-
if fn.Recv.NumFields() > 0 {
94-
typ := fn.Recv.List[0].Type
95-
return fmt.Sprintf("(%s).%s", recvString(typ), fn.Name)
96-
}
98+
declarationHasReceiver := fn.Recv != nil && fn.Recv.NumFields() > 0
99+
if declarationHasReceiver {
100+
typ := fn.Recv.List[0].Type
101+
return fmt.Sprintf("(%s).%s", recvString(typ), fn.Name)
97102
}
103+
98104
return fn.Name.Name
99105
}
100106

rule/deep-exit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ func (w lintDeepExit) Visit(node ast.Node) ast.Visitor {
7373
return w
7474
}
7575

76-
fn := fc.Sel.Name
7776
pkg := id.Name
78-
if w.exitFunctions[pkg] != nil && w.exitFunctions[pkg][fn] { // it's a call to an exit function
77+
fn := fc.Sel.Name
78+
isACallToExitFunction := w.exitFunctions[pkg] != nil && w.exitFunctions[pkg][fn]
79+
if isACallToExitFunction {
7980
w.onFailure(lint.Failure{
8081
Confidence: 1,
8182
Node: ce,

rule/defer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ type DeferRule struct {
1616

1717
func (r *DeferRule) configure(arguments lint.Arguments) {
1818
r.Lock()
19-
if r.allow == nil {
20-
r.allow = r.allowFromArgs(arguments)
19+
defer r.Unlock()
20+
if r.allow != nil {
21+
return // already configured
2122
}
22-
r.Unlock()
23+
24+
r.allow = r.allowFromArgs(arguments)
2325
}
2426

2527
// Apply applies the rule to given file.

rule/dot-imports.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ type lintImports struct {
8181
}
8282

8383
func (w lintImports) Visit(_ ast.Node) ast.Visitor {
84-
for _, is := range w.fileAst.Imports {
85-
if is.Name != nil && is.Name.Name == "." && !w.allowPackages.isAllowedPackage(is.Path.Value) {
84+
for _, importSpec := range w.fileAst.Imports {
85+
isDotImport := importSpec.Name != nil && importSpec.Name.Name == "."
86+
if isDotImport && !w.allowPackages.isAllowedPackage(importSpec.Path.Value) {
8687
w.onFailure(lint.Failure{
8788
Confidence: 1,
8889
Failure: "should not use dot imports",
89-
Node: is,
90+
Node: importSpec,
9091
Category: "imports",
9192
})
9293
}

rule/enforce-map-style.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func (r *EnforceMapStyleRule) Apply(file *lint.File, arguments lint.Arguments) [
9494
return true
9595
}
9696

97-
if len(v.Elts) > 0 {
98-
// not an empty map
97+
isEmptyMap := len(v.Elts) > 0
98+
if isEmptyMap {
9999
return true
100100
}
101101

rule/enforce-slice-style.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments)
101101
return true
102102
}
103103

104-
if len(v.Elts) > 0 {
105-
// not an empty slice
104+
isNotEmptySlice := len(v.Elts) > 0
105+
if isNotEmptySlice {
106106
return true
107107
}
108108

@@ -132,8 +132,8 @@ func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments)
132132
return true
133133
}
134134

135-
if len(v.Args) < 2 {
136-
// skip invalid make declarations
135+
isInvalidMakeDeclaration := len(v.Args) < 2
136+
if isInvalidMakeDeclaration {
137137
return true
138138
}
139139

@@ -148,8 +148,8 @@ func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments)
148148
return true
149149
}
150150

151-
if arg.Value != "0" {
152-
// skip slice with non-zero size
151+
isSliceSizeNotZero := arg.Value != "0"
152+
if isSliceSizeNotZero {
153153
return true
154154
}
155155

@@ -160,8 +160,8 @@ func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments)
160160
return true
161161
}
162162

163-
if arg.Value != "0" {
164-
// skip non-zero capacity slice
163+
isNonZeroCapacitySlice := arg.Value != "0"
164+
if isNonZeroCapacitySlice {
165165
return true
166166
}
167167
}

0 commit comments

Comments
 (0)