Skip to content

Commit

Permalink
feat: add logrlint (#3093)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
timonwong and ldez committed Aug 22, 2022
1 parent 320a18e commit f48530e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -1932,6 +1932,7 @@ linters:
- interfacer
- ireturn
- lll
- logrlint
- maintidx
- makezero
- maligned
Expand Down Expand Up @@ -2035,6 +2036,7 @@ linters:
- interfacer
- ireturn
- lll
- logrlint
- maintidx
- makezero
- maligned
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -43,6 +43,10 @@ test_linters:
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdata/$T
.PHONY: test_linters

test_linters_sub:
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdataSubDir/$T
.PHONY: test_linters_sub

# Maintenance

fast_generate: assets/github-action-config.json
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -94,6 +94,7 @@ require (
github.com/tdakkota/asciicheck v0.1.1
github.com/tetafro/godot v1.4.11
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
github.com/timonwong/logrlint v0.1.0
github.com/tomarrell/wrapcheck/v2 v2.6.2
github.com/tommy-muehle/go-mnd/v2 v2.5.0
github.com/ultraware/funlen v0.0.3
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.

19 changes: 19 additions & 0 deletions pkg/golinters/logrlint.go
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/timonwong/logrlint"
"golang.org/x/tools/go/analysis"

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

func NewLogrLint() *goanalysis.Linter {
a := logrlint.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -582,6 +582,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithSince("v1.8.0").
WithPresets(linter.PresetStyle),

linter.NewConfig(golinters.NewLogrLint()).
WithSince("v1.49.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithURL("https://github.com/timonwong/logrlint"),

linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
WithSince("v1.44.0").
WithPresets(linter.PresetComplexity).
Expand Down
12 changes: 12 additions & 0 deletions test/linters_test.go
Expand Up @@ -25,6 +25,18 @@ func TestTypecheck(t *testing.T) {
testSourcesFromDir(t, filepath.Join(testdataDir, "notcompiles"))
}

func TestSourcesFromTestdataSubDir(t *testing.T) {
subDirs := []string{
"logrlint",
}

for _, dir := range subDirs {
t.Run(dir, func(t *testing.T) {
testSourcesFromDir(t, filepath.Join(testdataDir, dir))
})
}
}

func testSourcesFromDir(t *testing.T, dir string) {
t.Helper()

Expand Down
5 changes: 5 additions & 0 deletions test/testdata/logrlint/go.mod
@@ -0,0 +1,5 @@
module logrlint

go 1.16

require github.com/go-logr/logr v1.2.3
2 changes: 2 additions & 0 deletions test/testdata/logrlint/go.sum

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

16 changes: 16 additions & 0 deletions test/testdata/logrlint/logrlint.go
@@ -0,0 +1,16 @@
//golangcitest:args -Elogrlint
package logrlint

import (
"fmt"

"github.com/go-logr/logr"
)

func Example() {
log := logr.Discard()
log = log.WithValues("key") // want `odd number of arguments passed as key-value pairs for logging`
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2")
}

0 comments on commit f48530e

Please sign in to comment.