Skip to content

Commit

Permalink
Use errcheck from main repo instead of golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Vilgelm committed Aug 19, 2020
1 parent 6ac41d9 commit d770f83
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 34 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pr.yml
Expand Up @@ -18,6 +18,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
version: latest
args: --verbose
tests-on-windows:
needs: golangci-lint # run after golangci-lint action to not produce duplicated errors
runs-on: windows-latest
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Expand Up @@ -14,7 +14,6 @@ require (
github.com/gofrs/flock v0.7.1
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613
github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d
Expand All @@ -28,6 +27,7 @@ require (
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
github.com/kisielk/errcheck v1.4.1-0.20200802052755-ea6ea2fa7078
github.com/kyoh86/exportloopref v0.1.7
github.com/maratori/testpackage v1.0.1
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
Expand Down Expand Up @@ -66,3 +66,5 @@ require (
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f
)

replace github.com/kisielk/errcheck => /Users/sergey.vilgelm@ibm.com/icloud/projects/errcheck
9 changes: 5 additions & 4 deletions go.sum
Expand Up @@ -71,6 +71,7 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-toolsmith/astcast v1.0.0 h1:JojxlmI6STnFVG9yOImLeGREv8W2ocNUM+iOhR6jE7g=
Expand Down Expand Up @@ -119,8 +120,6 @@ github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 h1:23T5iq8rbUYlhpt5
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2/go.mod h1:k9Qvh+8juN+UKMCS/3jFtGICgW8O96FVaZsaxdzDkR4=
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a h1:w8hkcTqaFpzKqonE9uMCefW1WDie15eSP/4MssdenaM=
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a/go.mod h1:ryS0uhF+x9jgbj/N71xsEqODy9BN81/GonCZiOzirOk=
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 h1:YYWNAGTKWhKpcLLt7aSj/odlKrSrelQwlovBpDuf19w=
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6/go.mod h1:DbHgvLiFKX1Sh2T1w8Q/h4NAI8MHIpzCdnBUDTXU3I0=
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 h1:9kfjN3AdxcbsZBf8NjltjWihK2QfBBBZuv91cMFfDHw=
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613/go.mod h1:SyvUF2NxV+sN8upjjeVYr5W7tyxaT1JVtvhKhOn2ii8=
github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3 h1:pe9JHs3cHHDQgOFXJJdYkK6fLz2PWyYtP4hthoCMvs8=
Expand Down Expand Up @@ -198,14 +197,14 @@ github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a/go.mod h1:x
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3 h1:jNYPNLe3d8smommaoQlK7LOA5ESyUJJ+Wf79ZtA7Vp4=
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3/go.mod h1:HEWGJkRDzjJY2sqdDwxccsGicWEf9BQOZsq2tV+xzM0=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5 h1:lrdPtrORjGv1HbbEvKWDUAy97mPpFm4B8hp77tcCUJY=
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
Expand All @@ -221,6 +220,7 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kyoh86/exportloopref v0.1.7 h1:u+iHuTbkbTS2D/JP7fCuZDo/t3rBVGo3Hf58Rc+lQVY=
github.com/kyoh86/exportloopref v0.1.7/go.mod h1:h1rDl2Kdj97+Kwh4gdz3ujE7XHmH51Q0lUiZ1z4NLj8=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand All @@ -239,6 +239,7 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -493,7 +494,6 @@ golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190110163146-51295c7ec13a/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down Expand Up @@ -526,6 +526,7 @@ golang.org/x/tools v0.0.0-20200324003944-a576cf524670/go.mod h1:Sl4aGygMT6LrqrWc
golang.org/x/tools v0.0.0-20200414032229-332987a829c3/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200625211823-6506e20df31f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200626171337-aa94e735be7f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200701041122-1837592efa10/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand Down
4 changes: 3 additions & 1 deletion pkg/commands/executor.go
Expand Up @@ -205,7 +205,9 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {
configData.WriteString("\nbuild-tags=%s" + strings.Join(cfg.Run.BuildTags, ","))

h := sha256.New()
h.Write(configData.Bytes()) //nolint:errcheck
if _, err := h.Write(configData.Bytes()); err != nil {
return nil, err
}
return h.Sum(nil), nil
}

Expand Down
52 changes: 32 additions & 20 deletions pkg/golinters/errcheck.go
Expand Up @@ -10,9 +10,10 @@ import (
"strings"
"sync"

errcheck "github.com/golangci/errcheck/golangci"
"github.com/kisielk/errcheck/errcheck"
"github.com/pkg/errors"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/fsutils"
Expand All @@ -36,17 +37,23 @@ func NewErrcheck() *goanalysis.Linter {
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
// copied from errcheck
checker, err := getChecker(&lintCtx.Settings().Errcheck)
if err != nil {
panic(err.Error())
}
checker.Verbose = lintCtx.Cfg.Run.IsVerbose
checker.Tags = lintCtx.Cfg.Run.BuildTags

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
prog := goanalysis.MakeFakeLoaderProgram(pass)
errCfg, err := genConfig(&lintCtx.Settings().Errcheck)
if err != nil {
return nil, err
}
errcheckIssues, err := errcheck.RunWithConfig(prog, errCfg)
if err != nil {
return nil, err
pkg := &packages.Package{
Fset: pass.Fset,
Syntax: pass.Files,
Types: pass.Pkg,
TypesInfo: pass.TypesInfo,
}

errcheckIssues := checker.CheckPackage(pkg)
if len(errcheckIssues) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -104,27 +111,32 @@ func parseIgnoreConfig(s string) (map[string]*regexp.Regexp, error) {
return cfg, nil
}

func genConfig(errCfg *config.ErrcheckSettings) (*errcheck.Config, error) {
func getChecker(errCfg *config.ErrcheckSettings) (*errcheck.Checker, error) {
checker := errcheck.NewChecker()
checker.Blank = errCfg.CheckAssignToBlank
checker.Asserts = errCfg.CheckTypeAssertions

ignoreConfig, err := parseIgnoreConfig(errCfg.Ignore)
if err != nil {
return nil, errors.Wrap(err, "failed to parse 'ignore' directive")
}

c := &errcheck.Config{
Ignore: ignoreConfig,
Blank: errCfg.CheckAssignToBlank,
Asserts: errCfg.CheckTypeAssertions,
checker.Ignore = map[string]*regexp.Regexp{}
for pkg, re := range ignoreConfig {
checker.Ignore[pkg] = re
}
checker.UpdateNonVendoredIgnore()

checker.AddExcludes(errcheck.DefaultExcludes)
if errCfg.Exclude != "" {
exclude, err := readExcludeFile(errCfg.Exclude)
if err != nil {
return nil, err
}
c.Exclude = exclude
checker.AddExcludes(exclude)
}

return c, nil
return checker, nil
}

func getFirstPathArg() string {
Expand Down Expand Up @@ -192,7 +204,7 @@ func setupConfigFileSearch(name string) []string {
return configSearchPaths
}

func readExcludeFile(name string) (map[string]bool, error) {
func readExcludeFile(name string) ([]string, error) {
var err error
var fh *os.File

Expand All @@ -206,12 +218,12 @@ func readExcludeFile(name string) (map[string]bool, error) {
return nil, errors.Wrapf(err, "failed reading exclude file: %s", name)
}
scanner := bufio.NewScanner(fh)
exclude := make(map[string]bool)
excludes := []string{}
for scanner.Scan() {
exclude[scanner.Text()] = true
excludes = append(excludes, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrapf(err, "failed scanning file: %s", name)
}
return exclude, nil
return excludes, nil
}
4 changes: 3 additions & 1 deletion scripts/expand_website_templates/main.go
Expand Up @@ -54,7 +54,9 @@ func updateStateFile(replacements map[string]string) error {
}

h := sha256.New()
h.Write(replBytes) //nolint:errcheck
if _, err := h.Write(replBytes); err != nil {
return err
}

var contentBuf bytes.Buffer
contentBuf.WriteString("This file stores hash of website templates to trigger " +
Expand Down
4 changes: 2 additions & 2 deletions test/run_test.go
Expand Up @@ -135,7 +135,7 @@ func TestSortedResults(t *testing.T) {
"--sort-results=false",
strings.Join([]string{
"testdata/sort_results/main.go:12:5: `db` is unused (deadcode)",
"testdata/sort_results/main.go:15:13: Error return value of `returnError` is not checked (errcheck)",
"testdata/sort_results/main.go:15:13: Error return value is not checked (errcheck)",
"testdata/sort_results/main.go:8:6: func `returnError` is unused (unused)",
}, "\n"),
},
Expand All @@ -144,7 +144,7 @@ func TestSortedResults(t *testing.T) {
strings.Join([]string{
"testdata/sort_results/main.go:8:6: func `returnError` is unused (unused)",
"testdata/sort_results/main.go:12:5: `db` is unused (deadcode)",
"testdata/sort_results/main.go:15:13: Error return value of `returnError` is not checked (errcheck)",
"testdata/sort_results/main.go:15:13: Error return value is not checked (errcheck)",
}, "\n"),
},
}
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/errcheck.go
Expand Up @@ -12,7 +12,7 @@ func RetErr() error {
}

func MissedErrorCheck() {
RetErr() // ERROR "Error return value of `RetErr` is not checked"
RetErr() // ERROR "Error return value is not checked"
}

func IgnoreCloseMissingErrHandling() error {
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/errcheck_exclude.go
Expand Up @@ -13,6 +13,6 @@ func TestErrcheckExclude() []byte {
}

func TestErrcheckNoExclude() []byte {
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value of `ioutil.ReadAll` is not checked"
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value is not checked"
return ret
}
4 changes: 2 additions & 2 deletions test/testdata/errcheck_ignore.go
Expand Up @@ -13,7 +13,7 @@ func TestErrcheckIgnoreOs() {
}

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

Expand All @@ -23,6 +23,6 @@ func TestErrcheckIgnoreIoutil() []byte {
}

func TestErrcheckNoIgnoreIoutil() []byte {
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value of `ioutil.ReadAll` is not checked"
ret, _ := ioutil.ReadAll(nil) // ERROR "Error return value is not checked"
return ret
}
9 changes: 8 additions & 1 deletion test/testdata/errcheck_ignore_default.go
Expand Up @@ -3,15 +3,22 @@
package testdata

import (
"crypto/sha256"
"fmt"
"os"
)

func TestErrcheckIgnoreHashWriteByDefault() []byte {
h := sha256.New()
h.Write([]byte("food"))
return h.Sum(nil)
}

func TestErrcheckIgnoreFmtByDefault(s string) int {
n, _ := fmt.Println(s)
return n
}

func TestErrcheckNoIgnoreOs() {
_, _ = os.Open("f.txt") // ERROR "Error return value of `os.Open` is not checked"
_, _ = os.Open("f.txt") // ERROR "Error return value is not checked"
}
7 changes: 7 additions & 0 deletions test/testdata/errcheck_type_assertions.go
@@ -0,0 +1,7 @@
//args: -Eerrcheck
//config: linters-settings.errcheck.check-type-assertions=true
package testdata

func ErrorTypeAssertion(filter map[string]interface{}) bool {
return filter["messages_sent.messageid"].(map[string]interface{})["$ne"] != nil
}

0 comments on commit d770f83

Please sign in to comment.