Skip to content

Commit b77118f

Browse files
Sergey Vilgelmleventovldez
authored
Use errcheck from main repo instead of golangci-lint fork (#1319)
Co-authored-by: Roman Leventov <[email protected]> Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent ce3ff22 commit b77118f

File tree

10 files changed

+91
-40
lines changed

10 files changed

+91
-40
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ require (
2323
github.com/gofrs/flock v0.8.0
2424
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
2525
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
26-
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6
2726
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613
2827
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
2928
github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc
@@ -35,6 +34,7 @@ require (
3534
github.com/jgautheron/goconst v0.0.0-20201117150253-ccae5bf973f3
3635
github.com/jingyugao/rowserrcheck v0.0.0-20210130005344-c6a0c12dd98d
3736
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
37+
github.com/kisielk/errcheck v1.6.0
3838
github.com/kulti/thelper v0.4.0
3939
github.com/kunwardeep/paralleltest v1.0.2
4040
github.com/kyoh86/exportloopref v0.1.8

go.sum

Lines changed: 3 additions & 2 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: 62 additions & 30 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"
@@ -23,51 +24,70 @@ import (
2324

2425
func NewErrcheck() *goanalysis.Linter {
2526
const linterName = "errcheck"
27+
2628
var mu sync.Mutex
2729
var res []goanalysis.Issue
30+
2831
analyzer := &analysis.Analyzer{
2932
Name: linterName,
3033
Doc: goanalysis.TheOnlyanalyzerDoc,
3134
}
35+
3236
return goanalysis.NewLinter(
3337
linterName,
3438
"Errcheck is a program for checking for unchecked errors "+
3539
"in go programs. These unchecked errors can be critical bugs in some cases",
3640
[]*analysis.Analyzer{analyzer},
3741
nil,
3842
).WithContextSetter(func(lintCtx *linter.Context) {
43+
// copied from errcheck
44+
checker, err := getChecker(&lintCtx.Settings().Errcheck)
45+
if err != nil {
46+
lintCtx.Log.Errorf("failed to get checker: %v", err)
47+
return
48+
}
49+
50+
checker.Tags = lintCtx.Cfg.Run.BuildTags
51+
3952
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
53+
pkg := &packages.Package{
54+
Fset: pass.Fset,
55+
Syntax: pass.Files,
56+
Types: pass.Pkg,
57+
TypesInfo: pass.TypesInfo,
4858
}
4959

50-
if len(errcheckIssues) == 0 {
60+
errcheckIssues := checker.CheckPackage(pkg).Unique()
61+
if len(errcheckIssues.UncheckedErrors) == 0 {
5162
return nil, nil
5263
}
5364

54-
issues := make([]goanalysis.Issue, 0, len(errcheckIssues))
55-
for _, i := range errcheckIssues {
65+
issues := make([]goanalysis.Issue, len(errcheckIssues.UncheckedErrors))
66+
for i, err := range errcheckIssues.UncheckedErrors {
5667
var text string
57-
if i.FuncName != "" {
58-
text = fmt.Sprintf("Error return value of %s is not checked", formatCode(i.FuncName, lintCtx.Cfg))
68+
if err.FuncName != "" {
69+
text = fmt.Sprintf(
70+
"Error return value of %s is not checked",
71+
formatCode(err.SelectorName, lintCtx.Cfg),
72+
)
5973
} else {
6074
text = "Error return value is not checked"
6175
}
62-
issues = append(issues, goanalysis.NewIssue(&result.Issue{
63-
FromLinter: linterName,
64-
Text: text,
65-
Pos: i.Pos,
66-
}, pass))
76+
77+
issues[i] = goanalysis.NewIssue(
78+
&result.Issue{
79+
FromLinter: linterName,
80+
Text: text,
81+
Pos: err.Pos,
82+
},
83+
pass,
84+
)
6785
}
86+
6887
mu.Lock()
6988
res = append(res, issues...)
7089
mu.Unlock()
90+
7191
return nil, nil
7292
}
7393
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
@@ -104,27 +124,35 @@ func parseIgnoreConfig(s string) (map[string]*regexp.Regexp, error) {
104124
return cfg, nil
105125
}
106126

107-
func genConfig(errCfg *config.ErrcheckSettings) (*errcheck.Config, error) {
127+
func getChecker(errCfg *config.ErrcheckSettings) (*errcheck.Checker, error) {
108128
ignoreConfig, err := parseIgnoreConfig(errCfg.Ignore)
109129
if err != nil {
110130
return nil, errors.Wrap(err, "failed to parse 'ignore' directive")
111131
}
112132

113-
c := &errcheck.Config{
114-
Ignore: ignoreConfig,
115-
Blank: errCfg.CheckAssignToBlank,
116-
Asserts: errCfg.CheckTypeAssertions,
133+
checker := errcheck.Checker{
134+
Exclusions: errcheck.Exclusions{
135+
BlankAssignments: !errCfg.CheckAssignToBlank,
136+
TypeAssertions: !errCfg.CheckTypeAssertions,
137+
SymbolRegexpsByPackage: map[string]*regexp.Regexp{},
138+
Symbols: append([]string{}, errcheck.DefaultExcludedSymbols...),
139+
},
140+
}
141+
142+
for pkg, re := range ignoreConfig {
143+
checker.Exclusions.SymbolRegexpsByPackage[pkg] = re
117144
}
118145

119146
if errCfg.Exclude != "" {
120147
exclude, err := readExcludeFile(errCfg.Exclude)
121148
if err != nil {
122149
return nil, err
123150
}
124-
c.Exclude = exclude
151+
152+
checker.Exclusions.Symbols = append(checker.Exclusions.Symbols, exclude...)
125153
}
126154

127-
return c, nil
155+
return &checker, nil
128156
}
129157

130158
func getFirstPathArg() string {
@@ -192,7 +220,7 @@ func setupConfigFileSearch(name string) []string {
192220
return configSearchPaths
193221
}
194222

195-
func readExcludeFile(name string) (map[string]bool, error) {
223+
func readExcludeFile(name string) ([]string, error) {
196224
var err error
197225
var fh *os.File
198226

@@ -205,13 +233,17 @@ func readExcludeFile(name string) (map[string]bool, error) {
205233
if fh == nil {
206234
return nil, errors.Wrapf(err, "failed reading exclude file: %s", name)
207235
}
236+
208237
scanner := bufio.NewScanner(fh)
209-
exclude := make(map[string]bool)
238+
239+
var excludes []string
210240
for scanner.Scan() {
211-
exclude[scanner.Text()] = true
241+
excludes = append(excludes, scanner.Text())
212242
}
243+
213244
if err := scanner.Err(); err != nil {
214245
return nil, errors.Wrapf(err, "failed scanning file: %s", name)
215246
}
216-
return exclude, nil
247+
248+
return excludes, nil
217249
}

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_ignore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func TestErrcheckIgnoreOs() {
1212
_, _ = os.Open("f.txt")
1313
}
1414

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

test/testdata/errcheck_ignore_default.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
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
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)