Skip to content

Commit 4c88f08

Browse files
Sergey Vilgelmldez
Sergey Vilgelm
authored andcommitted
Use errcheck from main repo instead of golangci-lint
1 parent aeb9830 commit 4c88f08

File tree

12 files changed

+69
-34
lines changed

12 files changed

+69
-34
lines changed

.github/workflows/pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
uses: golangci/[email protected]
3434
with:
3535
version: latest
36+
args: --verbose
3637
tests-on-windows:
3738
needs: golangci-lint # run after golangci-lint action to not produce duplicated errors
3839
runs-on: windows-latest

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
github.com/gofrs/flock v0.8.0
1818
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
1919
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
20-
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6
2120
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613
2221
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d
2322
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
@@ -34,6 +33,8 @@ require (
3433
github.com/kulti/thelper v0.1.0
3534
github.com/kunwardeep/paralleltest v1.0.2
3635
github.com/kyoh86/exportloopref v0.1.8
36+
github.com/kisielk/errcheck v1.4.1-0.20200802052755-ea6ea2fa7078
37+
github.com/kyoh86/exportloopref v0.1.7
3738
github.com/maratori/testpackage v1.0.1
3839
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
3940
github.com/mattn/go-colorable v0.1.8
@@ -77,3 +78,5 @@ require (
7778
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
7879
mvdan.cc/unparam v0.0.0-20200501210554-b37ab49443f7
7980
)
81+
82+
replace github.com/kisielk/errcheck => /Users/[email protected]/icloud/projects/errcheck

go.sum

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/commands/executor.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {
205205
configData.WriteString("\nbuild-tags=%s" + strings.Join(cfg.Run.BuildTags, ","))
206206

207207
h := sha256.New()
208-
h.Write(configData.Bytes()) //nolint:errcheck
208+
if _, err := h.Write(configData.Bytes()); err != nil {
209+
return nil, err
210+
}
209211
return h.Sum(nil), nil
210212
}
211213

pkg/golinters/errcheck.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010
"strings"
1111
"sync"
1212

13-
errcheck "github.com/golangci/errcheck/golangci"
13+
"github.com/kisielk/errcheck/errcheck"
1414
"github.com/pkg/errors"
1515
"golang.org/x/tools/go/analysis"
16+
"golang.org/x/tools/go/packages"
1617

1718
"github.com/golangci/golangci-lint/pkg/config"
1819
"github.com/golangci/golangci-lint/pkg/fsutils"
@@ -36,17 +37,23 @@ func NewErrcheck() *goanalysis.Linter {
3637
[]*analysis.Analyzer{analyzer},
3738
nil,
3839
).WithContextSetter(func(lintCtx *linter.Context) {
40+
// copied from errcheck
41+
checker, err := getChecker(&lintCtx.Settings().Errcheck)
42+
if err != nil {
43+
panic(err.Error())
44+
}
45+
checker.Verbose = lintCtx.Cfg.Run.IsVerbose
46+
checker.Tags = lintCtx.Cfg.Run.BuildTags
47+
3948
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
40-
prog := goanalysis.MakeFakeLoaderProgram(pass)
41-
errCfg, err := genConfig(&lintCtx.Settings().Errcheck)
42-
if err != nil {
43-
return nil, err
44-
}
45-
errcheckIssues, err := errcheck.RunWithConfig(prog, errCfg)
46-
if err != nil {
47-
return nil, err
49+
pkg := &packages.Package{
50+
Fset: pass.Fset,
51+
Syntax: pass.Files,
52+
Types: pass.Pkg,
53+
TypesInfo: pass.TypesInfo,
4854
}
4955

56+
errcheckIssues := checker.CheckPackage(pkg)
5057
if len(errcheckIssues) == 0 {
5158
return nil, nil
5259
}
@@ -104,27 +111,32 @@ func parseIgnoreConfig(s string) (map[string]*regexp.Regexp, error) {
104111
return cfg, nil
105112
}
106113

107-
func genConfig(errCfg *config.ErrcheckSettings) (*errcheck.Config, error) {
114+
func getChecker(errCfg *config.ErrcheckSettings) (*errcheck.Checker, error) {
115+
checker := errcheck.NewChecker()
116+
checker.Blank = errCfg.CheckAssignToBlank
117+
checker.Asserts = errCfg.CheckTypeAssertions
118+
108119
ignoreConfig, err := parseIgnoreConfig(errCfg.Ignore)
109120
if err != nil {
110121
return nil, errors.Wrap(err, "failed to parse 'ignore' directive")
111122
}
112123

113-
c := &errcheck.Config{
114-
Ignore: ignoreConfig,
115-
Blank: errCfg.CheckAssignToBlank,
116-
Asserts: errCfg.CheckTypeAssertions,
124+
checker.Ignore = map[string]*regexp.Regexp{}
125+
for pkg, re := range ignoreConfig {
126+
checker.Ignore[pkg] = re
117127
}
128+
checker.UpdateNonVendoredIgnore()
118129

130+
checker.AddExcludes(errcheck.DefaultExcludes)
119131
if errCfg.Exclude != "" {
120132
exclude, err := readExcludeFile(errCfg.Exclude)
121133
if err != nil {
122134
return nil, err
123135
}
124-
c.Exclude = exclude
136+
checker.AddExcludes(exclude)
125137
}
126138

127-
return c, nil
139+
return checker, nil
128140
}
129141

130142
func getFirstPathArg() string {
@@ -192,7 +204,7 @@ func setupConfigFileSearch(name string) []string {
192204
return configSearchPaths
193205
}
194206

195-
func readExcludeFile(name string) (map[string]bool, error) {
207+
func readExcludeFile(name string) ([]string, error) {
196208
var err error
197209
var fh *os.File
198210

@@ -206,12 +218,12 @@ func readExcludeFile(name string) (map[string]bool, error) {
206218
return nil, errors.Wrapf(err, "failed reading exclude file: %s", name)
207219
}
208220
scanner := bufio.NewScanner(fh)
209-
exclude := make(map[string]bool)
221+
excludes := []string{}
210222
for scanner.Scan() {
211-
exclude[scanner.Text()] = true
223+
excludes = append(excludes, scanner.Text())
212224
}
213225
if err := scanner.Err(); err != nil {
214226
return nil, errors.Wrapf(err, "failed scanning file: %s", name)
215227
}
216-
return exclude, nil
228+
return excludes, nil
217229
}

scripts/expand_website_templates/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func updateStateFile(replacements map[string]string) error {
5555
}
5656

5757
h := sha256.New()
58-
h.Write(replBytes) //nolint:errcheck
58+
if _, err := h.Write(replBytes); err != nil {
59+
return err
60+
}
5961

6062
var contentBuf bytes.Buffer
6163
contentBuf.WriteString("This file stores hash of website templates to trigger " +

test/run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestSortedResults(t *testing.T) {
135135
"--sort-results=false",
136136
strings.Join([]string{
137137
"testdata/sort_results/main.go:12:5: `db` is unused (deadcode)",
138-
"testdata/sort_results/main.go:15:13: Error return value of `returnError` is not checked (errcheck)",
138+
"testdata/sort_results/main.go:15:13: Error return value is not checked (errcheck)",
139139
"testdata/sort_results/main.go:8:6: func `returnError` is unused (unused)",
140140
}, "\n"),
141141
},
@@ -144,7 +144,7 @@ func TestSortedResults(t *testing.T) {
144144
strings.Join([]string{
145145
"testdata/sort_results/main.go:8:6: func `returnError` is unused (unused)",
146146
"testdata/sort_results/main.go:12:5: `db` is unused (deadcode)",
147-
"testdata/sort_results/main.go:15:13: Error return value of `returnError` is not checked (errcheck)",
147+
"testdata/sort_results/main.go:15:13: Error return value is not checked (errcheck)",
148148
}, "\n"),
149149
},
150150
}

test/testdata/errcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func RetErr() error {
1212
}
1313

1414
func MissedErrorCheck() {
15-
RetErr() // ERROR "Error return value of `RetErr` is not checked"
15+
RetErr() // ERROR "Error return value is not checked"
1616
}
1717

1818
func IgnoreCloseMissingErrHandling() error {

test/testdata/errcheck_exclude.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ func TestErrcheckExclude() []byte {
1313
}
1414

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

test/testdata/errcheck_ignore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestErrcheckIgnoreOs() {
1313
}
1414

1515
func TestErrcheckNoIgnoreFmt(s string) int {
16-
n, _ := fmt.Println(s) // ERROR "Error return value of `fmt.Println` is not checked"
16+
n, _ := fmt.Println(s) // ERROR "Error return value is not checked"
1717
return n
1818
}
1919

@@ -23,6 +23,6 @@ func TestErrcheckIgnoreIoutil() []byte {
2323
}
2424

2525
func TestErrcheckNoIgnoreIoutil() []byte {
26-
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value of `ioutil.ReadAll` is not checked"
26+
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value is not checked"
2727
return ret
2828
}

test/testdata/errcheck_ignore_default.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
package testdata
44

55
import (
6+
"crypto/sha256"
67
"fmt"
78
"os"
89
)
910

11+
func TestErrcheckIgnoreHashWriteByDefault() []byte {
12+
h := sha256.New()
13+
h.Write([]byte("food"))
14+
return h.Sum(nil)
15+
}
16+
1017
func TestErrcheckIgnoreFmtByDefault(s string) int {
1118
n, _ := fmt.Println(s)
1219
return n
1320
}
1421

1522
func TestErrcheckNoIgnoreOs() {
16-
_, _ = os.Open("f.txt") // ERROR "Error return value of `os.Open` is not checked"
23+
_, _ = os.Open("f.txt") // ERROR "Error return value is not checked"
1724
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//args: -Eerrcheck
2+
//config: linters-settings.errcheck.check-type-assertions=true
3+
package testdata
4+
5+
func ErrorTypeAssertion(filter map[string]interface{}) bool {
6+
return filter["messages_sent.messageid"].(map[string]interface{})["$ne"] != nil
7+
}

0 commit comments

Comments
 (0)