Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errcheck: allow exclude config without extra file #2110

Merged
merged 3 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ linters-settings:
# 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
3 changes: 3 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
fs.StringVar(&lsc.Errcheck.Exclude, "errcheck.exclude", "",
"Path to a file containing a list of functions to exclude from checking")
hideFlag("errcheck.exclude")
fs.StringSliceVar(&lsc.Errcheck.ExcludeFunctions, "errcheck.exclude-functions", nil,
"List of functions to exclude from checking")
hideFlag("errcheck.exclude-functions")
ldez marked this conversation as resolved.
Show resolved Hide resolved
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")
Expand Down
9 changes: 5 additions & 4 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ 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"`
Exclude string `mapstructure:"exclude"`
ExcludeFunctions []string `mapstructure:"exclude-functions"`
}

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
19 changes: 19 additions & 0 deletions test/testdata/errcheck_exclude_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//args: -Eerrcheck
//config: linters-settings.errcheck.check-blank=true
//config: linters-settings.errcheck.exclude-functions=io/ioutil.ReadFile,io/ioutil.ReadDir
ldez marked this conversation as resolved.
Show resolved Hide resolved
package testdata

import (
"io/ioutil"
)

func TestErrcheckExclude() []byte {
ret, _ := ioutil.ReadFile("f.txt")
_, _ = ioutil.ReadDir("dir")
return ret
}

func TestErrcheckNoExclude() []byte {
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value of `ioutil.ReadAll` is not checked"
return ret
}