@@ -8,51 +8,65 @@ import (
88
99 "golang.org/x/tools/go/analysis"
1010
11+ "github.com/golangci/golangci-lint/pkg/config"
1112 "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1213 "github.com/golangci/golangci-lint/pkg/lint/linter"
1314 "github.com/golangci/golangci-lint/pkg/result"
1415)
1516
16- const dogsledLinterName = "dogsled"
17+ const dogsledName = "dogsled"
1718
18- func NewDogsled () * goanalysis.Linter {
19+ //nolint:dupl
20+ func NewDogsled (settings * config.DogsledSettings ) * goanalysis.Linter {
1921 var mu sync.Mutex
2022 var resIssues []goanalysis.Issue
2123
2224 analyzer := & analysis.Analyzer {
23- Name : dogsledLinterName ,
25+ Name : dogsledName ,
2426 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
4332 }
4433
4534 mu .Lock ()
46- resIssues = append (resIssues , pkgIssues ... )
35+ resIssues = append (resIssues , issues ... )
4736 mu .Unlock ()
4837
4938 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 {
5248 return resIssues
5349 }).WithLoadMode (goanalysis .LoadModeSyntax )
5450}
5551
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+
5670type returnsVisitor struct {
5771 f * token.FileSet
5872 maxBlanks int
@@ -87,7 +101,7 @@ func (v *returnsVisitor) Visit(node ast.Node) ast.Visitor {
87101
88102 if numBlank > v .maxBlanks {
89103 v .issues = append (v .issues , result.Issue {
90- FromLinter : dogsledLinterName ,
104+ FromLinter : dogsledName ,
91105 Text : fmt .Sprintf ("declaration has %v blank identifiers" , numBlank ),
92106 Pos : v .f .Position (assgnStmt .Pos ()),
93107 })
0 commit comments