Denis Isaev 0421bac259 Fix #237, fix #178: use go/packages
Use go/packages instead of x/tools/loader: it allows to work
with go modules and speedups loading of packages with the help
of build cache.

A lot of linters became "fast": they are enabled by --fast now and
work in 1-2 seconds. Only unparam, interfacer and megacheck
are "slow" linters now.

Average project is analyzed 20-40% faster than before if all linters are
enabled! If we enable all linters except unparam, interfacer and
megacheck analysis is 10-20x faster!
2018-10-28 17:55:15 +03:00

70 lines
1.6 KiB
Go

package golinters
import (
"context"
"go/ast"
"go/token"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
govetAPI "github.com/golangci/govet"
)
type Govet struct{}
func (Govet) Name() string {
return "govet"
}
func (Govet) Desc() string {
return "Vet examines Go source code and reports suspicious constructs, " +
"such as Printf calls whose arguments do not align with the format string"
}
func (g Govet) Run(_ context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
var govetIssues []govetAPI.Issue
var err error
govetIssues, err = g.runImpl(lintCtx)
if err != nil {
return nil, err
}
if len(govetIssues) == 0 {
return nil, nil
}
res := make([]result.Issue, 0, len(govetIssues))
for _, i := range govetIssues {
res = append(res, result.Issue{
Pos: i.Pos,
Text: markIdentifiers(i.Message),
FromLinter: g.Name(),
})
}
return res, nil
}
func (g Govet) runImpl(lintCtx *linter.Context) ([]govetAPI.Issue, error) {
// TODO: check .S asm files: govet can do it if pass dirs
var govetIssues []govetAPI.Issue
for _, pkg := range lintCtx.Program.InitialPackages() {
if len(pkg.Files) == 0 {
continue
}
issues, err := govetAPI.Analyze(pkg.Files, lintCtx.Program.Fset, pkg,
lintCtx.Settings().Govet.CheckShadowing, getPath)
if err != nil {
return nil, err
}
govetIssues = append(govetIssues, issues...)
}
return govetIssues, nil
}
func getPath(f *ast.File, fset *token.FileSet) (string, error) {
return fsutils.ShortestRelPath(fset.Position(f.Pos()).Filename, "")
}