From 58459d5a90964a8d08ff7f79b259fdbe6e59fdf4 Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Tue, 16 Aug 2022 17:54:16 +0300 Subject: [PATCH 01/12] linters --- linters/.golangci.yml | 117 +++++++++++++++++++++++++++++++ linters/Makefile | 8 +++ linters/errs/common.go | 13 ++++ linters/errs/errcheck.go | 5 ++ linters/errs/ineffassign.go | 11 +++ linters/errs/ineffassign_test.go | 12 ++++ linters/errs/staticcheck.go | 7 ++ linters/errs/staticcheck_test.go | 12 ++++ linters/go.mod | 11 +++ linters/go.sum | 15 ++++ 10 files changed, 211 insertions(+) create mode 100644 linters/.golangci.yml create mode 100644 linters/Makefile create mode 100644 linters/errs/common.go create mode 100644 linters/errs/errcheck.go create mode 100644 linters/errs/ineffassign.go create mode 100644 linters/errs/ineffassign_test.go create mode 100644 linters/errs/staticcheck.go create mode 100644 linters/errs/staticcheck_test.go create mode 100644 linters/go.mod create mode 100644 linters/go.sum diff --git a/linters/.golangci.yml b/linters/.golangci.yml new file mode 100644 index 0000000..b022732 --- /dev/null +++ b/linters/.golangci.yml @@ -0,0 +1,117 @@ +linters: + # Please, do not use `enable-all`: it's deprecated and will be removed soon. + # Inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint. + # Full list of linters - https://golangci-lint.run/usage/linters + disable-all: true + enable: + - bodyclose # https://github.com/timakin/bodyclose +# - deadcode + - depguard + - errcheck # Mandatory. Do not disable. + - gocritic + - goimports + - gosec + - gosimple + - govet + - noctx + - nolintlint + - ineffassign # Mandatory. Do not disable. + - staticcheck # Mandatory. Do not disable. + - stylecheck + - structcheck + - typecheck +# - unused + - varcheck + +# Other linters: +# - dogsled +# - dupl +# - exportloopref +# - exhaustive # e.g. missing cases in switch of type +# - funlen +# - gochecknoinits +# - gocognit +# - goconst +# - gocyclo +# - goerr113 +# - gofmt +# - goprintffuncname +# - lll +# - misspell +# - nakedret +# - nlreturn +# - prealloc +# - revive +# - rowserrcheck +# - stylecheck +# - unconvert +# - unparam + +linters-settings: + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - commentedOutCode + - dupImport # https://github.com/go-critic/go-critic/issues/845 + - octalLiteral + - wrapperFunc + - whyNoLint + - hugeParam # TODO: configure(80 bytes is probably not so much) and enable. + + errcheck: + # 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.Closer).Close + - (io.ReadCloser).Close + + govet: + enable-all: true + disable: + - shadow + - fieldalignment + + depguard: + # Kind of list is passed in. + # Allowed values: allowlist|denylist + # Default: denylist + list-type: denylist + # Check the list against standard lib. + # Default: false + include-go-root: true + # A list of packages for the list type specified. + # Default: [] + packages: + - github.com/pkg/errors + # A list of packages for the list type specified. + # Specify an error message to output when a denied package is used. + # Default: [] + packages-with-error-message: + - github.com/pkg/errors: 'Deprecated: use standard "errors" instead.' + + stylecheck: + # Select the Go version to target. + # Default: 1.13 + go: "1.17" + # https://staticcheck.io/docs/options#checks + checks: ["all"] + +issues: + # Maximum issues count per one linter. Set to 0 to disable. Default is 50. + max-issues-per-linter: 0 + + # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. + max-same-issues: 50 + +run: + # include test files or not, default is true + tests: true + + # Timeout for analysis, e.g. 30s, 5m. + # Default: 1m + timeout: 5m diff --git a/linters/Makefile b/linters/Makefile new file mode 100644 index 0000000..e126847 --- /dev/null +++ b/linters/Makefile @@ -0,0 +1,8 @@ +# linter: +GOLINT = $(GOPATH)/bin/golangci-lint +$(GOLINT): + curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.48.0 + +.PHONY: lint +lint: $(GOLINT) + $(GOLINT) run diff --git a/linters/errs/common.go b/linters/errs/common.go new file mode 100644 index 0000000..c893b09 --- /dev/null +++ b/linters/errs/common.go @@ -0,0 +1,13 @@ +package errs + +import ( + "errors" +) + +func fnWithError() error { + return errors.New("some error") +} + +func fnNoError() error { + return nil +} diff --git a/linters/errs/errcheck.go b/linters/errs/errcheck.go new file mode 100644 index 0000000..619c3b1 --- /dev/null +++ b/linters/errs/errcheck.go @@ -0,0 +1,5 @@ +package errs + +func errcheck() { + fnWithError() +} diff --git a/linters/errs/ineffassign.go b/linters/errs/ineffassign.go new file mode 100644 index 0000000..701c6f0 --- /dev/null +++ b/linters/errs/ineffassign.go @@ -0,0 +1,11 @@ +package errs + +func ineffassign() error { + err := fnWithError() + err = fnNoError() + if err != nil { + return err + } + + return nil +} diff --git a/linters/errs/ineffassign_test.go b/linters/errs/ineffassign_test.go new file mode 100644 index 0000000..136cc46 --- /dev/null +++ b/linters/errs/ineffassign_test.go @@ -0,0 +1,12 @@ +package errs + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_ineffassign(t *testing.T) { + err := ineffassign() + require.Error(t, err) +} diff --git a/linters/errs/staticcheck.go b/linters/errs/staticcheck.go new file mode 100644 index 0000000..c0e7e8d --- /dev/null +++ b/linters/errs/staticcheck.go @@ -0,0 +1,7 @@ +package errs + +func staticcheck() (err error) { + err = fnWithError() + err = fnNoError() + return +} diff --git a/linters/errs/staticcheck_test.go b/linters/errs/staticcheck_test.go new file mode 100644 index 0000000..e0f8ab5 --- /dev/null +++ b/linters/errs/staticcheck_test.go @@ -0,0 +1,12 @@ +package errs + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_staticcheck(t *testing.T) { + err := staticcheck() + require.Error(t, err) +} diff --git a/linters/go.mod b/linters/go.mod new file mode 100644 index 0000000..e563be7 --- /dev/null +++ b/linters/go.mod @@ -0,0 +1,11 @@ +module linters + +go 1.17 + +require github.com/stretchr/testify v1.8.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/linters/go.sum b/linters/go.sum new file mode 100644 index 0000000..5164829 --- /dev/null +++ b/linters/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 3e2079ebb40f88e74a4cab582be16712f63b8d34 Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Tue, 16 Aug 2022 17:58:01 +0300 Subject: [PATCH 02/12] added config --- .github/workflows/lint.yaml | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 .github/workflows/lint.yaml diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..a34ee44 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,44 @@ +name: Lint + +on: + push: + branches: + - master + - main + pull_request: + +jobs: + lint: + name: lint + strategy: + matrix: + os: [ ubuntu-latest ] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version-file: ./go.mod + + - name: Cache/restore build and pkg cache + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.48 + + skip-cache: true + + # Optional: working directory, useful for monorepos + working-directory: linters From 0f815db5ee1d1ef1788e66c5fda042fc132de69d Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Tue, 16 Aug 2022 17:59:05 +0300 Subject: [PATCH 03/12] enabled --- linters/.golangci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linters/.golangci.yml b/linters/.golangci.yml index b022732..6f74f44 100644 --- a/linters/.golangci.yml +++ b/linters/.golangci.yml @@ -5,7 +5,7 @@ linters: disable-all: true enable: - bodyclose # https://github.com/timakin/bodyclose -# - deadcode + - deadcode - depguard - errcheck # Mandatory. Do not disable. - gocritic @@ -20,7 +20,7 @@ linters: - stylecheck - structcheck - typecheck -# - unused + - unused - varcheck # Other linters: From 281c162d3ec798fb6c7bed768635074b05982edc Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Tue, 16 Aug 2022 18:00:07 +0300 Subject: [PATCH 04/12] fix --- .github/workflows/lint.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index a34ee44..8661fd8 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -21,7 +21,7 @@ jobs: - name: Install Go uses: actions/setup-go@v3 with: - go-version-file: ./go.mod + go-version-file: ./linters/go.mod - name: Cache/restore build and pkg cache uses: actions/cache@v3 From 71f1266f8729b6203f9e950421cc3c70f736504d Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Tue, 16 Aug 2022 18:03:09 +0300 Subject: [PATCH 05/12] revert --- linters/.golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linters/.golangci.yml b/linters/.golangci.yml index 6f74f44..1216e12 100644 --- a/linters/.golangci.yml +++ b/linters/.golangci.yml @@ -5,7 +5,7 @@ linters: disable-all: true enable: - bodyclose # https://github.com/timakin/bodyclose - - deadcode +# - deadcode - depguard - errcheck # Mandatory. Do not disable. - gocritic From b62c4d0785621498ae15450684afa7fed773abd2 Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Tue, 16 Aug 2022 18:04:03 +0300 Subject: [PATCH 06/12] errcheck --- linters/errs/errcheck.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/linters/errs/errcheck.go b/linters/errs/errcheck.go index 619c3b1..11016f8 100644 --- a/linters/errs/errcheck.go +++ b/linters/errs/errcheck.go @@ -1,5 +1,7 @@ package errs -func errcheck() { +func errcheck() error { fnWithError() + + return nil } From 5a0d83f4f8845a9021f3e644d2c529151234fa2b Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Tue, 16 Aug 2022 18:04:19 +0300 Subject: [PATCH 07/12] revert --- linters/.golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linters/.golangci.yml b/linters/.golangci.yml index 1216e12..b022732 100644 --- a/linters/.golangci.yml +++ b/linters/.golangci.yml @@ -20,7 +20,7 @@ linters: - stylecheck - structcheck - typecheck - - unused +# - unused - varcheck # Other linters: From 692d9fdef8ef98fd5d61e007f1c7523d55077658 Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Fri, 26 Aug 2022 13:38:54 +0300 Subject: [PATCH 08/12] removed deprecated --- linters/.golangci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/linters/.golangci.yml b/linters/.golangci.yml index b022732..bf7c99a 100644 --- a/linters/.golangci.yml +++ b/linters/.golangci.yml @@ -5,7 +5,6 @@ linters: disable-all: true enable: - bodyclose # https://github.com/timakin/bodyclose -# - deadcode - depguard - errcheck # Mandatory. Do not disable. - gocritic @@ -18,10 +17,8 @@ linters: - ineffassign # Mandatory. Do not disable. - staticcheck # Mandatory. Do not disable. - stylecheck - - structcheck - typecheck # - unused - - varcheck # Other linters: # - dogsled From 1acab899eb35cd7aa81f21a5f6991eb9a5865028 Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Fri, 26 Aug 2022 14:51:05 +0300 Subject: [PATCH 09/12] ineffassign --- linters/ineffassign/ineffassign.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 linters/ineffassign/ineffassign.go diff --git a/linters/ineffassign/ineffassign.go b/linters/ineffassign/ineffassign.go new file mode 100644 index 0000000..1d23d23 --- /dev/null +++ b/linters/ineffassign/ineffassign.go @@ -0,0 +1,17 @@ +package ineffassign + +func storeCounter(counter int) { + _ = counter +} + +func ineffassign() { + var counter int + + // do something + + counter++ + storeCounter(counter) + + // do something else + counter++ +} From a3f63dbd34e83a5f0a5d431f59a9a63ce22100ab Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Fri, 26 Aug 2022 14:52:38 +0300 Subject: [PATCH 10/12] better naming --- linters/ineffassign/ineffassign.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linters/ineffassign/ineffassign.go b/linters/ineffassign/ineffassign.go index 1d23d23..e7b3766 100644 --- a/linters/ineffassign/ineffassign.go +++ b/linters/ineffassign/ineffassign.go @@ -4,7 +4,7 @@ func storeCounter(counter int) { _ = counter } -func ineffassign() { +func fn() { var counter int // do something From aaa5a534889bca2b1b5a9c6e2e1c9f64443e97b4 Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Fri, 26 Aug 2022 15:21:36 +0300 Subject: [PATCH 11/12] removed redundant --- linters/.golangci.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/linters/.golangci.yml b/linters/.golangci.yml index bf7c99a..0409d76 100644 --- a/linters/.golangci.yml +++ b/linters/.golangci.yml @@ -53,11 +53,8 @@ linters-settings: - performance - style disabled-checks: - - commentedOutCode - dupImport # https://github.com/go-critic/go-critic/issues/845 - - octalLiteral - - wrapperFunc - - whyNoLint + - whyNoLint # checked by nolintlint - hugeParam # TODO: configure(80 bytes is probably not so much) and enable. errcheck: From 22fd7f0872dd79085622878bb7b920e4f9b6b8d5 Mon Sep 17 00:00:00 2001 From: vtopc <32271530+vtopc@users.noreply.github.com> Date: Fri, 30 Sep 2022 22:30:09 +0300 Subject: [PATCH 12/12] staticcheck2 --- linters/errs/staticcheck.go | 7 ++++++- linters/errs/staticcheck_test.go | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/linters/errs/staticcheck.go b/linters/errs/staticcheck.go index c0e7e8d..8f989a7 100644 --- a/linters/errs/staticcheck.go +++ b/linters/errs/staticcheck.go @@ -1,7 +1,12 @@ package errs -func staticcheck() (err error) { +func staticcheck1() (err error) { err = fnWithError() err = fnNoError() return } + +func staticcheck2() (err error) { + err = fnNoError() + return fnWithError() +} diff --git a/linters/errs/staticcheck_test.go b/linters/errs/staticcheck_test.go index e0f8ab5..67ea07b 100644 --- a/linters/errs/staticcheck_test.go +++ b/linters/errs/staticcheck_test.go @@ -7,6 +7,6 @@ import ( ) func Test_staticcheck(t *testing.T) { - err := staticcheck() + err := staticcheck1() require.Error(t, err) }