/
maligned.go
58 lines (51 loc) · 1.63 KB
/
maligned.go
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
47
48
49
50
51
52
53
54
55
56
57
58
package golinters
import (
"fmt"
"sync"
malignedAPI "github.com/golangci/maligned"
"golang.org/x/tools/go/analysis"
"github.com/hitzhangjie/go-readability/pkg/golinters/goanalysis"
"github.com/hitzhangjie/go-readability/pkg/lint/linter"
"github.com/hitzhangjie/go-readability/pkg/result"
)
func NewMaligned() *goanalysis.Linter {
const linterName = "maligned"
var mu sync.Mutex
var res []goanalysis.Issue
analyzer := &analysis.Analyzer{
Name: linterName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
linterName,
"Tool to detect Go structs that would take less memory if their fields were sorted",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
prog := goanalysis.MakeFakeLoaderProgram(pass)
malignedIssues := malignedAPI.Run(prog)
if len(malignedIssues) == 0 {
return nil, nil
}
issues := make([]goanalysis.Issue, 0, len(malignedIssues))
for _, i := range malignedIssues {
text := fmt.Sprintf("struct of size %d bytes could be of size %d bytes", i.OldSize, i.NewSize)
if lintCtx.Settings().Maligned.SuggestNewOrder {
text += fmt.Sprintf(":\n%s", formatCodeBlock(i.NewStructDef, lintCtx.Cfg))
}
issues = append(issues, goanalysis.NewIssue(&result.Issue{
Pos: i.Pos,
Text: text,
FromLinter: linterName,
}, pass))
}
mu.Lock()
res = append(res, issues...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return res
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
}