Skip to content

Commit

Permalink
Upgrading to go-critic v0.5.0 (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwankgb committed Jul 4, 2020
1 parent 01b566a commit 306cbb0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
3 changes: 1 addition & 2 deletions go.mod
Expand Up @@ -8,8 +8,7 @@ require (
github.com/bombsimon/wsl/v3 v3.1.0
github.com/denis-tingajkin/go-header v0.3.1
github.com/fatih/color v1.9.0
github.com/go-critic/go-critic v0.4.3
github.com/go-lintpack/lintpack v0.5.2
github.com/go-critic/go-critic v0.5.0
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
github.com/gofrs/flock v0.7.1
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Expand Up @@ -60,8 +60,8 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-critic/go-critic v0.4.3 h1:sGEEdiuvLV0OC7/yC6MnK3K6LCPBplspK45B0XVdFAc=
github.com/go-critic/go-critic v0.4.3/go.mod h1:j4O3D4RoIwRqlZw5jJpx0BNfXWWbpcJoKu5cYSe4YmQ=
github.com/go-critic/go-critic v0.5.0 h1:Ic2p5UCl5fX/2WX2w8nroPpPhxRNsNTMlJzsu/uqwnM=
github.com/go-critic/go-critic v0.5.0/go.mod h1:4jeRh3ZAVnRYhuWdOEvwzVqLUpxMSoAT0xZ74JsTPlo=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-lintpack/lintpack v0.5.2 h1:DI5mA3+eKdWeJ40nU4d6Wc26qmdG8RCi/btYq0TuRN0=
Expand Down Expand Up @@ -95,6 +95,8 @@ github.com/go-toolsmith/strparse v1.0.0 h1:Vcw78DnpCAKlM20kSbAyO4mPfJn/lyYA4BJUD
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
github.com/go-toolsmith/typep v1.0.0 h1:zKymWyA1TRYvqYrYDrfEMZULyrhcnGY3x7LDKU2XQaA=
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
github.com/go-toolsmith/typep v1.0.2 h1:8xdsa1+FSIH/RhEkgnD1j2CJOy5mNllW1Q9tRiYwvlk=
github.com/go-toolsmith/typep v1.0.2/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b h1:khEcpUM4yFcxg4/FHQWkvVRmgijNXRfzkIDHh23ggEo=
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b/go.mod h1:aUCEOzzezBEjDBbFBoSiya/gduyIiWYRP6CnSFIV8AM=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
Expand Down Expand Up @@ -312,6 +314,8 @@ github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c h1:JoUA0uz
github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:5STLWrekHfjyYwxBRVRXNOSewLJ3PWfDJd1VyTS21fI=
github.com/quasilyte/go-ruleguard v0.1.2-0.20200318202121-b00d7a75d3d8 h1:DvnesvLtRPQOvaUbfXfh0tpMHg29by0H7F2U+QIkSu8=
github.com/quasilyte/go-ruleguard v0.1.2-0.20200318202121-b00d7a75d3d8/go.mod h1:CGFX09Ci3pq9QZdj86B+VGIdNj4VyCo2iPOGS9esB/k=
github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 h1:L8QM9bvf68pVdQ3bCFZMDmnt9yqcMBro1pC7F+IPYMY=
github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95/go.mod h1:rlzQ04UMyJXu/aOvhd8qT+hvDrFpiwqp8MRXDY9szc0=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down
16 changes: 5 additions & 11 deletions pkg/config/config_gocritic.go
Expand Up @@ -5,7 +5,7 @@ import (
"sort"
"strings"

"github.com/go-lintpack/lintpack"
"github.com/go-critic/go-critic/framework/linter"
"github.com/pkg/errors"

_ "github.com/go-critic/go-critic/checkers" // this import register checkers
Expand All @@ -18,9 +18,9 @@ const gocriticDebugKey = "gocritic"
var (
gocriticDebugf = logutils.Debug(gocriticDebugKey)
isGocriticDebug = logutils.HaveDebugTag(gocriticDebugKey)
allGocriticCheckers = lintpack.GetCheckersInfo()
allGocriticCheckerMap = func() map[string]*lintpack.CheckerInfo {
checkInfoMap := make(map[string]*lintpack.CheckerInfo)
allGocriticCheckers = linter.GetCheckersInfo()
allGocriticCheckerMap = func() map[string]*linter.CheckerInfo {
checkInfoMap := make(map[string]*linter.CheckerInfo)
for _, checkInfo := range allGocriticCheckers {
checkInfoMap[checkInfo.Name] = checkInfo
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func getAllCheckerNames() map[string]bool {
return allCheckerNames
}

func isEnabledByDefaultGocriticCheck(info *lintpack.CheckerInfo) bool {
func isEnabledByDefaultGocriticCheck(info *linter.CheckerInfo) bool {
return !info.HasTag("experimental") &&
!info.HasTag("opinionated") &&
!info.HasTag("performance")
Expand All @@ -290,9 +290,6 @@ func isEnabledByDefaultGocriticCheck(info *lintpack.CheckerInfo) bool {
func getDefaultEnabledGocriticCheckersNames() []string {
var enabled []string
for _, info := range allGocriticCheckers {
// get in sync with lintpack behavior in bindDefaultEnabledList
// in https://github.com/go-lintpack/lintpack/blob/master/linter/lintmain/internal/check/check.go#L317

enable := isEnabledByDefaultGocriticCheck(info)
if enable {
enabled = append(enabled, info.Name)
Expand All @@ -305,9 +302,6 @@ func getDefaultEnabledGocriticCheckersNames() []string {
func getDefaultDisabledGocriticCheckersNames() []string {
var disabled []string
for _, info := range allGocriticCheckers {
// get in sync with lintpack behavior in bindDefaultEnabledList
// in https://github.com/go-lintpack/lintpack/blob/master/linter/lintmain/internal/check/check.go#L317

enable := isEnabledByDefaultGocriticCheck(info)
if !enable {
disabled = append(disabled, info.Name)
Expand Down
34 changes: 17 additions & 17 deletions pkg/golinters/gocritic.go
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"sync"

"github.com/go-lintpack/lintpack"
gocriticlinter "github.com/go-critic/go-critic/framework/linter"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
Expand Down Expand Up @@ -38,15 +38,15 @@ func NewGocritic() *goanalysis.Linter {
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
lintpackCtx := lintpack.NewContext(pass.Fset, sizes)
enabledCheckers, err := buildEnabledCheckers(lintCtx, lintpackCtx)
linterCtx := gocriticlinter.NewContext(pass.Fset, sizes)
enabledCheckers, err := buildEnabledCheckers(lintCtx, linterCtx)
if err != nil {
return nil, err
}

lintpackCtx.SetPackageInfo(pass.TypesInfo, pass.Pkg)
linterCtx.SetPackageInfo(pass.TypesInfo, pass.Pkg)
var res []goanalysis.Issue
pkgIssues := runGocriticOnPackage(lintpackCtx, enabledCheckers, pass.Files)
pkgIssues := runGocriticOnPackage(linterCtx, enabledCheckers, pass.Files)
for i := range pkgIssues {
res = append(res, goanalysis.NewIssue(&pkgIssues[i], pass))
}
Expand All @@ -65,17 +65,17 @@ func NewGocritic() *goanalysis.Linter {
}).WithLoadMode(goanalysis.LoadModeTypesInfo)
}

func normalizeCheckerInfoParams(info *lintpack.CheckerInfo) lintpack.CheckerParams {
func normalizeCheckerInfoParams(info *gocriticlinter.CheckerInfo) gocriticlinter.CheckerParams {
// lowercase info param keys here because golangci-lint's config parser lowercases all strings
ret := lintpack.CheckerParams{}
ret := gocriticlinter.CheckerParams{}
for k, v := range info.Params {
ret[strings.ToLower(k)] = v
}

return ret
}

func configureCheckerInfo(info *lintpack.CheckerInfo, allParams map[string]config.GocriticCheckSettings) error {
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
Expand Down Expand Up @@ -108,12 +108,12 @@ func configureCheckerInfo(info *lintpack.CheckerInfo, allParams map[string]confi
return nil
}

func buildEnabledCheckers(lintCtx *linter.Context, lintpackCtx *lintpack.Context) ([]*lintpack.Checker, error) {
func buildEnabledCheckers(lintCtx *linter.Context, linterCtx *gocriticlinter.Context) ([]*gocriticlinter.Checker, error) {
s := lintCtx.Settings().Gocritic
allParams := s.GetLowercasedParams()

var enabledCheckers []*lintpack.Checker
for _, info := range lintpack.GetCheckersInfo() {
var enabledCheckers []*gocriticlinter.Checker
for _, info := range gocriticlinter.GetCheckersInfo() {
if !s.IsCheckEnabled(info.Name) {
continue
}
Expand All @@ -122,27 +122,27 @@ func buildEnabledCheckers(lintCtx *linter.Context, lintpackCtx *lintpack.Context
return nil, err
}

c := lintpack.NewChecker(lintpackCtx, info)
c := gocriticlinter.NewChecker(linterCtx, info)
enabledCheckers = append(enabledCheckers, c)
}

return enabledCheckers, nil
}

func runGocriticOnPackage(lintpackCtx *lintpack.Context, checkers []*lintpack.Checker,
func runGocriticOnPackage(linterCtx *gocriticlinter.Context, checkers []*gocriticlinter.Checker,
files []*ast.File) []result.Issue {
var res []result.Issue
for _, f := range files {
filename := filepath.Base(lintpackCtx.FileSet.Position(f.Pos()).Filename)
lintpackCtx.SetFileInfo(filename, f)
filename := filepath.Base(linterCtx.FileSet.Position(f.Pos()).Filename)
linterCtx.SetFileInfo(filename, f)

issues := runGocriticOnFile(lintpackCtx, f, checkers)
issues := runGocriticOnFile(linterCtx, f, checkers)
res = append(res, issues...)
}
return res
}

func runGocriticOnFile(ctx *lintpack.Context, f *ast.File, checkers []*lintpack.Checker) []result.Issue {
func runGocriticOnFile(ctx *gocriticlinter.Context, f *ast.File, checkers []*gocriticlinter.Checker) []result.Issue {
var res []result.Issue

for _, c := range checkers {
Expand Down
2 changes: 1 addition & 1 deletion test/errchk.go
Expand Up @@ -156,7 +156,7 @@ var (
errRx = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`)
errAutoRx = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`)
errQuotesRx = regexp.MustCompile(`"([^"]*)"`)
lineRx = regexp.MustCompile(`LINE(([+-])([0-9]+))?`)
lineRx = regexp.MustCompile(`LINE(([+-])(\d+))?`)
)

// wantedErrors parses expected errors from comments in a file.
Expand Down

0 comments on commit 306cbb0

Please sign in to comment.