Skip to content

Commit

Permalink
errcheck: allow exclude config without extra file (#2110)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
rliebz and ldez authored Jul 13, 2021
1 parent f285d2c commit b3f9763
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/golinters/errcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions test/testdata/errcheck/exclude_functions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters-settings:
errcheck:
check-blank: true
exclude-functions:
- io/ioutil.ReadFile
- io/ioutil.ReadDir
18 changes: 18 additions & 0 deletions test/testdata/errcheck_exclude_functions.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b3f9763

Please sign in to comment.