Skip to content

Commit b81346f

Browse files
a8mjirfag
authored andcommitted
linters/errcheck: add support for ignore/exclude flags
Add support for the exclude and ignore flags of errcheck. Note that, this commit depends on golangci/errcheck#1. We need to merge it first, update vendors and only then merge this one.
1 parent 59c3d20 commit b81346f

File tree

9 files changed

+530
-14
lines changed

9 files changed

+530
-14
lines changed

Gopkg.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
name = "github.com/spf13/viper"
2323
version = "1.0.2"
2424

25+
[[constraint]]
26+
branch = "master"
27+
name = "github.com/spf13/pflag"
28+
2529
[[constraint]]
2630
branch = "master"
2731
name = "github.com/mitchellh/go-ps"

pkg/commands/run.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,20 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager) {
7575
lsc := &cfg.LintersSettings
7676

7777
// Hide all linters settings flags: they were initially visible,
78-
// but when number of linters started to grow it became ovious that
78+
// but when number of linters started to grow it became obvious that
7979
// we can't fill 90% of flags by linters settings: common flags became hard to find.
8080
// New linters settings should be done only through config file.
8181
fs.BoolVar(&lsc.Errcheck.CheckTypeAssertions, "errcheck.check-type-assertions",
8282
false, "Errcheck: check for ignored type assertion results")
8383
hideFlag("errcheck.check-type-assertions")
84-
8584
fs.BoolVar(&lsc.Errcheck.CheckAssignToBlank, "errcheck.check-blank", false,
8685
"Errcheck: check for errors assigned to blank identifier: _ = errFunc()")
8786
hideFlag("errcheck.check-blank")
87+
fs.StringVar(&lsc.Errcheck.Exclude, "errcheck.exclude", "", "errcheck.exclude")
88+
hideFlag("errcheck.exclude")
89+
lsc.Errcheck.Ignore = config.IgnoreFlag{}
90+
fs.Var(&lsc.Errcheck.Ignore, "errcheck.ignore", "errcheck.ignore")
91+
hideFlag("errcheck.ignore")
8892

8993
fs.BoolVar(&lsc.Govet.CheckShadowing, "govet.check-shadowing", false,
9094
"Govet: check for shadowed variables")

pkg/config/config.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package config
22

33
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
47
"time"
58
)
69

@@ -115,10 +118,6 @@ type Run struct {
115118
}
116119

117120
type LintersSettings struct {
118-
Errcheck struct {
119-
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`
120-
CheckAssignToBlank bool `mapstructure:"check-blank"`
121-
}
122121
Govet struct {
123122
CheckShadowing bool `mapstructure:"check-shadowing"`
124123
UseInstalledPackages bool `mapstructure:"use-installed-packages"`
@@ -164,6 +163,14 @@ type LintersSettings struct {
164163
Unparam UnparamSettings
165164
Nakedret NakedretSettings
166165
Prealloc PreallocSettings
166+
Errcheck ErrcheckSettings
167+
}
168+
169+
type ErrcheckSettings struct {
170+
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`
171+
CheckAssignToBlank bool `mapstructure:"check-blank"`
172+
Ignore IgnoreFlag `mapstructure:"ignore"`
173+
Exclude string `mapstructure:"exclude"`
167174
}
168175

169176
type LllSettings struct {
@@ -248,3 +255,47 @@ func NewDefault() *Config {
248255
LintersSettings: defaultLintersSettings,
249256
}
250257
}
258+
259+
// IgnoreFlags was taken from errcheck in order to keep the API identical.
260+
// https://github.com/kisielk/errcheck/blob/1787c4bee836470bf45018cfbc783650db3c6501/main.go#L25-L60
261+
type IgnoreFlag map[string]*regexp.Regexp
262+
263+
func (f IgnoreFlag) String() string {
264+
pairs := make([]string, 0, len(f))
265+
for pkg, re := range f {
266+
prefix := ""
267+
if pkg != "" {
268+
prefix = pkg + ":"
269+
}
270+
pairs = append(pairs, prefix+re.String())
271+
}
272+
return fmt.Sprintf("%q", strings.Join(pairs, ","))
273+
}
274+
275+
func (f IgnoreFlag) Set(s string) error {
276+
if s == "" {
277+
return nil
278+
}
279+
for _, pair := range strings.Split(s, ",") {
280+
colonIndex := strings.Index(pair, ":")
281+
var pkg, re string
282+
if colonIndex == -1 {
283+
pkg = ""
284+
re = pair
285+
} else {
286+
pkg = pair[:colonIndex]
287+
re = pair[colonIndex+1:]
288+
}
289+
regex, err := regexp.Compile(re)
290+
if err != nil {
291+
return err
292+
}
293+
f[pkg] = regex
294+
}
295+
return nil
296+
}
297+
298+
// Type returns the type of the flag follow the pflag format.
299+
func (IgnoreFlag) Type() string {
300+
return "stringToRegexp"
301+
}

pkg/golinters/errcheck.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package golinters
22

33
import (
4+
"bufio"
45
"context"
56
"fmt"
7+
"os"
68

79
errcheckAPI "github.com/golangci/errcheck/golangci"
10+
"github.com/golangci/golangci-lint/pkg/config"
811
"github.com/golangci/golangci-lint/pkg/lint/linter"
912
"github.com/golangci/golangci-lint/pkg/result"
13+
"github.com/pkg/errors"
1014
)
1115

1216
type Errcheck struct{}
@@ -21,8 +25,11 @@ func (Errcheck) Desc() string {
2125
}
2226

2327
func (e Errcheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
24-
errCfg := &lintCtx.Settings().Errcheck
25-
issues, err := errcheckAPI.Run(lintCtx.Program, errCfg.CheckAssignToBlank, errCfg.CheckTypeAssertions)
28+
errCfg, err := genConfig(&lintCtx.Settings().Errcheck)
29+
if err != nil {
30+
return nil, err
31+
}
32+
issues, err := errcheckAPI.RunWithConfig(lintCtx.Program, errCfg)
2633
if err != nil {
2734
return nil, err
2835
}
@@ -48,3 +55,36 @@ func (e Errcheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is
4855

4956
return res, nil
5057
}
58+
59+
func genConfig(errCfg *config.ErrcheckSettings) (*errcheckAPI.Config, error) {
60+
c := &errcheckAPI.Config{
61+
Ignore: errCfg.Ignore,
62+
Blank: errCfg.CheckAssignToBlank,
63+
Asserts: errCfg.CheckTypeAssertions,
64+
}
65+
if errCfg.Exclude != "" {
66+
exclude, err := readExcludeFile(errCfg.Exclude)
67+
if err != nil {
68+
return nil, err
69+
}
70+
c.Exclude = exclude
71+
}
72+
return c, nil
73+
}
74+
75+
func readExcludeFile(name string) (map[string]bool, error) {
76+
exclude := make(map[string]bool)
77+
fh, err := os.Open(name)
78+
if err != nil {
79+
return nil, errors.Wrapf(err, "failed reading exclude file: %s", name)
80+
}
81+
scanner := bufio.NewScanner(fh)
82+
for scanner.Scan() {
83+
name := scanner.Text()
84+
exclude[name] = true
85+
}
86+
if err := scanner.Err(); err != nil {
87+
return nil, errors.Wrapf(err, "failed scanning file: %s", name)
88+
}
89+
return exclude, nil
90+
}

vendor/github.com/spf13/pflag/bytes.go

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/spf13/pflag/flag.go

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)