From c52bcf89ea5f25b4fe1046395e7f60699fc87b38 Mon Sep 17 00:00:00 2001 From: Jacek Olszak Date: Sat, 20 Dec 2025 12:30:05 +0100 Subject: [PATCH] Fix golangci-lint after recent Go version update --- .github/workflows/build.yml | 14 +-- .github/workflows/codeql-analysis.yml | 2 +- .golangci.yaml | 108 ++++++++++++++------ adapter/console/console.go | 7 +- adapter/internal/adaptertest/adaptertest.go | 8 +- adapter/internal/benchmark/benchmark.go | 8 +- adapter/internal/fake/std.go | 5 +- adapter/logadapter/logadapter.go | 4 +- adapter/logfmt/logfmt.go | 6 +- adapter/logrusadapter/logrus.go | 2 +- adapter/logrusadapter/logrus_test.go | 2 +- adapter/zapadapter/zapadapter_test.go | 2 +- logger/global.go | 14 +-- logger/logger_concurrency_test.go | 4 +- logger/logger_test.go | 20 ++-- logger/noop.go | 2 +- 16 files changed, 126 insertions(+), 82 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8fd6d14..b301744 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,10 +14,10 @@ jobs: matrix: go: [ "1.19", "1.20" ] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v5 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v6 with: go-version: ${{ matrix.go }} @@ -30,10 +30,10 @@ jobs: coverage: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v5 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v6 with: go-version: 1.19 @@ -46,10 +46,10 @@ jobs: lint: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v5 - name: Lint - uses: golangci/golangci-lint-action@v2.5.2 + uses: golangci/golangci-lint-action@v9 with: args: "-v" - version: v1.45 + version: v2.6 diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 15c4a17..05b80f8 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -24,7 +24,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v5 - name: Initialize CodeQL uses: github/codeql-action/init@v1 diff --git a/.golangci.yaml b/.golangci.yaml index a84cb3e..287411a 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,32 +1,82 @@ -issues: - exclude-rules: - # Disable some linters in tests. - - path: _test\.go - linters: - - funlen - - maligned - - gomnd - - path: adapter/internal/adaptertest - linters: - - funlen - - maligned - - gomnd - +version: "2" linters: + default: none enable: + - asasalint + - asciicheck + - bidichk + - containedctx + - contextcheck + - cyclop + - decorder + - dogsled + - dupl + - durationcheck + - embeddedstructfieldcheck + - errcheck + - errchkjson + - errname + - errorlint + - exhaustive + - exptostd + - fatcontext + - forbidigo + - forcetypeassert + - gocheckcompilerdirectives + - gochecksumtype + - gocognit + - gocritic + - gocyclo + - gomoddirectives + - gomodguard + - goprintffuncname + - gosmopolitan + - govet + - grouper + - iface + - ineffassign + - interfacebloat + - lll + - loggercheck + - maintidx + - makezero + - mirror + - misspell + - nakedret + - nilerr + - nilnesserr + - nilnil + - noctx + - nolintlint + - perfsprint + - prealloc + - reassign + - recvcheck + - revive + - staticcheck + - tagalign + - tagliatelle + - testableexamples + - testifylint - testpackage - - exportloopref - disable: - - interfacer - - paralleltest - - gofumpt - - exhaustivestruct - - scopelint - - gochecknoglobals - presets: - - bugs - - complexity - - format - - performance - - style - - unused \ No newline at end of file + - thelper + - tparallel + - unconvert + - unparam + - unused + - usestdlibvars + - usetesting + - wastedassign + - whitespace + - wrapcheck + settings: + revive: + severity: warning + cyclop: + max-complexity: 20 + nakedret: + max-func-lines: 50 + lll: + line-length: 120 + gocognit: + min-complexity: 64 diff --git a/adapter/console/console.go b/adapter/console/console.go index 5293aed..33dd2ef 100644 --- a/adapter/console/console.go +++ b/adapter/console/console.go @@ -7,8 +7,7 @@ // // The format of message produced by console adapters is: // -// LEVEL message key=value key=value error=error -// +// LEVEL message key=value key=value error=error package console import ( @@ -21,12 +20,12 @@ import ( ) // StdoutAdapter returns a logger.Adapter implementation which prints log messages to stdout. -func StdoutAdapter() logger.Adapter { // nolint +func StdoutAdapter() logger.Adapter { return printer.Adapter{Printer: WriterPrinter{os.Stdout}} } // StderrAdapter returns a logger.Adapter implementation which prints log messages to stderr. -func StderrAdapter() logger.Adapter { // nolint +func StderrAdapter() logger.Adapter { return printer.Adapter{Printer: WriterPrinter{os.Stderr}} } diff --git a/adapter/internal/adaptertest/adaptertest.go b/adapter/internal/adaptertest/adaptertest.go index 670a399..ecd0782 100644 --- a/adapter/internal/adaptertest/adaptertest.go +++ b/adapter/internal/adaptertest/adaptertest.go @@ -43,7 +43,7 @@ type InterfaceField struct { } // Run runs tests common to all logger.Adapter implementations. -func Run(t *testing.T, subject Subject) { // nolint +func Run(t *testing.T, subject Subject) { //nolint ctx := context.Background() var entry = logger.Entry{ @@ -171,7 +171,7 @@ func Run(t *testing.T, subject Subject) { // nolint "error only": { entry: func() logger.Entry { e := entry - e.Error = errors.New(errorFieldValue) // nolint:goerr113 + e.Error = errors.New(errorFieldValue) return e }(), @@ -187,7 +187,7 @@ func Run(t *testing.T, subject Subject) { // nolint "field and error": { entry: func() logger.Entry { e := entry.With(logger.Field{Key: "StringField", Value: stringFieldValue}) - e.Error = errors.New(errorFieldValue) // nolint:goerr113 + e.Error = errors.New(errorFieldValue) return e }(), @@ -199,7 +199,7 @@ func Run(t *testing.T, subject Subject) { // nolint e := entry. With(logger.Field{Key: "StringField", Value: stringFieldValue}). With(logger.Field{Key: "IntField", Value: intFieldValue}) - e.Error = errors.New(errorFieldValue) // nolint:goerr113 + e.Error = errors.New(errorFieldValue) return e }(), diff --git a/adapter/internal/benchmark/benchmark.go b/adapter/internal/benchmark/benchmark.go index ecd50b0..99c1b2b 100644 --- a/adapter/internal/benchmark/benchmark.go +++ b/adapter/internal/benchmark/benchmark.go @@ -15,7 +15,7 @@ import ( var ErrSome = errors.New("ErrSome") // Adapter runs benchmarks on any implementation of logger.Adapter. -func Adapter(b *testing.B, adapter logger.Adapter) { // nolint:funlen +func Adapter(b *testing.B, adapter logger.Adapter) { //nolint:funlen b.Helper() ctx := context.Background() @@ -62,9 +62,9 @@ func Adapter(b *testing.B, adapter logger.Adapter) { // nolint:funlen fields := map[string]interface{}{ "string": "str", "int": 1, - "int64": int64(64), // nolint - "float64": 1.64, // nolint - "float32": float32(1.32), // nolint + "int64": int64(64), + "float64": 1.64, + "float32": float32(1.32), "time": time.Time{}, } diff --git a/adapter/internal/fake/std.go b/adapter/internal/fake/std.go index 90547da..7cf661c 100644 --- a/adapter/internal/fake/std.go +++ b/adapter/internal/fake/std.go @@ -1,7 +1,6 @@ package fake import ( - "io/ioutil" "os" "testing" @@ -12,7 +11,7 @@ func swap(t *testing.T, get func() *os.File, set func(*os.File)) SwappedFile { t.Helper() prev := get() - tmpFile, err := ioutil.TempFile("", "") + tmpFile, err := os.CreateTemp(os.TempDir(), "") require.NoError(t, err) set(tmpFile) @@ -40,7 +39,7 @@ func (f SwappedFile) Release() { func (f SwappedFile) String(t *testing.T) string { t.Helper() - line, err := ioutil.ReadFile(f.current.Name()) + line, err := os.ReadFile(f.current.Name()) require.NoError(t, err) return string(line) diff --git a/adapter/logadapter/logadapter.go b/adapter/logadapter/logadapter.go index c8570f8..e132153 100644 --- a/adapter/logadapter/logadapter.go +++ b/adapter/logadapter/logadapter.go @@ -8,7 +8,7 @@ import ( "github.com/elgopher/yala/logger" ) -func Adapter(l *log.Logger) logger.Adapter { // nolint +func Adapter(l *log.Logger) logger.Adapter { if l == nil { return noopAdapter{} } @@ -21,7 +21,7 @@ type printerLogger struct { } func (p printerLogger) Println(skipCallerFrames int, msg string) { - _ = p.Logger.Output(skipCallerFrames+2, msg) // nolint + _ = p.Logger.Output(skipCallerFrames+2, msg) //nolint } type noopAdapter struct{} diff --git a/adapter/logfmt/logfmt.go b/adapter/logfmt/logfmt.go index 7118d8b..edec0c2 100644 --- a/adapter/logfmt/logfmt.go +++ b/adapter/logfmt/logfmt.go @@ -41,11 +41,7 @@ func writeValue(builder *strings.Builder, value interface{}) { valueStr = strings.ReplaceAll(valueStr, `"`, `\"`) } - requiresQuoting := false - - if strings.ContainsRune(valueStr, ' ') || strings.ContainsRune(valueStr, '=') { - requiresQuoting = true - } + requiresQuoting := strings.ContainsRune(valueStr, ' ') || strings.ContainsRune(valueStr, '=') if requiresQuoting { builder.WriteByte('"') diff --git a/adapter/logrusadapter/logrus.go b/adapter/logrusadapter/logrus.go index 9adc787..83995e1 100644 --- a/adapter/logrusadapter/logrus.go +++ b/adapter/logrusadapter/logrus.go @@ -34,7 +34,7 @@ func (a Adapter) Log(ctx context.Context, entry logger.Entry) { logrusLogger.Log(logrusLevel(entry), entry.Message) } -func loggerWithFields(logrusLogger LogrusLogger, entry logger.Entry) LogrusLogger { // nolint:ireturn +func loggerWithFields(logrusLogger LogrusLogger, entry logger.Entry) LogrusLogger { //nolint:ireturn length := len(entry.Fields) if entry.Error != nil { length++ diff --git a/adapter/logrusadapter/logrus_test.go b/adapter/logrusadapter/logrus_test.go index 24be551..303e34f 100644 --- a/adapter/logrusadapter/logrus_test.go +++ b/adapter/logrusadapter/logrus_test.go @@ -39,7 +39,7 @@ func TestAdapter_Log(t *testing.T) { }) } -func newAdapter(writer io.Writer) logger.Adapter { // nolint +func newAdapter(writer io.Writer) logger.Adapter { logrusLogger := logrus.New() logrusLogger.SetFormatter(&logrus.JSONFormatter{}) logrusLogger.SetOutput(writer) diff --git a/adapter/zapadapter/zapadapter_test.go b/adapter/zapadapter/zapadapter_test.go index 571e49d..9b4bbd5 100644 --- a/adapter/zapadapter/zapadapter_test.go +++ b/adapter/zapadapter/zapadapter_test.go @@ -57,7 +57,7 @@ func TestAdapter_Log(t *testing.T) { }) } -func newAdapter(writer io.Writer) logger.Adapter { // nolint +func newAdapter(writer io.Writer) logger.Adapter { scheme := generateUniqueScheme() // Zap does not allow to override existing scheme _ = zap.RegisterSink(scheme, func(url *url.URL) (zap.Sink, error) { return sinkWriter{Writer: writer}, nil diff --git a/logger/global.go b/logger/global.go index 01b4085..767c571 100644 --- a/logger/global.go +++ b/logger/global.go @@ -10,14 +10,14 @@ import ( // Global is a logger shared globally. You can use it to define global logger for your package: // -// package yourpackage -// import "github.com/elgopher/yala/logger" +// package yourpackage +// import "github.com/elgopher/yala/logger" // -// var log logger.Global // define global logger, no need to initialize (by default nothing is logged) +// var log logger.Global // define global logger, no need to initialize (by default nothing is logged) // -// func SetLoggerAdapter(adapter logger.Adapter) { -// log.SetAdapter(adapter) -// } +// func SetLoggerAdapter(adapter logger.Adapter) { +// log.SetAdapter(adapter) +// } // // It is safe to use it concurrently. // @@ -46,7 +46,7 @@ func (g *Global) SetAdapter(adapter Adapter) { g.adapterValue().Store(adapterWrapper{Adapter: adapter}) } -func (g *Global) getAdapter() Adapter { // nolint:ireturn +func (g *Global) getAdapter() Adapter { //nolint:ireturn value := g.adapterValue() adapter, ok := value.Load().(Adapter) diff --git a/logger/logger_concurrency_test.go b/logger/logger_concurrency_test.go index e6619d4..3e627a6 100644 --- a/logger/logger_concurrency_test.go +++ b/logger/logger_concurrency_test.go @@ -28,7 +28,7 @@ func TestConcurrency(t *testing.T) { global.WithError(ErrSome).Error(ctx, message) }) // then - assert.Equal(t, adapter.Count(), 6000) + assert.Equal(t, 6000, adapter.Count()) }) t.Run("normal log functions", func(t *testing.T) { @@ -44,7 +44,7 @@ func TestConcurrency(t *testing.T) { log.WithError(ErrSome).Error(ctx, message) }) // then - assert.Equal(t, adapter.Count(), 6000) + assert.Equal(t, 6000, adapter.Count()) }) t.Run("With should not data race when -race flag is used", func(t *testing.T) { diff --git a/logger/logger_test.go b/logger/logger_test.go index 3a71d6f..fa36cd3 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -349,7 +349,7 @@ func TestLogCause(t *testing.T) { func TestWith(t *testing.T) { globalWith := func(l anyLogger, k string, v interface{}) anyLogger { - return l.(*logger.Global).With(k, v) // nolint:forcetypeassert // no generics still in Go + return l.(*logger.Global).With(k, v) //nolint:forcetypeassert // no generics still in Go } tests := map[string]struct { @@ -392,7 +392,7 @@ func TestWith(t *testing.T) { return logger.WithAdapter(adapter).With(field.Key, field.Value) }, With: func(l anyLogger, k string, v interface{}) anyLogger { - return l.(logger.Logger).With(k, v) // nolint:forcetypeassert // no generics still in Go + return l.(logger.Logger).With(k, v) //nolint:forcetypeassert // no generics still in Go }, }, "global, WithFields": { @@ -403,7 +403,7 @@ func TestWith(t *testing.T) { return global.With(field.Key, field.Value) }, With: func(l anyLogger, k string, v interface{}) anyLogger { - return l.(*logger.Global).WithFields(logger.Fields{k: v}) // nolint:forcetypeassert // no generics still in Go + return l.(*logger.Global).WithFields(logger.Fields{k: v}) //nolint:forcetypeassert // no generics still in Go }, }, "normal, WithFields": { @@ -411,7 +411,7 @@ func TestWith(t *testing.T) { return logger.WithAdapter(adapter).With(field.Key, field.Value) }, With: func(l anyLogger, k string, v interface{}) anyLogger { - return l.(logger.Logger).WithFields(logger.Fields{k: v}) // nolint:forcetypeassert // no generics still in Go + return l.(logger.Logger).WithFields(logger.Fields{k: v}) //nolint:forcetypeassert // no generics still in Go }, }, } @@ -454,8 +454,8 @@ func TestWith(t *testing.T) { loggerWithField1.Info(ctx, message) loggerWithBothFields.Info(ctx, message) require.Len(t, adapter.entries, 2) - assert.Equal(t, adapter.entries[0].Fields, []logger.Field{field1}) - assert.Equal(t, adapter.entries[1].Fields, []logger.Field{field1, field2}) + assert.Equal(t, []logger.Field{field1}, adapter.entries[0].Fields) + assert.Equal(t, []logger.Field{field1, field2}, adapter.entries[1].Fields) }) }) } @@ -502,7 +502,7 @@ func TestGlobal_WithFields(t *testing.T) { func TestWithError(t *testing.T) { globalWithError := func(l anyLogger, err error) anyLogger { - return l.(*logger.Global).WithError(err) // nolint:forcetypeassert // no generics still in Go + return l.(*logger.Global).WithError(err) //nolint:forcetypeassert // no generics still in Go } loggersWithError := map[string]struct { @@ -545,7 +545,7 @@ func TestWithError(t *testing.T) { return logger.WithAdapter(adapter).WithError(err) }, WithError: func(l anyLogger, err error) anyLogger { - return l.(logger.Logger).WithError(err) // nolint:forcetypeassert // no generics still in Go + return l.(logger.Logger).WithError(err) //nolint:forcetypeassert // no generics still in Go }, }, } @@ -591,7 +591,7 @@ func TestWithSkippedCallerFrame(t *testing.T) { return logger.WithAdapter(adapter) }, skipOneFrame: func(l anyLogger, adapter logger.Adapter) anyLogger { - return l.(logger.Logger).WithSkippedCallerFrame() // nolint:forcetypeassert // no generics still in Go + return l.(logger.Logger).WithSkippedCallerFrame() //nolint:forcetypeassert // no generics still in Go }, }, "global": { @@ -602,7 +602,7 @@ func TestWithSkippedCallerFrame(t *testing.T) { return &global }, skipOneFrame: func(l anyLogger, adapter logger.Adapter) anyLogger { - return l.(*logger.Global).WithSkippedCallerFrame() // nolint:forcetypeassert // no generics still in Go + return l.(*logger.Global).WithSkippedCallerFrame() //nolint:forcetypeassert // no generics still in Go }, }, } diff --git a/logger/noop.go b/logger/noop.go index d438761..0b7e915 100644 --- a/logger/noop.go +++ b/logger/noop.go @@ -23,7 +23,7 @@ func (g *initialGlobalNoopAdapter) Log(_ context.Context, entry Entry) { g.once.Do(func() { const framesToSkip = 4 _, file, line, _ := runtime.Caller(entry.SkippedCallerFrames + framesToSkip) - fmt.Printf("%s:%d cannot log message with level %s. Please configure the global logger.\n", file, line, entry.Level) // nolint + fmt.Printf("%s:%d cannot log message with level %s. Please configure the global logger.\n", file, line, entry.Level) //nolint }) } }