Skip to content

Commit 6b14d27

Browse files
committed
Resolve linter warning
1 parent ca09c44 commit 6b14d27

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

cmd/gocognit/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func main() {
111111
jsonEncode bool
112112
ignoreExpr string
113113
)
114+
114115
flag.IntVar(&over, "over", defaultOverFlagVal, "show functions with complexity > N only")
115116
flag.IntVar(&top, "top", defaultTopFlagVal, "show the top N most complex functions only")
116117
flag.BoolVar(&avg, "avg", false, "show the average complexity")
@@ -121,9 +122,11 @@ func main() {
121122

122123
log.SetFlags(0)
123124
log.SetPrefix("gocognit: ")
125+
124126
flag.Usage = usage
125127
flag.Parse()
126128
args := flag.Args()
129+
127130
if len(args) == 0 {
128131
usage()
129132
}
@@ -146,12 +149,14 @@ func main() {
146149
}
147150

148151
filteredStats := filterStats(stats, ignoreRegexp, top, over)
152+
149153
var written int
150154
if jsonEncode {
151155
written, err = writeJSONStats(os.Stdout, filteredStats)
152156
} else {
153157
written, err = writeTextStats(os.Stdout, filteredStats, tmpl)
154158
}
159+
155160
if err != nil {
156161
log.Fatal(err)
157162
}
@@ -175,6 +180,7 @@ func analyzePath(path string, includeTests bool) ([]gocognit.Stat, error) {
175180

176181
func analyze(paths []string, includeTests bool) (stats []gocognit.Stat, err error) {
177182
var out []gocognit.Stat
183+
178184
for _, path := range paths {
179185
stats, err := analyzePath(path, includeTests)
180186
if err != nil {
@@ -189,11 +195,13 @@ func analyze(paths []string, includeTests bool) (stats []gocognit.Stat, err erro
189195

190196
func isDir(filename string) bool {
191197
fi, err := os.Stat(filename)
198+
192199
return err == nil && fi.IsDir()
193200
}
194201

195202
func analyzeFile(fname string, stats []gocognit.Stat) ([]gocognit.Stat, error) {
196203
fset := token.NewFileSet()
204+
197205
f, err := parser.ParseFile(fset, fname, nil, parser.ParseComments)
198206
if err != nil {
199207
return nil, err
@@ -240,6 +248,7 @@ func writeTextStats(w io.Writer, stats []gocognit.Stat, tmpl *template.Template)
240248
if err := tmpl.Execute(w, stat); err != nil {
241249
return i, err
242250
}
251+
243252
fmt.Fprintln(w)
244253
}
245254

@@ -266,6 +275,7 @@ func prepareRegexp(expr string) (*regexp.Regexp, error) {
266275

267276
func filterStats(sortedStats []gocognit.Stat, ignoreRegexp *regexp.Regexp, top, over int) []gocognit.Stat {
268277
var filtered []gocognit.Stat
278+
269279
i := 0
270280
for _, stat := range sortedStats {
271281
if i == top {
@@ -296,6 +306,7 @@ func average(stats []gocognit.Stat) float64 {
296306
for _, s := range stats {
297307
total += s.Complexity
298308
}
309+
299310
return float64(total) / float64(len(stats))
300311
}
301312

doc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
// Package gocognit defines Analyzer other utilities to checks and calculate the complexity of function based on "cognitive complexity" methods.
1+
// Package gocognit defines Analyzer other utilities to checks and calculate
2+
// the complexity of function based on "cognitive complexity" methods.
23
package gocognit

gocognit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func ComplexityStats(f *ast.File, fset *token.FileSet, stats []Stat) []Stat {
3939
})
4040
}
4141
}
42+
4243
return stats
4344
}
4445

@@ -66,9 +67,11 @@ func funcName(fn *ast.FuncDecl) string {
6667
if fn.Recv != nil {
6768
if fn.Recv.NumFields() > 0 {
6869
typ := fn.Recv.List[0].Type
70+
6971
return fmt.Sprintf("(%s).%s", recvString(typ), fn.Name)
7072
}
7173
}
74+
7275
return fn.Name.Name
7376
}
7477

@@ -79,6 +82,7 @@ func Complexity(fn *ast.FuncDecl) int {
7982
}
8083

8184
ast.Walk(&v, fn)
85+
8286
return v.complexity
8387
}
8488

@@ -162,6 +166,7 @@ func (v *complexityVisitor) Visit(n ast.Node) ast.Visitor {
162166
case *ast.CallExpr:
163167
return v.visitCallExpr(n)
164168
}
169+
165170
return v
166171
}
167172

@@ -209,6 +214,7 @@ func (v *complexityVisitor) visitSwitchStmt(n *ast.SwitchStmt) ast.Visitor {
209214
v.incNesting()
210215
ast.Walk(v, n.Body)
211216
v.decNesting()
217+
212218
return nil
213219
}
214220

@@ -226,6 +232,7 @@ func (v *complexityVisitor) visitTypeSwitchStmt(n *ast.TypeSwitchStmt) ast.Visit
226232
v.incNesting()
227233
ast.Walk(v, n.Body)
228234
v.decNesting()
235+
229236
return nil
230237
}
231238

@@ -235,6 +242,7 @@ func (v *complexityVisitor) visitSelectStmt(n *ast.SelectStmt) ast.Visitor {
235242
v.incNesting()
236243
ast.Walk(v, n.Body)
237244
v.decNesting()
245+
238246
return nil
239247
}
240248

@@ -256,6 +264,7 @@ func (v *complexityVisitor) visitForStmt(n *ast.ForStmt) ast.Visitor {
256264
v.incNesting()
257265
ast.Walk(v, n.Body)
258266
v.decNesting()
267+
259268
return nil
260269
}
261270

@@ -275,6 +284,7 @@ func (v *complexityVisitor) visitRangeStmt(n *ast.RangeStmt) ast.Visitor {
275284
v.incNesting()
276285
ast.Walk(v, n.Body)
277286
v.decNesting()
287+
278288
return nil
279289
}
280290

@@ -302,10 +312,12 @@ func (v *complexityVisitor) visitBinaryExpr(n *ast.BinaryExpr) ast.Visitor {
302312
for _, op := range ops {
303313
if lastOp != op {
304314
v.incComplexity()
315+
305316
lastOp = op
306317
}
307318
}
308319
}
320+
309321
return v
310322
}
311323

@@ -317,11 +329,13 @@ func (v *complexityVisitor) visitCallExpr(n *ast.CallExpr) ast.Visitor {
317329
v.incComplexity()
318330
}
319331
}
332+
320333
return v
321334
}
322335

323336
func (v *complexityVisitor) collectBinaryOps(exp ast.Expr) []token.Token {
324337
v.markCalculated(exp)
338+
325339
if exp, ok := exp.(*ast.BinaryExpr); ok {
326340
return mergeBinaryOps(v.collectBinaryOps(exp.X), exp.Op, v.collectBinaryOps(exp.Y))
327341
}
@@ -339,9 +353,11 @@ func (v *complexityVisitor) incIfComplexity(n *ast.IfStmt) {
339353
func mergeBinaryOps(x []token.Token, op token.Token, y []token.Token) []token.Token {
340354
var out []token.Token
341355
out = append(out, x...)
356+
342357
if isBinaryLogicalOp(op) {
343358
out = append(out, op)
344359
}
360+
345361
out = append(out, y...)
346362
return out
347363
}

recv.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ func recvString(recv ast.Expr) string {
2020
case *ast.IndexListExpr:
2121
return recvString(t.X)
2222
}
23+
2324
return "BADRECV"
2425
}

recv_pre118.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ func recvString(recv ast.Expr) string {
1616
case *ast.StarExpr:
1717
return "*" + recvString(t.X)
1818
}
19+
1920
return "BADRECV"
2021
}

0 commit comments

Comments
 (0)