Skip to content

Commit c7e3c0f

Browse files
committed
chore: migrate prealloc
1 parent 600e10e commit c7e3c0f

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

pkg/golinters/prealloc/prealloc.go

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,23 @@ package prealloc
22

33
import (
44
"fmt"
5-
"sync"
65

76
"github.com/alexkohler/prealloc/pkg"
87
"golang.org/x/tools/go/analysis"
98

109
"github.com/golangci/golangci-lint/pkg/config"
1110
"github.com/golangci/golangci-lint/pkg/goanalysis"
1211
"github.com/golangci/golangci-lint/pkg/golinters/internal"
13-
"github.com/golangci/golangci-lint/pkg/lint/linter"
14-
"github.com/golangci/golangci-lint/pkg/result"
1512
)
1613

1714
const linterName = "prealloc"
1815

1916
func New(settings *config.PreallocSettings) *goanalysis.Linter {
20-
var mu sync.Mutex
21-
var resIssues []goanalysis.Issue
22-
2317
analyzer := &analysis.Analyzer{
2418
Name: linterName,
2519
Doc: goanalysis.TheOnlyanalyzerDoc,
2620
Run: func(pass *analysis.Pass) (any, error) {
27-
issues := runPreAlloc(pass, settings)
28-
29-
if len(issues) == 0 {
30-
return nil, nil
31-
}
32-
33-
mu.Lock()
34-
resIssues = append(resIssues, issues...)
35-
mu.Unlock()
21+
runPreAlloc(pass, settings)
3622

3723
return nil, nil
3824
},
@@ -43,23 +29,16 @@ func New(settings *config.PreallocSettings) *goanalysis.Linter {
4329
"Finds slice declarations that could potentially be pre-allocated",
4430
[]*analysis.Analyzer{analyzer},
4531
nil,
46-
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
47-
return resIssues
48-
}).WithLoadMode(goanalysis.LoadModeSyntax)
32+
).WithLoadMode(goanalysis.LoadModeSyntax)
4933
}
5034

51-
func runPreAlloc(pass *analysis.Pass, settings *config.PreallocSettings) []goanalysis.Issue {
52-
var issues []goanalysis.Issue
53-
35+
func runPreAlloc(pass *analysis.Pass, settings *config.PreallocSettings) {
5436
hints := pkg.Check(pass.Files, settings.Simple, settings.RangeLoops, settings.ForLoops)
5537

5638
for _, hint := range hints {
57-
issues = append(issues, goanalysis.NewIssue(&result.Issue{
58-
Pos: pass.Fset.Position(hint.Pos),
59-
Text: fmt.Sprintf("Consider pre-allocating %s", internal.FormatCode(hint.DeclaredSliceName, nil)),
60-
FromLinter: linterName,
61-
}, pass))
39+
pass.Report(analysis.Diagnostic{
40+
Pos: hint.Pos,
41+
Message: fmt.Sprintf("Consider pre-allocating %s", internal.FormatCode(hint.DeclaredSliceName, nil)),
42+
})
6243
}
63-
64-
return issues
6544
}

0 commit comments

Comments
 (0)