From b3f97632464b050f8893314649d0c3f06ad66872 Mon Sep 17 00:00:00 2001 From: Robert Liebowitz Date: Mon, 12 Jul 2021 20:23:12 -0400 Subject: [PATCH] errcheck: allow exclude config without extra file (#2110) Co-authored-by: Fernandez Ludovic --- .golangci.example.yml | 8 ++++++++ .golangci.yml | 6 ++++++ pkg/config/linters_settings.go | 11 +++++++---- pkg/golinters/errcheck.go | 2 ++ test/testdata/errcheck/exclude_functions.yml | 6 ++++++ test/testdata/errcheck_exclude_functions.go | 18 ++++++++++++++++++ 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/testdata/errcheck/exclude_functions.yml create mode 100644 test/testdata/errcheck_exclude_functions.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 350029ea..7cdb6073 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -113,10 +113,18 @@ linters-settings: # see https://github.com/kisielk/errcheck#the-deprecated-method for details ignore: fmt:.*,io/ioutil:^Read.* + # [deprecated] use exclude-functions instead. # path to a file containing a list of functions to exclude from checking # see https://github.com/kisielk/errcheck#excluding-functions for details exclude: /path/to/file.txt + # list of functions to exclude from checking, where each entry is a single function to exclude. + # see https://github.com/kisielk/errcheck#excluding-functions for details + exclude-functions: + - io/ioutil.ReadFile + - io.Copy(*bytes.Buffer) + - io.Copy(os.Stdout) + errorlint: # Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats errorf: true diff --git a/.golangci.yml b/.golangci.yml index 12768450..bc6efa23 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -122,6 +122,12 @@ issues: - path: _test\.go linters: - gomnd + + - path: pkg/golinters/errcheck.go + text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead" + - path: pkg/commands/run.go + text: "SA1019: lsc.Errcheck.Exclude is deprecated: use ExcludeFunctions instead" + # TODO must be removed after the release of the next version (v1.41.0) - path: pkg/commands/run.go linters: diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index d0b9c0de..b65c19c4 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -161,10 +161,13 @@ type DuplSettings struct { } type ErrcheckSettings struct { - CheckTypeAssertions bool `mapstructure:"check-type-assertions"` - CheckAssignToBlank bool `mapstructure:"check-blank"` - Ignore string `mapstructure:"ignore"` - Exclude string `mapstructure:"exclude"` + CheckTypeAssertions bool `mapstructure:"check-type-assertions"` + CheckAssignToBlank bool `mapstructure:"check-blank"` + Ignore string `mapstructure:"ignore"` + ExcludeFunctions []string `mapstructure:"exclude-functions"` + + // Deprecated: use ExcludeFunctions instead + Exclude string `mapstructure:"exclude"` } type ErrorLintSettings struct { diff --git a/pkg/golinters/errcheck.go b/pkg/golinters/errcheck.go index 9aac7326..2d9a4fc4 100644 --- a/pkg/golinters/errcheck.go +++ b/pkg/golinters/errcheck.go @@ -152,6 +152,8 @@ func getChecker(errCfg *config.ErrcheckSettings) (*errcheck.Checker, error) { checker.Exclusions.Symbols = append(checker.Exclusions.Symbols, exclude...) } + checker.Exclusions.Symbols = append(checker.Exclusions.Symbols, errCfg.ExcludeFunctions...) + return &checker, nil } diff --git a/test/testdata/errcheck/exclude_functions.yml b/test/testdata/errcheck/exclude_functions.yml new file mode 100644 index 00000000..fcf07927 --- /dev/null +++ b/test/testdata/errcheck/exclude_functions.yml @@ -0,0 +1,6 @@ +linters-settings: + errcheck: + check-blank: true + exclude-functions: + - io/ioutil.ReadFile + - io/ioutil.ReadDir diff --git a/test/testdata/errcheck_exclude_functions.go b/test/testdata/errcheck_exclude_functions.go new file mode 100644 index 00000000..9ad8dda6 --- /dev/null +++ b/test/testdata/errcheck_exclude_functions.go @@ -0,0 +1,18 @@ +//args: -Eerrcheck +//config_path: testdata/errcheck/exclude_functions.yml +package testdata + +import ( + "io/ioutil" +) + +func TestErrcheckExcludeFunctions() []byte { + ret, _ := ioutil.ReadFile("f.txt") + ioutil.ReadDir("dir") + return ret +} + +func TestErrcheckNoExcludeFunctions() []byte { + ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value of `ioutil.ReadAll` is not checked" + return ret +}