diff --git a/README.md b/README.md index e69f42ff2b74..7b50dd781739 100644 --- a/README.md +++ b/README.md @@ -469,6 +469,7 @@ Flags: --no-config Don't read config --skip-dirs strings Regexps of directories to skip --skip-files strings Regexps of files to skip + --errcheck.without-tests Boolean value to include tests in errcheck or not -E, --enable strings Enable specific linter -D, --disable strings Disable specific linter --enable-all Enable all linters diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 42faf7e2400a..e7a06fb9cffd 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -103,6 +103,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is fs.StringVar(&lsc.Errcheck.Ignore, "errcheck.ignore", "fmt:.*", `Comma-separated list of pairs of the form pkg:regex. The regex is used to ignore names within pkg`) hideFlag("errcheck.ignore") + fs.BoolVar(&lsc.Errcheck.WithoutTests, "errcheck.without-tests", false, "Boolean value to include tests in errcheck or not") fs.BoolVar(&lsc.Govet.CheckShadowing, "govet.check-shadowing", false, "Govet: check for shadowed variables") diff --git a/pkg/config/config.go b/pkg/config/config.go index 720c5e312e2d..eb2e7dd16a9f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -181,6 +181,7 @@ type ErrcheckSettings struct { CheckAssignToBlank bool `mapstructure:"check-blank"` Ignore string `mapstructure:"ignore"` Exclude string `mapstructure:"exclude"` + WithoutTests bool `mapstructure:"without-tests"` } type LllSettings struct { diff --git a/pkg/golinters/errcheck.go b/pkg/golinters/errcheck.go index bd1d7c3ef73d..11665f4d5c52 100644 --- a/pkg/golinters/errcheck.go +++ b/pkg/golinters/errcheck.go @@ -98,9 +98,10 @@ func genConfig(errCfg *config.ErrcheckSettings) (*errcheckAPI.Config, error) { } c := &errcheckAPI.Config{ - Ignore: ignoreConfig, - Blank: errCfg.CheckAssignToBlank, - Asserts: errCfg.CheckTypeAssertions, + Ignore: ignoreConfig, + Blank: errCfg.CheckAssignToBlank, + Asserts: errCfg.CheckTypeAssertions, + WithoutTests: errCfg.WithoutTests, } if errCfg.Exclude != "" { diff --git a/vendor/github.com/golangci/errcheck/golangci/golangci.go b/vendor/github.com/golangci/errcheck/golangci/golangci.go index 1095adeb4de6..27a1814d08f1 100644 --- a/vendor/github.com/golangci/errcheck/golangci/golangci.go +++ b/vendor/github.com/golangci/errcheck/golangci/golangci.go @@ -2,6 +2,7 @@ package golangci import ( "regexp" + "strings" "github.com/golangci/errcheck/internal/errcheck" "golang.org/x/tools/go/loader" @@ -59,10 +60,9 @@ func RunWithConfig(program *loader.Program, c *Config) ([]Issue, error) { for pkg, re := range c.Ignore { checker.Ignore[pkg] = re } - if err := checker.CheckProgram(program); err != nil { if e, ok := err.(*errcheck.UncheckedErrors); ok { - return makeIssues(e), nil + return makeIssues(e, checker.WithoutTests), nil } if err == errcheck.ErrNoGoFiles { return nil, nil @@ -75,9 +75,12 @@ func RunWithConfig(program *loader.Program, c *Config) ([]Issue, error) { return nil, nil } -func makeIssues(e *errcheck.UncheckedErrors) []Issue { +func makeIssues(e *errcheck.UncheckedErrors, withoutTests bool) []Issue { var ret []Issue for _, uncheckedError := range e.Errors { + if withoutTests && strings.Contains(uncheckedError.Pos.Filename, "_test.go") { + continue + } ret = append(ret, Issue(uncheckedError)) }