@@ -2,37 +2,23 @@ package prealloc
2
2
3
3
import (
4
4
"fmt"
5
- "sync"
6
5
7
6
"github.com/alexkohler/prealloc/pkg"
8
7
"golang.org/x/tools/go/analysis"
9
8
10
9
"github.com/golangci/golangci-lint/pkg/config"
11
10
"github.com/golangci/golangci-lint/pkg/goanalysis"
12
11
"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"
15
12
)
16
13
17
14
const linterName = "prealloc"
18
15
19
16
func New (settings * config.PreallocSettings ) * goanalysis.Linter {
20
- var mu sync.Mutex
21
- var resIssues []goanalysis.Issue
22
-
23
17
analyzer := & analysis.Analyzer {
24
18
Name : linterName ,
25
19
Doc : goanalysis .TheOnlyanalyzerDoc ,
26
20
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 )
36
22
37
23
return nil , nil
38
24
},
@@ -43,23 +29,16 @@ func New(settings *config.PreallocSettings) *goanalysis.Linter {
43
29
"Finds slice declarations that could potentially be pre-allocated" ,
44
30
[]* analysis.Analyzer {analyzer },
45
31
nil ,
46
- ).WithIssuesReporter (func (* linter.Context ) []goanalysis.Issue {
47
- return resIssues
48
- }).WithLoadMode (goanalysis .LoadModeSyntax )
32
+ ).WithLoadMode (goanalysis .LoadModeSyntax )
49
33
}
50
34
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 ) {
54
36
hints := pkg .Check (pass .Files , settings .Simple , settings .RangeLoops , settings .ForLoops )
55
37
56
38
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
+ })
62
43
}
63
-
64
- return issues
65
44
}
0 commit comments