Skip to content

add linter zerologlint #3726

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

Merged
merged 6 commits into from
Apr 16, 2023
Merged
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
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- zerologlint

# Enable all available linters.
# Default: false
Expand Down Expand Up @@ -2285,6 +2286,7 @@ linters:
- whitespace
- wrapcheck
- wsl
- zerologlint

# Enable presets.
# https://golangci-lint.run/usage/linters
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ require (
github.com/xen0n/gosmopolitan v1.2.1
github.com/yagipy/maintidx v1.0.0
github.com/yeya24/promlinter v0.2.0
github.com/ykadowak/zerologlint v0.1.1
gitlab.com/bosi/decorder v0.2.3
golang.org/x/tools v0.8.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pkg/golinters/zerologlint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/ykadowak/zerologlint"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewZerologLint() *goanalysis.Linter {
return goanalysis.NewLinter(
"zerologlint",
"Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg`.",
[]*analysis.Analyzer{zerologlint.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.26.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/nolintlint/README.md"),

linter.NewConfig(golinters.NewZerologLint()).
WithSince("v1.53.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/ykadowak/zerologlint"),
}

enabledByDefault := map[string]bool{
Expand Down
1 change: 1 addition & 0 deletions test/linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestSourcesFromTestdataSubDir(t *testing.T) {
subDirs := []string{
"loggercheck",
"ginkgolinter",
"zerologlint",
}

for _, dir := range subDirs {
Expand Down
14 changes: 14 additions & 0 deletions test/testdata/zerologlint/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module zerologlint

go 1.19

require (
github.com/rs/zerolog v1.29.1
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
golang.org/x/sys v0.7.0 // indirect
)
21 changes: 21 additions & 0 deletions test/testdata/zerologlint/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions test/testdata/zerologlint/zerologlint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//golangcitest:args -Ezerologlint
package zerologlint

import (
"fmt"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/exp/slices"
)

func expectWarnings() {
log.Error() // want "must be dispatched by Msg or Send method"
log.Info() // want "must be dispatched by Msg or Send method"
log.Fatal() // want "must be dispatched by Msg or Send method"
log.Debug() // want "must be dispatched by Msg or Send method"
log.Warn() // want "must be dispatched by Msg or Send method"

err := fmt.Errorf("foobarerror")
log.Error().Err(err) // want "must be dispatched by Msg or Send method"
log.Error().Err(err).Str("foo", "bar").Int("foo", 1) // want "must be dispatched by Msg or Send method"

logger := log.Error() // want "must be dispatched by Msg or Send method"
logger.Err(err).Str("foo", "bar").Int("foo", 1)

// include zerolog.Dict()
log.Info(). // want "must be dispatched by Msg or Send method"
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
)

// conditional
logger2 := log.Info() // want "must be dispatched by Msg or Send method"
if err != nil {
logger2 = log.Error() // want "must be dispatched by Msg or Send method"
}
logger2.Str("foo", "bar")
}

func expectNoWarnings() {
log.Fatal().Send()
log.Panic().Msg("")
log.Debug().Send()
log.Info().Msg("")
log.Warn().Send()
log.Error().Msg("")

log.Error().Str("foo", "bar").Send()
err := fmt.Errorf("foobarerror")
log.Error().Err(err).Str("foo", "bar").Int("foo", 1).Msg("")

logger := log.Error()
logger.Send()

// include zerolog.Dict()
log.Info().
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
).Send()

// conditional
logger2 := log.Info()
if err != nil {
logger2 = log.Error()
}
logger2.Send()
}

// https://github.com/ykadowak/zerologlint/pull/2
func packageNil() {
s := []int{1, 2, 3}
slices.Sort(s)
}