Skip to content

Linters demo #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -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: ./linters/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
111 changes: 111 additions & 0 deletions linters/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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
- depguard
- errcheck # Mandatory. Do not disable.
- gocritic
- goimports
- gosec
- gosimple
- govet
- noctx
- nolintlint
- ineffassign # Mandatory. Do not disable.
- staticcheck # Mandatory. Do not disable.
- stylecheck
- typecheck
# - unused

# 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:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- whyNoLint # checked by nolintlint
- 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
8 changes: 8 additions & 0 deletions linters/Makefile
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions linters/errs/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package errs

import (
"errors"
)

func fnWithError() error {
return errors.New("some error")
}

func fnNoError() error {
return nil
}
7 changes: 7 additions & 0 deletions linters/errs/errcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package errs

func errcheck() error {
fnWithError()

return nil
}
11 changes: 11 additions & 0 deletions linters/errs/ineffassign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errs

func ineffassign() error {
err := fnWithError()
err = fnNoError()
if err != nil {
return err
}

return nil
}
12 changes: 12 additions & 0 deletions linters/errs/ineffassign_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 12 additions & 0 deletions linters/errs/staticcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package errs

func staticcheck1() (err error) {
err = fnWithError()
err = fnNoError()
return
}

func staticcheck2() (err error) {
err = fnNoError()
return fnWithError()
}
12 changes: 12 additions & 0 deletions linters/errs/staticcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package errs

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_staticcheck(t *testing.T) {
err := staticcheck1()
require.Error(t, err)
}
11 changes: 11 additions & 0 deletions linters/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
15 changes: 15 additions & 0 deletions linters/go.sum
Original file line number Diff line number Diff line change
@@ -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=
17 changes: 17 additions & 0 deletions linters/ineffassign/ineffassign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ineffassign

func storeCounter(counter int) {
_ = counter
}

func fn() {
var counter int

// do something

counter++
storeCounter(counter)

// do something else
counter++
}