golangci-lint/pkg/golinters/gocritic.go

168 lines
4.5 KiB
Go
Raw Normal View History

2018-11-05 18:20:28 +03:00
package golinters
import (
"fmt"
"go/ast"
"go/types"
"path/filepath"
"runtime"
"sort"
"strings"
2018-11-05 18:20:28 +03:00
"sync"
2020-07-04 22:07:26 +02:00
gocriticlinter "github.com/go-critic/go-critic/framework/linter"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
2019-09-27 20:58:49 -04:00
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
2018-11-05 18:20:28 +03:00
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)
const gocriticName = "gocritic"
2018-11-05 18:20:28 +03:00
func NewGocritic() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue
sizes := types.SizesFor("gc", runtime.GOARCH)
analyzer := &analysis.Analyzer{
Name: gocriticName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
gocriticName,
`Provides many diagnostics that check for bugs, performance and style issues.
Extensible without recompilation through dynamic rules.
Dynamic rules are written declaratively with AST patterns, filters, report message and optional suggestion.`,
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
2020-07-04 22:07:26 +02:00
linterCtx := gocriticlinter.NewContext(pass.Fset, sizes)
enabledCheckers, err := buildEnabledCheckers(lintCtx, linterCtx)
if err != nil {
return nil, err
}
2018-11-05 18:20:28 +03:00
2020-07-04 22:07:26 +02:00
linterCtx.SetPackageInfo(pass.TypesInfo, pass.Pkg)
var res []goanalysis.Issue
2020-07-04 22:07:26 +02:00
pkgIssues := runGocriticOnPackage(linterCtx, enabledCheckers, pass.Files)
for i := range pkgIssues {
res = append(res, goanalysis.NewIssue(&pkgIssues[i], pass))
}
if len(res) == 0 {
return nil, nil
}
mu.Lock()
resIssues = append(resIssues, res...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
2018-11-05 18:20:28 +03:00
}
2020-07-04 22:07:26 +02:00
func normalizeCheckerInfoParams(info *gocriticlinter.CheckerInfo) gocriticlinter.CheckerParams {
// lowercase info param keys here because golangci-lint's config parser lowercases all strings
2020-07-04 22:07:26 +02:00
ret := gocriticlinter.CheckerParams{}
for k, v := range info.Params {
ret[strings.ToLower(k)] = v
}
return ret
}
2020-07-04 22:07:26 +02:00
func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string]config.GocriticCheckSettings) error {
params := allParams[strings.ToLower(info.Name)]
if params == nil { // no config for this checker
return nil
}
infoParams := normalizeCheckerInfoParams(info)
for k, p := range params {
v, ok := infoParams[k]
if ok {
v.Value = p
continue
}
// param `k` isn't supported
if len(info.Params) == 0 {
return fmt.Errorf("checker %s config param %s doesn't exist: checker doesn't have params",
info.Name, k)
}
var supportedKeys []string
for sk := range info.Params {
supportedKeys = append(supportedKeys, sk)
}
sort.Strings(supportedKeys)
return fmt.Errorf("checker %s config param %s doesn't exist, all existing: %s",
info.Name, k, supportedKeys)
}
return nil
}
2018-11-05 18:20:28 +03:00
2020-07-04 22:07:26 +02:00
func buildEnabledCheckers(lintCtx *linter.Context, linterCtx *gocriticlinter.Context) ([]*gocriticlinter.Checker, error) {
2018-11-05 18:20:28 +03:00
s := lintCtx.Settings().Gocritic
allParams := s.GetLowercasedParams()
2020-07-04 22:07:26 +02:00
var enabledCheckers []*gocriticlinter.Checker
for _, info := range gocriticlinter.GetCheckersInfo() {
2018-11-05 18:20:28 +03:00
if !s.IsCheckEnabled(info.Name) {
continue
}
if err := configureCheckerInfo(info, allParams); err != nil {
return nil, err
}
c, err := gocriticlinter.NewChecker(linterCtx, info)
if err != nil {
return nil, err
}
2018-11-05 18:20:28 +03:00
enabledCheckers = append(enabledCheckers, c)
}
return enabledCheckers, nil
}
2020-07-04 22:07:26 +02:00
func runGocriticOnPackage(linterCtx *gocriticlinter.Context, checkers []*gocriticlinter.Checker,
files []*ast.File) []result.Issue {
2018-11-05 18:20:28 +03:00
var res []result.Issue
for _, f := range files {
2020-07-04 22:07:26 +02:00
filename := filepath.Base(linterCtx.FileSet.Position(f.Pos()).Filename)
linterCtx.SetFileInfo(filename, f)
2018-11-05 18:20:28 +03:00
2020-07-04 22:07:26 +02:00
issues := runGocriticOnFile(linterCtx, f, checkers)
res = append(res, issues...)
2018-11-05 18:20:28 +03:00
}
return res
2018-11-05 18:20:28 +03:00
}
2020-07-04 22:07:26 +02:00
func runGocriticOnFile(ctx *gocriticlinter.Context, f *ast.File, checkers []*gocriticlinter.Checker) []result.Issue {
var res []result.Issue
2018-11-05 18:20:28 +03:00
for _, c := range checkers {
// All checkers are expected to use *lint.Context
// as read-only structure, so no copying is required.
for _, warn := range c.Check(f) {
pos := ctx.FileSet.Position(warn.Node.Pos())
res = append(res, result.Issue{
Pos: pos,
Text: fmt.Sprintf("%s: %s", c.Info.Name, warn.Text),
FromLinter: gocriticName,
})
}
2018-11-05 18:20:28 +03:00
}
return res
2018-11-05 18:20:28 +03:00
}