@@ -8,51 +8,65 @@ import (
8
8
9
9
"golang.org/x/tools/go/analysis"
10
10
11
+ "github.com/golangci/golangci-lint/pkg/config"
11
12
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
12
13
"github.com/golangci/golangci-lint/pkg/lint/linter"
13
14
"github.com/golangci/golangci-lint/pkg/result"
14
15
)
15
16
16
- const dogsledLinterName = "dogsled"
17
+ const dogsledName = "dogsled"
17
18
18
- func NewDogsled () * goanalysis.Linter {
19
+ //nolint:dupl
20
+ func NewDogsled (settings * config.DogsledSettings ) * goanalysis.Linter {
19
21
var mu sync.Mutex
20
22
var resIssues []goanalysis.Issue
21
23
22
24
analyzer := & analysis.Analyzer {
23
- Name : dogsledLinterName ,
25
+ Name : dogsledName ,
24
26
Doc : goanalysis .TheOnlyanalyzerDoc ,
25
- }
26
- return goanalysis .NewLinter (
27
- dogsledLinterName ,
28
- "Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())" ,
29
- []* analysis.Analyzer {analyzer },
30
- nil ,
31
- ).WithContextSetter (func (lintCtx * linter.Context ) {
32
- analyzer .Run = func (pass * analysis.Pass ) (interface {}, error ) {
33
- var pkgIssues []goanalysis.Issue
34
- for _ , f := range pass .Files {
35
- v := returnsVisitor {
36
- maxBlanks : lintCtx .Settings ().Dogsled .MaxBlankIdentifiers ,
37
- f : pass .Fset ,
38
- }
39
- ast .Walk (& v , f )
40
- for i := range v .issues {
41
- pkgIssues = append (pkgIssues , goanalysis .NewIssue (& v .issues [i ], pass ))
42
- }
27
+ Run : func (pass * analysis.Pass ) (interface {}, error ) {
28
+ issues := runDogsled (pass , settings )
29
+
30
+ if len (issues ) == 0 {
31
+ return nil , nil
43
32
}
44
33
45
34
mu .Lock ()
46
- resIssues = append (resIssues , pkgIssues ... )
35
+ resIssues = append (resIssues , issues ... )
47
36
mu .Unlock ()
48
37
49
38
return nil , nil
50
- }
51
- }).WithIssuesReporter (func (* linter.Context ) []goanalysis.Issue {
39
+ },
40
+ }
41
+
42
+ return goanalysis .NewLinter (
43
+ dogsledName ,
44
+ "Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())" ,
45
+ []* analysis.Analyzer {analyzer },
46
+ nil ,
47
+ ).WithIssuesReporter (func (* linter.Context ) []goanalysis.Issue {
52
48
return resIssues
53
49
}).WithLoadMode (goanalysis .LoadModeSyntax )
54
50
}
55
51
52
+ func runDogsled (pass * analysis.Pass , settings * config.DogsledSettings ) []goanalysis.Issue {
53
+ var reports []goanalysis.Issue
54
+ for _ , f := range pass .Files {
55
+ v := & returnsVisitor {
56
+ maxBlanks : settings .MaxBlankIdentifiers ,
57
+ f : pass .Fset ,
58
+ }
59
+
60
+ ast .Walk (v , f )
61
+
62
+ for i := range v .issues {
63
+ reports = append (reports , goanalysis .NewIssue (& v .issues [i ], pass ))
64
+ }
65
+ }
66
+
67
+ return reports
68
+ }
69
+
56
70
type returnsVisitor struct {
57
71
f * token.FileSet
58
72
maxBlanks int
@@ -87,7 +101,7 @@ func (v *returnsVisitor) Visit(node ast.Node) ast.Visitor {
87
101
88
102
if numBlank > v .maxBlanks {
89
103
v .issues = append (v .issues , result.Issue {
90
- FromLinter : dogsledLinterName ,
104
+ FromLinter : dogsledName ,
91
105
Text : fmt .Sprintf ("declaration has %v blank identifiers" , numBlank ),
92
106
Pos : v .f .Position (assgnStmt .Pos ()),
93
107
})
0 commit comments