src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

s1006.go (1190B)


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