Skip to content

Commit b3f9763

Browse files
rliebzldez
andauthored
errcheck: allow exclude config without extra file (#2110)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent f285d2c commit b3f9763

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

.golangci.example.yml

+8
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,18 @@ linters-settings:
113113
# see https://github.com/kisielk/errcheck#the-deprecated-method for details
114114
ignore: fmt:.*,io/ioutil:^Read.*
115115

116+
# [deprecated] use exclude-functions instead.
116117
# path to a file containing a list of functions to exclude from checking
117118
# see https://github.com/kisielk/errcheck#excluding-functions for details
118119
exclude: /path/to/file.txt
119120

121+
# list of functions to exclude from checking, where each entry is a single function to exclude.
122+
# see https://github.com/kisielk/errcheck#excluding-functions for details
123+
exclude-functions:
124+
- io/ioutil.ReadFile
125+
- io.Copy(*bytes.Buffer)
126+
- io.Copy(os.Stdout)
127+
120128
errorlint:
121129
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
122130
errorf: true

.golangci.yml

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ issues:
122122
- path: _test\.go
123123
linters:
124124
- gomnd
125+
126+
- path: pkg/golinters/errcheck.go
127+
text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
128+
- path: pkg/commands/run.go
129+
text: "SA1019: lsc.Errcheck.Exclude is deprecated: use ExcludeFunctions instead"
130+
125131
# TODO must be removed after the release of the next version (v1.41.0)
126132
- path: pkg/commands/run.go
127133
linters:

pkg/config/linters_settings.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,13 @@ type DuplSettings struct {
161161
}
162162

163163
type ErrcheckSettings struct {
164-
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`
165-
CheckAssignToBlank bool `mapstructure:"check-blank"`
166-
Ignore string `mapstructure:"ignore"`
167-
Exclude string `mapstructure:"exclude"`
164+
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`
165+
CheckAssignToBlank bool `mapstructure:"check-blank"`
166+
Ignore string `mapstructure:"ignore"`
167+
ExcludeFunctions []string `mapstructure:"exclude-functions"`
168+
169+
// Deprecated: use ExcludeFunctions instead
170+
Exclude string `mapstructure:"exclude"`
168171
}
169172

170173
type ErrorLintSettings struct {

pkg/golinters/errcheck.go

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func getChecker(errCfg *config.ErrcheckSettings) (*errcheck.Checker, error) {
152152
checker.Exclusions.Symbols = append(checker.Exclusions.Symbols, exclude...)
153153
}
154154

155+
checker.Exclusions.Symbols = append(checker.Exclusions.Symbols, errCfg.ExcludeFunctions...)
156+
155157
return &checker, nil
156158
}
157159

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
linters-settings:
2+
errcheck:
3+
check-blank: true
4+
exclude-functions:
5+
- io/ioutil.ReadFile
6+
- io/ioutil.ReadDir
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//args: -Eerrcheck
2+
//config_path: testdata/errcheck/exclude_functions.yml
3+
package testdata
4+
5+
import (
6+
"io/ioutil"
7+
)
8+
9+
func TestErrcheckExcludeFunctions() []byte {
10+
ret, _ := ioutil.ReadFile("f.txt")
11+
ioutil.ReadDir("dir")
12+
return ret
13+
}
14+
15+
func TestErrcheckNoExcludeFunctions() []byte {
16+
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value of `ioutil.ReadAll` is not checked"
17+
return ret
18+
}

0 commit comments

Comments
 (0)