| 1 |
package sa5000 |
| 2 |
|
| 3 |
import ( |
| 4 |
"honnef.co/go/tools/analysis/lint" |
| 5 |
"honnef.co/go/tools/analysis/report" |
| 6 |
"honnef.co/go/tools/go/ir" |
| 7 |
"honnef.co/go/tools/go/ir/irutil" |
| 8 |
"honnef.co/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.RawDocumentation{ |
| 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) (any, 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 |
} |