prysm-pulse/tools/analyzers/maligned/analyzer.go
Radosław Kapka e1cd9143e0
Package-level comments for static analyzers (#7332)
* package-level comments
* Merge refs/heads/master into analyzer-package-comments
2020-09-24 10:25:55 +00:00

45 lines
1.1 KiB
Go

// Package maligned implements a static analyzer to ensure that Go structs take up the least possible memory.
package maligned
import (
"errors"
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
// Doc explaining the tool.
const Doc = "Tool to detect Go structs that would take less memory if their fields were sorted."
// Analyzer runs static analysis.
var Analyzer = &analysis.Analyzer{
Name: "maligned",
Doc: Doc,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("analyzer is not type *inspector.Inspector")
}
nodeFilter := []ast.Node{
(*ast.StructType)(nil),
}
inspect.Preorder(nodeFilter, func(node ast.Node) {
if s, ok := node.(*ast.StructType); ok {
if err := malign(node.Pos(), pass.TypesInfo.Types[s].Type.(*types.Struct)); err != nil {
pass.Reportf(node.Pos(), err.Error())
}
}
})
return nil, nil
}