golangci-lint/pkg/golinters/unconvert.go
Isaev Denis 95ec0cf21e
dramatically reduce memory usage (#758)
Run all linters per package. It allows unloading package data when it's
processed. It dramatically reduces memory (and CPU because of GC) usage.

Relates: #337
2019-09-30 16:19:41 +03:00

54 lines
1.3 KiB
Go

package golinters
import (
"sync"
unconvertAPI "github.com/golangci/unconvert"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)
func NewUnconvert() *goanalysis.Linter {
const linterName = "unconvert"
var mu sync.Mutex
var res []result.Issue
analyzer := &analysis.Analyzer{
Name: goanalysis.TheOnlyAnalyzerName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
linterName,
"Remove unnecessary type conversions",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
prog := goanalysis.MakeFakeLoaderProgram(pass)
positions := unconvertAPI.Run(prog)
if len(positions) == 0 {
return nil, nil
}
issues := make([]result.Issue, 0, len(positions))
for _, pos := range positions {
issues = append(issues, result.Issue{
Pos: pos,
Text: "unnecessary conversion",
FromLinter: linterName,
})
}
mu.Lock()
res = append(res, issues...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []result.Issue {
return res
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
}