-
-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy paths1006.go
More file actions
46 lines (40 loc) · 1.17 KB
/
s1006.go
File metadata and controls
46 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package s1006
import (
"go/ast"
"honnef.co/go/tools/analysis/code"
"honnef.co/go/tools/analysis/facts/generated"
"honnef.co/go/tools/analysis/lint"
"honnef.co/go/tools/analysis/report"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)
var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
Analyzer: &analysis.Analyzer{
Name: "S1006",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer},
},
Doc: &lint.Documentation{
Title: `Use \"for { ... }\" for infinite loops`,
Text: `For infinite loops, using \'for { ... }\' is the most idiomatic choice.`,
Since: "2017.1",
MergeIf: lint.MergeIfAny,
},
})
var Analyzer = SCAnalyzer.Analyzer
func run(pass *analysis.Pass) (interface{}, error) {
fn := func(node ast.Node) {
loop := node.(*ast.ForStmt)
if loop.Init != nil || loop.Post != nil {
return
}
if !code.IsBoolConst(pass, loop.Cond) || !code.BoolConst(pass, loop.Cond) {
return
}
report.Report(pass, loop, "should use for {} instead of for true {}",
report.ShortRange(),
report.FilterGenerated())
}
code.Preorder(pass, fn, (*ast.ForStmt)(nil))
return nil, nil
}