Skip to content

Commit 0d670d6

Browse files
committed
dev: reuse created linter
1 parent dc28699 commit 0d670d6

File tree

3 files changed

+9
-79
lines changed

3 files changed

+9
-79
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ require (
8686
github.com/pelletier/go-toml/v2 v2.2.2
8787
github.com/polyfloyd/go-errorlint v1.6.0
8888
github.com/quasilyte/go-ruleguard/dsl v0.3.22
89+
github.com/rnben/go-break-select-in-for v0.0.1
8990
github.com/ryancurrah/gomodguard v1.3.3
9091
github.com/ryanrolds/sqlclosecheck v0.5.1
9192
github.com/sanposhiho/wastedassign/v2 v2.0.7

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,19 @@
11
package gobreakselectinfor
22

33
import (
4-
"go/ast"
5-
"go/token"
6-
"sync"
7-
4+
"github.com/rnben/go-break-select-in-for/pkg/analyzer"
85
"golang.org/x/tools/go/analysis"
96

107
"github.com/golangci/golangci-lint/pkg/goanalysis"
11-
"github.com/golangci/golangci-lint/pkg/lint/linter"
12-
"github.com/golangci/golangci-lint/pkg/result"
138
)
149

15-
const linterName = "gobreakselectinfor"
16-
1710
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
4112

4213
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},
4617
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)
9219
}

0 commit comments

Comments
 (0)