Skip to content

Commit 4e3255d

Browse files
committed
Update gocritic
Fix #324, relates #314 1. Update gocritic to the latest version 2. Use proper gocritic checkers repo, old repo was archived 3. Get enabled by default gocritic checks in sync with go-critic: don't enable performance, experimental and opinionated checks by default 4. Support of `enabled-tags` options for gocritic 5. Enable almost all gocritic checks for the project 6. Make rich debugging for gocritic 7. Meticulously validate gocritic checks config
1 parent 7705f82 commit 4e3255d

File tree

86 files changed

+1152
-466
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1152
-466
lines changed

.golangci.example.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,22 @@ linters-settings:
150150
range-loops: true # Report preallocation suggestions on range loops, true by default
151151
for-loops: false # Report preallocation suggestions on for loops, false by default
152152
gocritic:
153-
# which checks should be enabled; can't be combined with 'disabled-checks';
154-
# default are: [appendAssign appendCombine assignOp builtinShadow captLocal caseOrder defaultCaseOrder
155-
# dupArg dupBranchBody dupCase elseif flagDeref ifElseChain importShadow indexAlloc paramTypeCombine
156-
# rangeExprCopy rangeValCopy regexpMust singleCaseSwitch sloppyLen switchTrue typeSwitchVar typeUnparen
157-
# underef unlambda unslice dupSubExpr hugeParam];
158-
# all checks list: https://github.com/go-critic/checkers
153+
# Which checks should be enabled; can't be combined with 'disabled-checks';
154+
# See https://go-critic.github.io/overview#checks-overview
155+
# To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`
156+
# By default list of stable checks is used.
159157
enabled-checks:
160158
- rangeValCopy
161-
# which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
159+
160+
# Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
162161
disabled-checks:
163162
- regexpMust
163+
164+
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint` run to see all tags and checks.
165+
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
166+
enabled-tags:
167+
- performance
168+
164169
settings: # settings passed to gocritic
165170
captLocal: # must be valid enabled check name
166171
checkLocals: true

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ linters-settings:
2424
line-length: 140
2525
goimports:
2626
local-prefixes: github.com/golangci/golangci-lint
27+
gocritic:
28+
enabled-tags:
29+
- performance
30+
- style
31+
- experimental
32+
disabled-checks:
33+
- wrapperFunc
2734

2835
linters:
2936
enable-all: true

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ release:
2828
rm -rf dist
2929
curl -sL https://git.io/goreleaser | bash
3030

31+
update_deps:
32+
GO111MODULE=on go mod verify
33+
GO111MODULE=on go mod tidy
34+
rm -rf vendor
35+
GO111MODULE=on go mod vendor
36+
3137
.PHONY: test

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -667,17 +667,22 @@ linters-settings:
667667
range-loops: true # Report preallocation suggestions on range loops, true by default
668668
for-loops: false # Report preallocation suggestions on for loops, false by default
669669
gocritic:
670-
# which checks should be enabled; can't be combined with 'disabled-checks';
671-
# default are: [appendAssign appendCombine assignOp builtinShadow captLocal caseOrder defaultCaseOrder
672-
# dupArg dupBranchBody dupCase elseif flagDeref ifElseChain importShadow indexAlloc paramTypeCombine
673-
# rangeExprCopy rangeValCopy regexpMust singleCaseSwitch sloppyLen switchTrue typeSwitchVar typeUnparen
674-
# underef unlambda unslice dupSubExpr hugeParam];
675-
# all checks list: https://github.com/go-critic/checkers
670+
# Which checks should be enabled; can't be combined with 'disabled-checks';
671+
# See https://go-critic.github.io/overview#checks-overview
672+
# To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`
673+
# By default list of stable checks is used.
676674
enabled-checks:
677675
- rangeValCopy
678-
# which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
676+
677+
# Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
679678
disabled-checks:
680679
- regexpMust
680+
681+
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint` run to see all tags and checks.
682+
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
683+
enabled-tags:
684+
- performance
685+
681686
settings: # settings passed to gocritic
682687
captLocal: # must be valid enabled check name
683688
checkLocals: true
@@ -764,6 +769,13 @@ linters-settings:
764769
line-length: 140
765770
goimports:
766771
local-prefixes: github.com/golangci/golangci-lint
772+
gocritic:
773+
enabled-tags:
774+
- performance
775+
- style
776+
- experimental
777+
disabled-checks:
778+
- wrapperFunc
767779
768780
linters:
769781
enable-all: true

go.mod

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ require (
66
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
77
github.com/davecgh/go-spew v1.1.0 // indirect
88
github.com/fatih/color v1.6.0
9-
github.com/go-critic/checkers v0.0.0-20181204210945-97246d3b3c67
10-
github.com/go-lintpack/lintpack v0.5.1
9+
github.com/go-critic/go-critic v0.0.0-20181204210945-0af0999fabfb
10+
github.com/go-lintpack/lintpack v0.5.2
1111
github.com/go-ole/go-ole v1.2.1 // indirect
12-
github.com/go-toolsmith/astcast v0.0.0-20181028201508-b7a89ed70af1 // indirect
13-
github.com/go-toolsmith/astcopy v0.0.0-20180903214859-79b422d080c4 // indirect
1412
github.com/go-toolsmith/pkgload v0.0.0-20181120203407-5122569a890b // indirect
15-
github.com/go-toolsmith/strparse v0.0.0-20180903215201-830b6daa1241 // indirect
16-
github.com/go-toolsmith/typep v0.0.0-20181030061450-d63dc7650676 // indirect
1713
github.com/gobwas/glob v0.2.3 // indirect
1814
github.com/gogo/protobuf v1.0.0 // indirect
1915
github.com/golang/mock v1.1.1

go.sum

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ github.com/fatih/color v1.6.0 h1:66qjqZk8kalYAvDRtM1AdAJQI0tj4Wrue3Eq3B3pmFU=
1010
github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
1111
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
1212
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
13-
github.com/go-critic/checkers v0.0.0-20181204210945-97246d3b3c67 h1:AhL5n4pH/qzefJ64+0RbymXZSBsvgbBaVJQCcjFaJPw=
14-
github.com/go-critic/checkers v0.0.0-20181204210945-97246d3b3c67/go.mod h1:Cg5JCP9M6m93z6fecpRcVgD2lZf2RvPtb85ldjiShZc=
15-
github.com/go-lintpack/lintpack v0.5.1 h1:v5D/csM90cu5PANqkj1JcNZGX/mrr3Z2Wu7Q8KuFd9M=
16-
github.com/go-lintpack/lintpack v0.5.1/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM=
13+
github.com/go-critic/go-critic v0.0.0-20181204210945-0af0999fabfb h1:faOtDYqSVJsFEJAW+SwEMvh7alhYsb42fER6tt8yXfA=
14+
github.com/go-critic/go-critic v0.0.0-20181204210945-0af0999fabfb/go.mod h1:PSww+HOJZQ3TN2hi6sphNiW1PhwELxbsK8+Jy1sjML8=
15+
github.com/go-lintpack/lintpack v0.5.2 h1:DI5mA3+eKdWeJ40nU4d6Wc26qmdG8RCi/btYq0TuRN0=
16+
github.com/go-lintpack/lintpack v0.5.2/go.mod h1:NwZuYi2nUHho8XEIZ6SIxihrnPoqBTDqfpXvXAN0sXM=
1717
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
1818
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
1919
github.com/go-toolsmith/astcast v0.0.0-20181028201508-b7a89ed70af1 h1:h+1eMw+tZAlgTVclcVN0/rdPaBI/RUzG0peblT6df+Q=
@@ -147,6 +147,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
147147
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
148148
golang.org/x/tools v0.0.0-20181117154741-2ddaf7f79a09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
149149
golang.org/x/tools v0.0.0-20181201035826-d0ca3933b724/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
150+
golang.org/x/tools v0.0.0-20181205014116-22934f0fdb62/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
150151
golang.org/x/tools v0.0.0-20181220024903-92cdcd90bf52 h1:oOIe9Zzq27JsS/3ACpGF1HwWnWNflZWT/3EvM7mtcEk=
151152
golang.org/x/tools v0.0.0-20181220024903-92cdcd90bf52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
152153
gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=

pkg/config/config.go

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package config
22

33
import (
4-
"errors"
5-
"strings"
64
"time"
7-
8-
"github.com/golangci/golangci-lint/pkg/logutils"
95
)
106

117
const (
@@ -198,99 +194,6 @@ type PreallocSettings struct {
198194
ForLoops bool `mapstructure:"for-loops"`
199195
}
200196

201-
type GocriticCheckSettings map[string]interface{}
202-
203-
type GocriticSettings struct {
204-
EnabledChecks []string `mapstructure:"enabled-checks"`
205-
DisabledChecks []string `mapstructure:"disabled-checks"`
206-
SettingsPerCheck map[string]GocriticCheckSettings `mapstructure:"settings"`
207-
208-
inferredEnabledChecks map[string]bool
209-
}
210-
211-
func (s *GocriticSettings) InferEnabledChecks(log logutils.Log) {
212-
enabledChecks := s.EnabledChecks
213-
if len(enabledChecks) == 0 {
214-
if len(s.DisabledChecks) != 0 {
215-
for _, defaultCheck := range defaultGocriticEnabledChecks {
216-
if !s.isCheckDisabled(defaultCheck) {
217-
enabledChecks = append(enabledChecks, defaultCheck)
218-
}
219-
}
220-
} else {
221-
enabledChecks = defaultGocriticEnabledChecks
222-
}
223-
}
224-
225-
s.inferredEnabledChecks = map[string]bool{}
226-
for _, check := range enabledChecks {
227-
s.inferredEnabledChecks[strings.ToLower(check)] = true
228-
}
229-
log.Infof("Gocritic enabled checks: %s", enabledChecks)
230-
}
231-
232-
func (s GocriticSettings) isCheckDisabled(name string) bool {
233-
for _, disabledCheck := range s.DisabledChecks {
234-
if disabledCheck == name {
235-
return true
236-
}
237-
}
238-
239-
return false
240-
}
241-
242-
func (s GocriticSettings) Validate(log logutils.Log) error {
243-
if len(s.EnabledChecks) != 0 && len(s.DisabledChecks) != 0 {
244-
return errors.New("both enabled and disabled check aren't allowed for gocritic")
245-
}
246-
247-
for checkName := range s.SettingsPerCheck {
248-
if !s.IsCheckEnabled(checkName) {
249-
log.Warnf("Gocritic settings were provided for not enabled check %q", checkName)
250-
}
251-
}
252-
253-
return nil
254-
}
255-
256-
func (s GocriticSettings) IsCheckEnabled(name string) bool {
257-
return s.inferredEnabledChecks[strings.ToLower(name)]
258-
}
259-
260-
// Its a good idea to keep this list in sync with the gocritic stable checks list in:
261-
// https://github.com/go-critic/go-critic/blob/master/checkers/checkers_test.go#L63
262-
var defaultGocriticEnabledChecks = []string{
263-
"appendAssign",
264-
"appendCombine",
265-
"assignOp",
266-
"builtinShadow",
267-
"captLocal",
268-
"caseOrder",
269-
"defaultCaseOrder",
270-
"dupArg",
271-
"dupBranchBody",
272-
"dupCase",
273-
"elseif",
274-
"flagDeref",
275-
"ifElseChain",
276-
"importShadow",
277-
"indexAlloc",
278-
"paramTypeCombine",
279-
"rangeExprCopy",
280-
"rangeValCopy",
281-
"regexpMust",
282-
"singleCaseSwitch",
283-
"sloppyLen",
284-
"switchTrue",
285-
"typeSwitchVar",
286-
"typeUnparen",
287-
"underef",
288-
"unlambda",
289-
"unslice",
290-
"dupSubExpr",
291-
"hugeParam",
292-
}
293-
294197
var defaultLintersSettings = LintersSettings{
295198
Lll: LllSettings{
296199
LineLength: 120,

0 commit comments

Comments
 (0)