github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/pkg/golinters/golint.go (about) 1 package golinters 2 3 import ( 4 "fmt" 5 "go/ast" 6 "go/token" 7 "go/types" 8 "sync" 9 10 lintAPI "github.com/golangci/lint-1" 11 "golang.org/x/tools/go/analysis" 12 13 "github.com/elek/golangci-lint/pkg/golinters/goanalysis" 14 "github.com/elek/golangci-lint/pkg/lint/linter" 15 "github.com/elek/golangci-lint/pkg/result" 16 ) 17 18 func golintProcessPkg(minConfidence float64, files []*ast.File, fset *token.FileSet, 19 typesPkg *types.Package, typesInfo *types.Info) ([]result.Issue, error) { 20 l := new(lintAPI.Linter) 21 ps, err := l.LintPkg(files, fset, typesPkg, typesInfo) 22 if err != nil { 23 return nil, fmt.Errorf("can't lint %d files: %s", len(files), err) 24 } 25 26 if len(ps) == 0 { 27 return nil, nil 28 } 29 30 issues := make([]result.Issue, 0, len(ps)) // This is worst case 31 for idx := range ps { 32 if ps[idx].Confidence >= minConfidence { 33 issues = append(issues, result.Issue{ 34 Pos: ps[idx].Position, 35 Text: ps[idx].Text, 36 FromLinter: golintName, 37 }) 38 // TODO: use p.Link and p.Category 39 } 40 } 41 42 return issues, nil 43 } 44 45 const golintName = "golint" 46 47 func NewGolint() *goanalysis.Linter { 48 var mu sync.Mutex 49 var resIssues []goanalysis.Issue 50 51 analyzer := &analysis.Analyzer{ 52 Name: golintName, 53 Doc: goanalysis.TheOnlyanalyzerDoc, 54 } 55 return goanalysis.NewLinter( 56 golintName, 57 "Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes", 58 []*analysis.Analyzer{analyzer}, 59 nil, 60 ).WithContextSetter(func(lintCtx *linter.Context) { 61 analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { 62 res, err := golintProcessPkg(lintCtx.Settings().Golint.MinConfidence, pass.Files, pass.Fset, pass.Pkg, pass.TypesInfo) 63 if err != nil || len(res) == 0 { 64 return nil, err 65 } 66 67 mu.Lock() 68 for i := range res { 69 resIssues = append(resIssues, goanalysis.NewIssue(&res[i], pass)) 70 } 71 mu.Unlock() 72 73 return nil, nil 74 } 75 }).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { 76 return resIssues 77 }).WithLoadMode(goanalysis.LoadModeTypesInfo) 78 }