github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/staticcheck/sa5000/sa5000.go (about)

     1  package sa5000
     2  
     3  import (
     4  	"github.com/amarpal/go-tools/analysis/lint"
     5  	"github.com/amarpal/go-tools/analysis/report"
     6  	"github.com/amarpal/go-tools/go/ir"
     7  	"github.com/amarpal/go-tools/go/ir/irutil"
     8  	"github.com/amarpal/go-tools/internal/passes/buildir"
     9  
    10  	"golang.org/x/tools/go/analysis"
    11  )
    12  
    13  var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
    14  	Analyzer: &analysis.Analyzer{
    15  		Name:     "SA5000",
    16  		Run:      run,
    17  		Requires: []*analysis.Analyzer{buildir.Analyzer},
    18  	},
    19  	Doc: &lint.Documentation{
    20  		Title:    `Assignment to nil map`,
    21  		Since:    "2017.1",
    22  		Severity: lint.SeverityError,
    23  		MergeIf:  lint.MergeIfAny,
    24  	},
    25  })
    26  
    27  var Analyzer = SCAnalyzer.Analyzer
    28  
    29  func run(pass *analysis.Pass) (interface{}, error) {
    30  	for _, fn := range pass.ResultOf[buildir.Analyzer].(*buildir.IR).SrcFuncs {
    31  		for _, block := range fn.Blocks {
    32  			for _, ins := range block.Instrs {
    33  				mu, ok := ins.(*ir.MapUpdate)
    34  				if !ok {
    35  					continue
    36  				}
    37  				c, ok := irutil.Flatten(mu.Map).(*ir.Const)
    38  				if !ok {
    39  					continue
    40  				}
    41  				if c.Value != nil {
    42  					continue
    43  				}
    44  				report.Report(pass, mu, "assignment to nil map")
    45  			}
    46  		}
    47  	}
    48  	return nil, nil
    49  }