Skip to content

Commit 8f8c68a

Browse files
authored
build(deps): bump github.com/alexkohler/nakedret to 2.0.1 (#3760)
1 parent 252ae9f commit 8f8c68a

File tree

4 files changed

+17
-117
lines changed

4 files changed

+17
-117
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
1414
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.3.0
1515
github.com/OpenPeeDeeP/depguard v1.1.1
16+
github.com/alexkohler/nakedret/v2 v2.0.1
1617
github.com/alexkohler/prealloc v1.0.0
1718
github.com/alingse/asasalint v0.0.11
1819
github.com/ashanbrown/forbidigo v1.5.1

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/nakedret.go

+7-114
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,27 @@
11
package golinters
22

33
import (
4-
"fmt"
5-
"go/ast"
6-
"go/token"
7-
"sync"
8-
4+
"github.com/alexkohler/nakedret/v2"
95
"golang.org/x/tools/go/analysis"
106

117
"github.com/golangci/golangci-lint/pkg/config"
128
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
13-
"github.com/golangci/golangci-lint/pkg/lint/linter"
14-
"github.com/golangci/golangci-lint/pkg/result"
159
)
1610

1711
const nakedretName = "nakedret"
1812

19-
//nolint:dupl
2013
func NewNakedret(settings *config.NakedretSettings) *goanalysis.Linter {
21-
var mu sync.Mutex
22-
var resIssues []goanalysis.Issue
23-
24-
analyzer := &analysis.Analyzer{
25-
Name: nakedretName,
26-
Doc: goanalysis.TheOnlyanalyzerDoc,
27-
Run: func(pass *analysis.Pass) (any, error) {
28-
issues := runNakedRet(pass, settings)
29-
30-
if len(issues) == 0 {
31-
return nil, nil
32-
}
33-
34-
mu.Lock()
35-
resIssues = append(resIssues, issues...)
36-
mu.Unlock()
37-
38-
return nil, nil
39-
},
14+
var maxLines int
15+
if settings != nil {
16+
maxLines = settings.MaxFuncLines
4017
}
4118

19+
analyzer := nakedret.NakedReturnAnalyzer(uint(maxLines))
20+
4221
return goanalysis.NewLinter(
4322
nakedretName,
4423
"Finds naked returns in functions greater than a specified function length",
4524
[]*analysis.Analyzer{analyzer},
4625
nil,
47-
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
48-
return resIssues
49-
}).WithLoadMode(goanalysis.LoadModeSyntax)
50-
}
51-
52-
func runNakedRet(pass *analysis.Pass, settings *config.NakedretSettings) []goanalysis.Issue {
53-
var issues []goanalysis.Issue
54-
55-
for _, file := range pass.Files {
56-
v := nakedretVisitor{
57-
maxLength: settings.MaxFuncLines,
58-
f: pass.Fset,
59-
}
60-
61-
ast.Walk(&v, file)
62-
63-
for i := range v.issues {
64-
issues = append(issues, goanalysis.NewIssue(&v.issues[i], pass))
65-
}
66-
}
67-
68-
return issues
69-
}
70-
71-
type nakedretVisitor struct {
72-
maxLength int
73-
f *token.FileSet
74-
issues []result.Issue
75-
}
76-
77-
func (v *nakedretVisitor) processFuncDecl(funcDecl *ast.FuncDecl) {
78-
file := v.f.File(funcDecl.Pos())
79-
functionLineLength := file.Position(funcDecl.End()).Line - file.Position(funcDecl.Pos()).Line
80-
81-
// Scan the body for usage of the named returns
82-
for _, stmt := range funcDecl.Body.List {
83-
s, ok := stmt.(*ast.ReturnStmt)
84-
if !ok {
85-
continue
86-
}
87-
88-
if len(s.Results) != 0 {
89-
continue
90-
}
91-
92-
file := v.f.File(s.Pos())
93-
if file == nil || functionLineLength <= v.maxLength {
94-
continue
95-
}
96-
if funcDecl.Name == nil {
97-
continue
98-
}
99-
100-
v.issues = append(v.issues, result.Issue{
101-
FromLinter: nakedretName,
102-
Text: fmt.Sprintf("naked return in func `%s` with %d lines of code",
103-
funcDecl.Name.Name, functionLineLength),
104-
Pos: v.f.Position(s.Pos()),
105-
})
106-
}
107-
}
108-
109-
func (v *nakedretVisitor) Visit(node ast.Node) ast.Visitor {
110-
funcDecl, ok := node.(*ast.FuncDecl)
111-
if !ok {
112-
return v
113-
}
114-
115-
var namedReturns []*ast.Ident
116-
117-
// We've found a function
118-
if funcDecl.Type != nil && funcDecl.Type.Results != nil {
119-
for _, field := range funcDecl.Type.Results.List {
120-
for _, ident := range field.Names {
121-
if ident != nil {
122-
namedReturns = append(namedReturns, ident)
123-
}
124-
}
125-
}
126-
}
127-
128-
if len(namedReturns) == 0 || funcDecl.Body == nil {
129-
return v
130-
}
131-
132-
v.processFuncDecl(funcDecl)
133-
return v
26+
).WithLoadMode(goanalysis.LoadModeSyntax)
13427
}

test/testdata/nakedret.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
//golangcitest:args -Enakedret
22
package testdata
33

4+
import "fmt"
5+
46
func NakedretIssue() (a int, b string) {
57
if a > 0 {
6-
return
8+
return // want "naked return in func `NakedretIssue` with 33 lines of code"
79
}
810

11+
fmt.Println("nakedret")
12+
913
if b == "" {
1014
return 0, "0"
1115
}
@@ -30,8 +34,8 @@ func NakedretIssue() (a int, b string) {
3034
// ...
3135
// ...
3236

33-
// len of this function is 31
34-
return // want "naked return in func `NakedretIssue` with 31 lines of code"
37+
// len of this function is 33
38+
return // want "naked return in func `NakedretIssue` with 33 lines of code"
3539
}
3640

3741
func NoNakedretIssue() (a int, b string) {

0 commit comments

Comments
 (0)