Skip to content

Commit 8c1237b

Browse files
committed
Use the newest go vet
The newest go vet based on go/analysis
1 parent 7289a90 commit 8c1237b

Some content is hidden

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

85 files changed

+6716
-4595
lines changed

.golangci.example.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ linters-settings:
8181
govet:
8282
# report about shadowed variables
8383
check-shadowing: true
84+
85+
# settings per analyzer
86+
settings:
87+
printf: # analyzer name, run `go tool vet help` to see all analyzers
88+
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
89+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
90+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
91+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
92+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
8493
golint:
8594
# minimal confidence for issues, default is 0.8
8695
min-confidence: 0.8

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
linters-settings:
22
govet:
33
check-shadowing: true
4+
settings:
5+
printf:
6+
funcs:
7+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
8+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
9+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
10+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
411
golint:
512
min-confidence: 0
613
gocyclo:
@@ -42,6 +49,7 @@ linters:
4249
run:
4350
skip-dirs:
4451
- test/testdata_etc
52+
- pkg/golinters/goanalysis/(checker|passes)
4553

4654
issues:
4755
exclude-rules:

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
test:
22
go build -o golangci-lint ./cmd/golangci-lint
33
GL_TEST_RUN=1 ./golangci-lint run -v
4-
GL_TEST_RUN=1 ./golangci-lint run --fast --no-config -v --skip-dirs test/testdata_etc
5-
GL_TEST_RUN=1 ./golangci-lint run --no-config -v --skip-dirs test/testdata_etc
4+
GL_TEST_RUN=1 ./golangci-lint run --fast --no-config -v --skip-dirs 'test/testdata_etc,pkg/golinters/goanalysis/(checker|passes)'
5+
GL_TEST_RUN=1 ./golangci-lint run --no-config -v --skip-dirs 'test/testdata_etc,pkg/golinters/goanalysis/(checker|passes)'
66
GL_TEST_RUN=1 go test -v ./...
77

88
test_race:

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ GolangCI-Lint can be used with zero configuration. By default the following lint
185185
```bash
186186
$ golangci-lint help linters
187187
Enabled by default linters:
188-
govet (vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: true, auto-fix: false]
188+
govet (vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
189189
errcheck: Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: true, auto-fix: false]
190190
staticcheck: Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false, auto-fix: false]
191191
unused: Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
@@ -612,6 +612,15 @@ linters-settings:
612612
govet:
613613
# report about shadowed variables
614614
check-shadowing: true
615+
616+
# settings per analyzer
617+
settings:
618+
printf: # analyzer name, run `go tool vet help` to see all analyzers
619+
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
620+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
621+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
622+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
623+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
615624
golint:
616625
# minimal confidence for issues, default is 0.8
617626
min-confidence: 0.8
@@ -785,6 +794,13 @@ than the default and have more strict settings:
785794
linters-settings:
786795
govet:
787796
check-shadowing: true
797+
settings:
798+
printf:
799+
funcs:
800+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
801+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
802+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
803+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
788804
golint:
789805
min-confidence: 0
790806
gocyclo:
@@ -826,6 +842,7 @@ linters:
826842
run:
827843
skip-dirs:
828844
- test/testdata_etc
845+
- pkg/golinters/goanalysis/(checker|passes)
829846
830847
issues:
831848
exclude-rules:

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ require (
1919
github.com/golangci/gocyclo v0.0.0-20180528134321-2becd97e67ee
2020
github.com/golangci/gofmt v0.0.0-20181105071733-0b8337e80d98
2121
github.com/golangci/gosec v0.0.0-20180901114220-66fb7fc33547
22-
github.com/golangci/govet v0.0.0-20180818181408-44ddbe260190
2322
github.com/golangci/ineffassign v0.0.0-20180808204949-2ee8f2867dde
2423
github.com/golangci/lint-1 v0.0.0-20180610141402-4bf9709227d1
2524
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca
@@ -52,7 +51,7 @@ require (
5251
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect
5352
golang.org/x/net v0.0.0-20190313220215-9f648a60d977 // indirect
5453
golang.org/x/sys v0.0.0-20190312061237-fead79001313 // indirect
55-
golang.org/x/tools v0.0.0-20190314010720-1286b2016bb1
54+
golang.org/x/tools v0.0.0-20190314010720-f0bfdbff1f9c
5655
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
5756
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
5857
gopkg.in/yaml.v2 v2.2.1

go.sum

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ github.com/golangci/gofmt v0.0.0-20181105071733-0b8337e80d98 h1:ir6/L2ZOJfFrJlOT
5858
github.com/golangci/gofmt v0.0.0-20181105071733-0b8337e80d98/go.mod h1:9qCChq59u/eW8im404Q2WWTrnBUQKjpNYKMbU4M7EFU=
5959
github.com/golangci/gosec v0.0.0-20180901114220-66fb7fc33547 h1:qMomh8bv+kDazm1dSLZ9S3zZ2PJZMHL4ilfBjxFOlmI=
6060
github.com/golangci/gosec v0.0.0-20180901114220-66fb7fc33547/go.mod h1:0qUabqiIQgfmlAmulqxyiGkkyF6/tOGSnY2cnPVwrzU=
61-
github.com/golangci/govet v0.0.0-20180818181408-44ddbe260190 h1:SLIgprnxQNjBpkz55PK1vfb64/gKU/TgVi0obFw8Lec=
62-
github.com/golangci/govet v0.0.0-20180818181408-44ddbe260190/go.mod h1:pPwb+AK755h3/r73avHz5bEN6sa51/2HEZlLaV53hCo=
6361
github.com/golangci/ineffassign v0.0.0-20180808204949-2ee8f2867dde h1:qEGp3ZF1Qw6TkbWKn6GdJ12Ssu/CpJBaBcJ4hrUjrSo=
6462
github.com/golangci/ineffassign v0.0.0-20180808204949-2ee8f2867dde/go.mod h1:e5tpTHCfVze+7EpLEozzMB3eafxo2KT5veNg1k6byQU=
6563
github.com/golangci/lint-1 v0.0.0-20180610141402-4bf9709227d1 h1:PHK2kIh21Zt4IcG0bBRzQwEDVKF64LnkoSXnm8lfJUk=
@@ -146,8 +144,6 @@ github.com/spf13/viper v1.0.2 h1:Ncr3ZIuJn322w2k1qmzXDnkLAdQMlJqBa9kfAH+irso=
146144
github.com/spf13/viper v1.0.2/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM=
147145
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
148146
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
149-
golang.org/x/crypto v0.0.0-20180505025534-4ec37c66abab h1:w4c/LoOA2vE8SYwh8wEEQVRUwpph7TtcjH7AtZvOjy0=
150-
golang.org/x/crypto v0.0.0-20180505025534-4ec37c66abab/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
151147
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
152148
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a h1:YX8ljsm6wXlHZO+aRz9Exqr0evNhKRNe5K/gi+zKh4U=
153149
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -173,10 +169,8 @@ golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGm
173169
golang.org/x/tools v0.0.0-20181117154741-2ddaf7f79a09/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
174170
golang.org/x/tools v0.0.0-20181205014116-22934f0fdb62/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
175171
golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
176-
golang.org/x/tools v0.0.0-20190125232054-379209517ffe h1:ZJ3JgA0fnPnX6nSjHp3y5XWNUf3zaTbWlilINJoPFkQ=
177-
golang.org/x/tools v0.0.0-20190125232054-379209517ffe/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
178-
golang.org/x/tools v0.0.0-20190314010720-1286b2016bb1 h1:bVqQ31OV9908eHt6CNzw09jKOqi34qHsp4nsinaxiuw=
179-
golang.org/x/tools v0.0.0-20190314010720-1286b2016bb1/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
172+
golang.org/x/tools v0.0.0-20190314010720-f0bfdbff1f9c h1:nE2ID2IbO0sUUG/3vWMz0LStAvkaW9wpnFp/65bxJw8=
173+
golang.org/x/tools v0.0.0-20190314010720-f0bfdbff1f9c/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
180174
gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo=
181175
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
182176
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/commands/executor.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewExecutor(version, commit, date string) *Executor {
3939
version: version,
4040
commit: commit,
4141
date: date,
42-
DBManager: lintersdb.NewManager(),
42+
DBManager: lintersdb.NewManager(nil),
4343
}
4444

4545
e.log = report.NewLogWrapper(logutils.NewStderrLog(""), &e.reportData)
@@ -82,6 +82,9 @@ func NewExecutor(version, commit, date string) *Executor {
8282
e.log.Fatalf("Can't read config: %s", err)
8383
}
8484

85+
// recreate after getting config
86+
e.DBManager = lintersdb.NewManager(e.cfg)
87+
8588
e.cfg.LintersSettings.Gocritic.InferEnabledChecks(e.log)
8689
if err := e.cfg.LintersSettings.Gocritic.Validate(e.log); err != nil {
8790
e.log.Fatalf("Invalid gocritic settings: %s", err)

pkg/commands/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) (<-chan resul
278278
}
279279
lintCtx.Log = e.log.Child("linters context")
280280

281-
runner, err := lint.NewRunner(lintCtx.ASTCache, e.cfg, e.log.Child("runner"), e.goenv, e.lineCache)
281+
runner, err := lint.NewRunner(lintCtx.ASTCache, e.cfg, e.log.Child("runner"),
282+
e.goenv, e.lineCache, e.DBManager)
282283
if err != nil {
283284
return nil, err
284285
}

pkg/config/config.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ type Run struct {
121121
}
122122

123123
type LintersSettings struct {
124-
Govet struct {
125-
CheckShadowing bool `mapstructure:"check-shadowing"`
126-
}
124+
Govet GovetSettings
127125
Golint struct {
128126
MinConfidence float64 `mapstructure:"min-confidence"`
129127
}
@@ -173,6 +171,11 @@ type LintersSettings struct {
173171
Gocritic GocriticSettings
174172
}
175173

174+
type GovetSettings struct {
175+
CheckShadowing bool `mapstructure:"check-shadowing"`
176+
Settings map[string]map[string]interface{}
177+
}
178+
176179
type ErrcheckSettings struct {
177180
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`
178181
CheckAssignToBlank bool `mapstructure:"check-blank"`

pkg/golinters/goanalysis/linter.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package goanalysis
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"strings"
8+
9+
"github.com/pkg/errors"
10+
"golang.org/x/tools/go/analysis"
11+
12+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis/checker"
13+
"github.com/golangci/golangci-lint/pkg/lint/linter"
14+
"github.com/golangci/golangci-lint/pkg/result"
15+
)
16+
17+
type Linter struct {
18+
name, desc string
19+
analyzers []*analysis.Analyzer
20+
cfg map[string]map[string]interface{}
21+
}
22+
23+
func NewLinter(name, desc string, analyzers []*analysis.Analyzer, cfg map[string]map[string]interface{}) *Linter {
24+
return &Linter{name: name, desc: desc, analyzers: analyzers, cfg: cfg}
25+
}
26+
27+
func (lnt Linter) Name() string {
28+
return lnt.name
29+
}
30+
31+
func (lnt Linter) Desc() string {
32+
return lnt.desc
33+
}
34+
35+
func (lnt Linter) allAnalyzerNames() []string {
36+
var ret []string
37+
for _, a := range lnt.analyzers {
38+
ret = append(ret, a.Name)
39+
}
40+
return ret
41+
}
42+
43+
func allFlagNames(fs *flag.FlagSet) []string {
44+
var ret []string
45+
fs.VisitAll(func(f *flag.Flag) {
46+
ret = append(ret, f.Name)
47+
})
48+
return ret
49+
}
50+
51+
func valueToString(v interface{}) string {
52+
if ss, ok := v.([]string); ok {
53+
return strings.Join(ss, ",")
54+
}
55+
56+
if is, ok := v.([]interface{}); ok {
57+
var ss []string
58+
for _, i := range is {
59+
ss = append(ss, fmt.Sprint(i))
60+
}
61+
return valueToString(ss)
62+
}
63+
64+
return fmt.Sprint(v)
65+
}
66+
67+
func (lnt Linter) configureAnalyzer(a *analysis.Analyzer, cfg map[string]interface{}) error {
68+
for k, v := range cfg {
69+
f := a.Flags.Lookup(k)
70+
if f == nil {
71+
validFlagNames := allFlagNames(&a.Flags)
72+
if len(validFlagNames) == 0 {
73+
return fmt.Errorf("analyzer doesn't have settings")
74+
}
75+
76+
return fmt.Errorf("analyzer doesn't have setting %q, valid settings: %v",
77+
k, validFlagNames)
78+
}
79+
80+
if err := f.Value.Set(valueToString(v)); err != nil {
81+
return errors.Wrapf(err, "failed to set analyzer setting %q with value %v", k, v)
82+
}
83+
}
84+
85+
return nil
86+
}
87+
88+
func (lnt Linter) configure() error {
89+
analyzersMap := map[string]*analysis.Analyzer{}
90+
for _, a := range lnt.analyzers {
91+
analyzersMap[a.Name] = a
92+
}
93+
94+
for analyzerName, analyzerSettings := range lnt.cfg {
95+
a := analyzersMap[analyzerName]
96+
if a == nil {
97+
return fmt.Errorf("settings key %q must be valid analyzer name, valid analyzers: %v",
98+
analyzerName, lnt.allAnalyzerNames())
99+
}
100+
101+
if err := lnt.configureAnalyzer(a, analyzerSettings); err != nil {
102+
return errors.Wrapf(err, "failed to configure analyzer %s", analyzerName)
103+
}
104+
}
105+
106+
return nil
107+
}
108+
109+
func (lnt Linter) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
110+
if err := analysis.Validate(lnt.analyzers); err != nil {
111+
return nil, errors.Wrap(err, "failed to validate analyzers")
112+
}
113+
114+
if err := lnt.configure(); err != nil {
115+
return nil, errors.Wrap(err, "failed to configure analyzers")
116+
}
117+
118+
diags, errs := checker.Run(lnt.analyzers, lintCtx.Packages)
119+
for i := 1; i < len(errs); i++ {
120+
lintCtx.Log.Warnf("%s error: %s", lnt.Name(), errs[i])
121+
}
122+
if len(errs) != 0 {
123+
return nil, errs[0]
124+
}
125+
126+
var issues []result.Issue
127+
for _, diag := range diags {
128+
i := result.Issue{
129+
FromLinter: lnt.Name(),
130+
Text: fmt.Sprintf("%s: %s", diag.AnalyzerName, diag.Message),
131+
Pos: diag.Position,
132+
}
133+
issues = append(issues, i)
134+
}
135+
136+
return issues, nil
137+
}

0 commit comments

Comments
 (0)