Skip to content

Commit

Permalink
updated golangci-lint config, fixed linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
na4ma4 committed Feb 27, 2024
1 parent 4e09a82 commit 89f1645
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
1 change: 1 addition & 0 deletions .github/workflows/automerge.yml
Expand Up @@ -5,4 +5,5 @@ on:

jobs:
dependabot:
name: "Dependabot"
uses: na4ma4/actions/.github/workflows/dependabot-automerge.yml@main
84 changes: 61 additions & 23 deletions .golangci.yml
@@ -1,12 +1,14 @@
# This code is licensed under the terms of the MIT license.
# This code is licensed under the terms of the MIT license https://opensource.org/license/mit
# Copyright (c) 2021 Marat Reymers

## Golden config for golangci-lint v1.51.2
## Golden config for golangci-lint v1.55.2
#
# This is the best config for golangci-lint based on my experience and opinion.
# It is very strict, but not extremely strict.
# Feel free to adopt and change it for your needs.
# Feel free to adapt and change it for your needs.
#
# Imported from https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322
# Modified for na4ma4 repositories.
#

run:
Expand Down Expand Up @@ -65,6 +67,9 @@ linters-settings:
- "^github.com/prometheus/client_golang/.+Opts$"
- "^github.com/spf13/cobra.Command$"
- "^github.com/spf13/cobra.CompletionOptions$"
- "^github.com/stretchr/testify/mock.Mock$"
- "^github.com/testcontainers/testcontainers-go.+Request$"
- "^github.com/testcontainers/testcontainers-go.FromDockerfile$"
- "^golang.org/x/tools/go/analysis.Analyzer$"
- "^google.golang.org/protobuf/.+Options$"
- "^gopkg.in/yaml.v3.Node$"
Expand All @@ -78,6 +83,9 @@ linters-settings:
# If lower than 0, disable the check.
# Default: 40
statements: 50
# Ignore comments when counting lines.
# Default false
ignore-comments: true

gocognit:
# Minimal code complexity to report.
Expand All @@ -99,26 +107,32 @@ linters-settings:
skipRecvDeref: false

gomnd:
# List of numbers to exclude from analysis.
# The numbers should be written as string.
# Values always ignored: "1", "1.0", "0" and "0.0"
# Default: []
ignored-numbers:
- '2'
# List of function patterns to exclude from analysis.
# Values always ignored: `time.Date`,
# `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`,
# `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`.
# Default: []
ignored-functions:
- flag.Arg
- flag.Duration.*
- flag.Float.*
- flag.Int.*
- flag.Uint.*
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.Mkdir.*
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.ExponentialBuckets.*
- prometheus.LinearBuckets
- tabwriter.NewWriter
- cobra.MinimumNArgs
- cobra.MaximumNArgs
- cobra.ExactArgs
- cobra.RangeArgs

gomoddirectives:
replace-allow-list:
- k8s.io/kube-openapi

gomodguard:
blocked:
Expand Down Expand Up @@ -152,7 +166,7 @@ linters-settings:
shadow:
# Whether to be strict about shadowing; can be noisy.
# Default: false
strict: false
strict: true

nakedret:
# Make an issue if func has more lines of code than this setting, and it has naked returns.
Expand Down Expand Up @@ -182,11 +196,6 @@ linters-settings:
# Default: false
all: true

staticcheck:
# Disable SA1019 check for now (openpgp is deprecated).
checks:
- "all"
- "-SA1019"

linters:
disable-all: true
Expand Down Expand Up @@ -217,6 +226,7 @@ linters:
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
- gochecknoglobals # checks that no global variables exist
- gochecknoinits # checks that no init functions are present in Go code
- gochecksumtype # checks exhaustiveness on Go "sum types"
- gocognit # computes and checks the cognitive complexity of functions
- goconst # finds repeated strings that could be replaced by a constant
- gocritic # provides diagnostics that check for bugs, performance and style issues
Expand All @@ -231,6 +241,7 @@ linters:
- lll # reports long lines
- loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
- makezero # finds slice declarations with non-zero initial length
- mirror # reports wrong mirror patterns of bytes/strings usage
- musttag # enforces field tags in (un)marshaled structs
- nakedret # finds naked returns in functions greater than a specified function length
- nestif # reports deeply nested if statements
Expand All @@ -240,15 +251,19 @@ linters:
- nolintlint # reports ill-formed or insufficient nolint directives
- nonamedreturns # reports all named returns
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
- perfsprint # checks that fmt.Sprintf can be replaced with a faster alternative
- predeclared # finds code that shadows one of Go's predeclared identifiers
- promlinter # checks Prometheus metrics naming via promlint
- protogetter # reports direct reads from proto message fields when getters should be used
- reassign # checks that package variables are not reassigned
- revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
- rowserrcheck # checks whether Err of rows is checked successfully
- sloglint # ensure consistent code style when using log/slog
- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
- stylecheck # is a replacement for golint
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
- testableexamples # checks if examples are testable (have an expected output)
- testifylint # checks usage of github.com/stretchr/testify
- testpackage # makes you use a separate _test package
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- unconvert # removes unnecessary type conversions
Expand All @@ -264,11 +279,14 @@ linters:
#- ginkgolinter # [if you use ginkgo/gomega] enforces standards of using ginkgo and gomega
#- godox # detects FIXME, TODO and other comment keywords
#- goheader # checks is file header matches to pattern
#- inamedparam # [great idea, but too strict, need to ignore a lot of cases by default] reports interfaces with unnamed method parameters
#- interfacebloat # checks the number of methods inside an interface
#- ireturn # accept interfaces, return concrete types
#- prealloc # [premature optimization, but can be used in some cases] finds slice declarations that could potentially be preallocated
#- tagalign # checks that struct tags are well aligned
#- varnamelen # [great idea, but too many false positives] checks that the length of a variable's name matches its scope
#- wrapcheck # checks that errors returned from external packages are wrapped
#- zerologlint # detects the wrong usage of zerolog that a user forgets to dispatch zerolog.Event

## disabled
#- containedctx # detects struct contained context.Context field
Expand All @@ -281,6 +299,7 @@ linters:
#- goerr113 # [too strict] checks the errors handling expressions
#- gofmt # [replaced by goimports] checks whether code was gofmt-ed
#- gofumpt # [replaced by goimports, gofumports is not available yet] checks whether code was gofumpt-ed
#- gosmopolitan # reports certain i18n/l10n anti-patterns in your Go codebase
#- grouper # analyzes expression groups
#- importas # enforces consistent import aliases
#- maintidx # measures the maintainability index of each function
Expand Down Expand Up @@ -311,14 +330,10 @@ issues:
max-same-issues: 50

exclude-rules:
- source: "^//\\s*go:generate\\s"
linters: [ lll ]
- source: "(noinspection|TODO)"
linters: [ godot ]
- source: "//noinspection"
linters: [ gocritic ]
- source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {"
linters: [ errorlint ]
- path: "_test\\.go"
linters:
- bodyclose
Expand All @@ -328,3 +343,26 @@ issues:
- gosec
- noctx
- wrapcheck
- linters: [ staticcheck ]
text: "options.AndFrom is deprecated"
- linters: [ staticcheck ]
text: "The component config package has been deprecated and will be removed in a future release."
- path: "cmd/"
linters:
- gochecknoinits
- gochecknoglobals
# Exclude `lll` issues for long lines with `go:generate`.
- linters:
- lll
source: '^//\s*\+kubebuilder:'
- linters:
- godot
source: '^//\s*\+optional'
- path: "api/v1/"
linters:
- gochecknoinits
# Allow dot imports for ginkgo and gomega
- source: ginkgo|gomega
linters:
- revive
text: "should not use dot imports"
2 changes: 1 addition & 1 deletion handler_test.go
Expand Up @@ -18,7 +18,7 @@ func ExampleLoggingHTTPHandler() {

loggedRouter := zaptool.LoggingHTTPHandler(
logger,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
// do nothing
}),
zaptool.LoggingOptionTimestamp(false),
Expand Down
2 changes: 1 addition & 1 deletion loglevels.go
Expand Up @@ -154,7 +154,7 @@ func (a *LogLevels) String() string {

for k, v := range a.levels {
if v.Level() == zapcore.InvalidLevel {
out = append(out, fmt.Sprintf("%s:invalid", k))
out = append(out, k+":invalid")
continue
}

Expand Down

0 comments on commit 89f1645

Please sign in to comment.