|
1 | 1 | package gobreakselectinfor
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "go/ast" |
5 |
| - "go/token" |
6 |
| - "sync" |
7 |
| - |
| 4 | + "github.com/rnben/go-break-select-in-for/pkg/analyzer" |
8 | 5 | "golang.org/x/tools/go/analysis"
|
9 | 6 |
|
10 | 7 | "github.com/golangci/golangci-lint/pkg/goanalysis"
|
11 |
| - "github.com/golangci/golangci-lint/pkg/lint/linter" |
12 |
| - "github.com/golangci/golangci-lint/pkg/result" |
13 | 8 | )
|
14 | 9 |
|
15 |
| -const linterName = "gobreakselectinfor" |
16 |
| - |
17 | 10 | func New() *goanalysis.Linter {
|
18 |
| - var mu sync.Mutex |
19 |
| - var resIssues []goanalysis.Issue |
20 |
| - |
21 |
| - analyzer := &analysis.Analyzer{ |
22 |
| - Name: linterName, |
23 |
| - Doc: goanalysis.TheOnlyanalyzerDoc, |
24 |
| - Run: func(pass *analysis.Pass) (any, error) { |
25 |
| - fileIssues := run(pass) |
26 |
| - res := make([]goanalysis.Issue, 0, len(fileIssues)) |
27 |
| - for i := range fileIssues { |
28 |
| - res = append(res, goanalysis.NewIssue(&fileIssues[i], pass)) |
29 |
| - } |
30 |
| - if len(res) == 0 { |
31 |
| - return nil, nil |
32 |
| - } |
33 |
| - |
34 |
| - mu.Lock() |
35 |
| - resIssues = append(resIssues, res...) |
36 |
| - mu.Unlock() |
37 |
| - |
38 |
| - return nil, nil |
39 |
| - }, |
40 |
| - } |
| 11 | + a := analyzer.Analyzer |
41 | 12 |
|
42 | 13 | return goanalysis.NewLinter(
|
43 |
| - linterName, |
44 |
| - "Checks that break statement inside select statement inside for loop", |
45 |
| - []*analysis.Analyzer{analyzer}, |
| 14 | + a.Name, |
| 15 | + a.Doc, |
| 16 | + []*analysis.Analyzer{a}, |
46 | 17 | nil,
|
47 |
| - ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
48 |
| - return resIssues |
49 |
| - }).WithLoadMode(goanalysis.LoadModeSyntax) |
50 |
| -} |
51 |
| - |
52 |
| -func run(pass *analysis.Pass) []result.Issue { |
53 |
| - var res []result.Issue |
54 |
| - |
55 |
| - inspect := func(node ast.Node) bool { |
56 |
| - funcDecl, ok := node.(*ast.FuncDecl) |
57 |
| - if !ok { |
58 |
| - return true |
59 |
| - } |
60 |
| - |
61 |
| - ast.Inspect(funcDecl.Body, func(stmt ast.Node) bool { |
62 |
| - if forStmt, ok := stmt.(*ast.ForStmt); ok { |
63 |
| - ast.Inspect(forStmt.Body, func(stmt ast.Node) bool { |
64 |
| - if selStmt, ok := stmt.(*ast.SelectStmt); ok { |
65 |
| - ast.Inspect(selStmt.Body, func(stmt ast.Node) bool { |
66 |
| - if brkStmt, ok := stmt.(*ast.BranchStmt); ok && brkStmt.Tok == token.BREAK { |
67 |
| - pass.Reportf(stmt.Pos(), "break statement inside select statement inside for loop") |
68 |
| - res = append(res, result.Issue{ |
69 |
| - Pos: pass.Fset.Position(stmt.Pos()), |
70 |
| - Text: "break statement inside select statement inside for loop", |
71 |
| - FromLinter: linterName, |
72 |
| - }) |
73 |
| - return true |
74 |
| - } |
75 |
| - return true |
76 |
| - }) |
77 |
| - } |
78 |
| - return true |
79 |
| - }) |
80 |
| - } |
81 |
| - return true |
82 |
| - }) |
83 |
| - |
84 |
| - return true |
85 |
| - } |
86 |
| - |
87 |
| - for _, f := range pass.Files { |
88 |
| - ast.Inspect(f, inspect) |
89 |
| - } |
90 |
| - |
91 |
| - return res |
| 18 | + ).WithLoadMode(goanalysis.LoadModeTypesInfo) |
92 | 19 | }
|
0 commit comments