From 7389573e23d72efb1112ed6ef3567dcadb5f21ea Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Mon, 24 Mar 2025 17:35:22 +0000 Subject: [PATCH] Update golangci-lint config to v2 --- .github/workflows/CI.yml | 2 +- .golangci.yml | 255 +++++++++++++++++++----------------- Taskfile.yml | 4 - examples/subcommands/cli.go | 2 +- 4 files changed, 139 insertions(+), 124 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 21a46ca..aa1cda6 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -17,4 +17,4 @@ jobs: name: CI permissions: contents: read - uses: FollowTheProcess/ci/.github/workflows/Go.yml@v1 + uses: FollowTheProcess/ci/.github/workflows/Go.yml@v2 diff --git a/.golangci.yml b/.golangci.yml index 370917a..189d4ba 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,16 @@ +version: "2" + +formatters: + enable: + - gofumpt + - goimports + + settings: + gofumpt: + extra-rules: true + linters: - enable-all: true + default: all disable: - decorder # Don't care about this - dupl # Basically every table driven test ever triggers this @@ -8,9 +19,9 @@ linters: - exhaustruct # No - forbidigo # Nothing to forbid - funlen # Bad metric for complexity - - gci # gofmt/goimports is fine - ginkgolinter # I don't use whatever this is - gochecknoglobals # Globals are fine sometimes, use common sense + - gocognit # Cmplexity with another name, don't need both - gocyclo # cyclop does this instead - godox # "todo" and "fixme" comments are allowed - goheader # No need @@ -22,125 +33,133 @@ linters: - nestif # cyclop does this - nlreturn # Similar to wsl, I think best left to judgement - nonamedreturns # Named returns are often helpful, it's naked returns that are the issue - - paralleltest # No need - - tenv # Replaced by usetesting - - thelper # Lots of false positives due to the way I do table tests in here + - paralleltest # I've never had Go tests take longer than a few seconds, it's fine + - thelper # Lots of false positives in here due to the way I do table tests + - unparam # gopls is better and more subtle - varnamelen # Lots of false positives of things that are fine - wrapcheck # Not every error must be wrapped - wsl # Very aggressive, some of this I like but tend to do anyway -issues: - exclude-rules: - - path: _test\.go - linters: - - prealloc # These kinds of optimisations will make no difference to test code - -linters-settings: - cyclop: - max-complexity: 20 - - depguard: + exclusions: + presets: + # See https://golangci-lint.run/usage/false-positives/#exclusion-presets + - comments # Revive in particular has lots of false positives + - std-error-handling + - common-false-positives rules: - main: - deny: - - pkg: io/ioutil - desc: io/ioutil is deprecated, use io instead - - - pkg: "math/rand$" - desc: use math/rand/v2 instead - - errcheck: - check-type-assertions: true - check-blank: true - - exhaustive: - check: - - switch - - map - default-signifies-exhaustive: true - - staticcheck: - checks: - - all - - gosimple: - checks: - - all - - govet: - enable-all: true - - gofumpt: - extra-rules: true - - nakedret: - max-func-lines: 0 # Disallow any naked returns - - nolintlint: - allow-unused: false - require-explanation: true - require-specific: true - - usetesting: - context-background: true - context-todo: true - os-chdir: true - os-mkdir-temp: true - os-setenv: true - os-create-temp: true - os-temp-dir: true - - revive: - max-open-files: 256 - ignore-generated-header: true - enable-all-rules: true - rules: - - name: add-constant - disabled: true # goconst does this - - - name: argument-limit - arguments: - - 5 - - - name: cognitive-complexity - disabled: true # gocognit does this - - - name: comment-spacings - arguments: - - "nolint:" - - - name: cyclomatic - disabled: true # cyclop does this - - - name: exported - arguments: - - checkPrivateReceivers - - checkPublicInterface - - - name: function-length - disabled: true # Bad proxy for complexity - - - name: function-result-limit - arguments: - - 3 - - - name: import-shadowing - disabled: true # predeclared does this - - - name: line-length-limit - disabled: true # gofmt/golines handles this well enough - - - name: redefines-builtin-id - disabled: true # predeclared does this - - - name: unhandled-error - arguments: - - fmt\.(Fp|P)rint(ln|f)? - - strings.Builder.Write(String|Byte)? - - bytes.Buffer.Write(String|Byte)? - - - name: unused-parameter - disabled: true # Leaves a mess of _ in test cases etc. + - path: _test\.go + linters: + - prealloc # These kinds of optimisations will make no difference to test code + - gosec # Tests don't need security stuff + + settings: + cyclop: + max-complexity: 20 + + depguard: + rules: + main: + deny: + - pkg: io/ioutil + desc: io/ioutil is deprecated, use io instead + + - pkg: "math/rand$" + desc: use math/rand/v2 instead + + errcheck: + check-type-assertions: true + check-blank: true + + exhaustive: + check: + - switch + - map + default-signifies-exhaustive: true + + staticcheck: + checks: + - all + + gosec: + excludes: + - G104 # Errors not checked, handled by errcheck + + govet: + enable-all: true + + nakedret: + max-func-lines: 0 # Disallow any naked returns + + nolintlint: + allow-unused: false + require-explanation: true + require-specific: true + + usetesting: + context-background: true + context-todo: true + os-chdir: true + os-mkdir-temp: true + os-setenv: true + os-create-temp: true + os-temp-dir: true + + revive: + max-open-files: 256 + enable-all-rules: true + rules: + - name: add-constant + disabled: true # goconst does this + + - name: argument-limit + arguments: + - 5 + + - name: cognitive-complexity + disabled: true # gocognit does this + + - name: comment-spacings + arguments: + - "nolint:" + + - name: cyclomatic + disabled: true # cyclop does this + + - name: exported + arguments: + - checkPrivateReceivers + - checkPublicInterface + + - name: function-length + disabled: true # Bad proxy for complexity + + - name: function-result-limit + arguments: + - 3 + + - name: import-shadowing + disabled: true # predeclared does this + + - name: line-length-limit + disabled: true # gofmt/golines handles this well enough + + - name: max-public-structs + disabled: true # This is a dumb rule + + - name: redefines-builtin-id + disabled: true # predeclared does this + + - name: unhandled-error + arguments: + - fmt\.(Fp|P)rint(ln|f)? + - strings.Builder.Write(String|Byte)? + - bytes.Buffer.Write(String|Byte)? + + - name: flag-parameter + disabled: true # As far as I can work out this just doesn't like bools + + - name: unused-parameter + disabled: true # The gopls unused analyzer covers this better - - name: unused-receiver - disabled: true # As above + - name: unused-receiver + disabled: true # As above diff --git a/Taskfile.yml b/Taskfile.yml index 4213648..69d9548 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -26,12 +26,8 @@ tasks: desc: Run go fmt on all source files sources: - "**/*.go" - preconditions: - - sh: command -v golines - msg: golines not installed, see https://github.com/segmentio/golines cmds: - go fmt ./... - - golines . --ignore-generated --write-output --max-len 120 test: desc: Run the test suite diff --git a/examples/subcommands/cli.go b/examples/subcommands/cli.go index ebc30d0..a7e28ac 100644 --- a/examples/subcommands/cli.go +++ b/examples/subcommands/cli.go @@ -24,9 +24,9 @@ func BuildCLI() (*cli.Command, error) { type sayOptions struct { thing string + items []string count int shout bool - items []string } func buildSayCommand() (*cli.Command, error) {