From 5bfc9aba9f028d6cf584081b8dc16f4b0cfe0356 Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Fri, 19 Sep 2025 15:46:30 +0200 Subject: [PATCH] Disable unwanted gocritic checks instead of filtering them The golangci-lint docs indicate that, for gocritic, disabled-checks and enabled-checks can't be combined. That is however not accurate; enabled-checks adds checks, disabled-checks disables them, in that order. If disable-all isn't set, then the setup starts with the checks enabled by default, adds any additional checks specified in enabled-checks, and removes checks specified in disabled-checks. This means that instead of filtering unwanted checks, we can disable them entirely. This makes the set of disabled checks easier to read, and perhaps easier to clean up in future. For singleCaseSwitch and wrapperFunc, we only want to ignore specific instances, so this is still done by filtering. Signed-off-by: Stephen Kitt --- hack/golangci.yaml | 27 +++++++++++++++++++-------- hack/golangci.yaml.in | 27 +++++++++++++++++++-------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/hack/golangci.yaml b/hack/golangci.yaml index 151cde238c9..4595be7c9a1 100644 --- a/hack/golangci.yaml +++ b/hack/golangci.yaml @@ -126,12 +126,7 @@ linters: - linters: - gocritic - text: "append result not assigned to the same slice|put a space between `//` and comment text|sloppyLen|elseif|should rewrite switch statement to if statement|regexpMust|wrapperFunc: use strings.ReplaceAll|singleCaseSwitch|deprecatedComment|exitAfterDefer|captLocal|unlambda|underef|unslice|valSwap|typeSwitchVar" - - # https://github.com/kubernetes/kubernetes/issues/117288#issuecomment-1507008918 - - linters: - - gocritic - text: "assignOp:" + text: "wrapperFunc: use strings.ReplaceAll|should rewrite switch statement to if statement" # Kube-API-Linter should only be run on the API definitions - linters: @@ -391,9 +386,25 @@ linters: pkg: ^k8s\.io/component-base/featuregate$ msg: should not use Add, use AddVersioned instead gocritic: - enabled-checks: - - equalFold + enabled-checks: # These are in addition to the default checks - see https://golangci-lint.run/docs/linters/configuration/#gocritic - boolExprSimplify + - equalFold + disabled-checks: # This disables checks that are enabled by default (this can be combined with enabled-checks, contrary to the docs) + - appendAssign + - assignOp # https://github.com/kubernetes/kubernetes/issues/117288#issuecomment-1507008918 + - captLocal + - commentFormatting + - deprecatedComment + - elseif + # The following have few (single-digit) occurrences left in k/k and wouldn't be too onerous to enable + - exitAfterDefer + - regexpMust + - sloppyLen + - typeSwitchVar + - underef + - unlambda + - unslice + - valSwap revive: # Only these rules are enabled. rules: diff --git a/hack/golangci.yaml.in b/hack/golangci.yaml.in index 95d837d0f16..4a2e6489f9b 100644 --- a/hack/golangci.yaml.in +++ b/hack/golangci.yaml.in @@ -141,12 +141,7 @@ linters: - linters: - gocritic - text: "append result not assigned to the same slice|put a space between `//` and comment text|sloppyLen|elseif|should rewrite switch statement to if statement|regexpMust|wrapperFunc: use strings.ReplaceAll|singleCaseSwitch|deprecatedComment|exitAfterDefer|captLocal|unlambda|underef|unslice|valSwap|typeSwitchVar" - - # https://github.com/kubernetes/kubernetes/issues/117288#issuecomment-1507008918 - - linters: - - gocritic - text: "assignOp:" + text: "wrapperFunc: use strings.ReplaceAll|should rewrite switch statement to if statement" {{- end}} @@ -238,9 +233,25 @@ linters: {{- end}} {{- if .Base }} gocritic: - enabled-checks: - - equalFold + enabled-checks: # These are in addition to the default checks - see https://golangci-lint.run/docs/linters/configuration/#gocritic - boolExprSimplify + - equalFold + disabled-checks: # This disables checks that are enabled by default (this can be combined with enabled-checks, contrary to the docs) + - appendAssign + - assignOp # https://github.com/kubernetes/kubernetes/issues/117288#issuecomment-1507008918 + - captLocal + - commentFormatting + - deprecatedComment + - elseif + # The following have few (single-digit) occurrences left in k/k and wouldn't be too onerous to enable + - exitAfterDefer + - regexpMust + - sloppyLen + - typeSwitchVar + - underef + - unlambda + - unslice + - valSwap {{- end}} revive: # Only these rules are enabled.