Isaev Denis 95ec0cf21e
dramatically reduce memory usage ()
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: 
2019-09-30 16:19:41 +03:00

56 lines
1.5 KiB
Go

package golinters // nolint:dupl
import (
"fmt"
"sync"
structcheckAPI "github.com/golangci/check/cmd/structcheck"
"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 NewStructcheck() *goanalysis.Linter {
const linterName = "structcheck"
var mu sync.Mutex
var res []result.Issue
analyzer := &analysis.Analyzer{
Name: goanalysis.TheOnlyAnalyzerName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
linterName,
"Finds unused struct fields",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
checkExported := lintCtx.Settings().Structcheck.CheckExportedFields
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
prog := goanalysis.MakeFakeLoaderProgram(pass)
structcheckIssues := structcheckAPI.Run(prog, checkExported)
if len(structcheckIssues) == 0 {
return nil, nil
}
issues := make([]result.Issue, 0, len(structcheckIssues))
for _, i := range structcheckIssues {
issues = append(issues, result.Issue{
Pos: i.Pos,
Text: fmt.Sprintf("%s is unused", formatCode(i.FieldName, lintCtx.Cfg)),
FromLinter: linterName,
})
}
mu.Lock()
res = append(res, issues...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []result.Issue {
return res
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
}