Skip to content
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

feat: add logrlint #3093

Merged
merged 7 commits into from Aug 22, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
}