Skip to content

Commit f48530e

Browse files
timonwongldez
andauthored
feat: add logrlint (#3093)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 320a18e commit f48530e

File tree

10 files changed

+69
-0
lines changed

10 files changed

+69
-0
lines changed

.golangci.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,7 @@ linters:
19321932
- interfacer
19331933
- ireturn
19341934
- lll
1935+
- logrlint
19351936
- maintidx
19361937
- makezero
19371938
- maligned
@@ -2035,6 +2036,7 @@ linters:
20352036
- interfacer
20362037
- ireturn
20372038
- lll
2039+
- logrlint
20382040
- maintidx
20392041
- makezero
20402042
- maligned

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ test_linters:
4343
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdata/$T
4444
.PHONY: test_linters
4545

46+
test_linters_sub:
47+
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdataSubDir/$T
48+
.PHONY: test_linters_sub
49+
4650
# Maintenance
4751

4852
fast_generate: assets/github-action-config.json

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ require (
9494
github.com/tdakkota/asciicheck v0.1.1
9595
github.com/tetafro/godot v1.4.11
9696
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
97+
github.com/timonwong/logrlint v0.1.0
9798
github.com/tomarrell/wrapcheck/v2 v2.6.2
9899
github.com/tommy-muehle/go-mnd/v2 v2.5.0
99100
github.com/ultraware/funlen v0.0.3

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/logrlint.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/timonwong/logrlint"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewLogrLint() *goanalysis.Linter {
11+
a := logrlint.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
19+
}

pkg/lint/lintersdb/manager.go

+6
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
582582
WithSince("v1.8.0").
583583
WithPresets(linter.PresetStyle),
584584

585+
linter.NewConfig(golinters.NewLogrLint()).
586+
WithSince("v1.49.0").
587+
WithLoadForGoAnalysis().
588+
WithPresets(linter.PresetBugs).
589+
WithURL("https://github.com/timonwong/logrlint"),
590+
585591
linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
586592
WithSince("v1.44.0").
587593
WithPresets(linter.PresetComplexity).

test/linters_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ func TestTypecheck(t *testing.T) {
2525
testSourcesFromDir(t, filepath.Join(testdataDir, "notcompiles"))
2626
}
2727

28+
func TestSourcesFromTestdataSubDir(t *testing.T) {
29+
subDirs := []string{
30+
"logrlint",
31+
}
32+
33+
for _, dir := range subDirs {
34+
t.Run(dir, func(t *testing.T) {
35+
testSourcesFromDir(t, filepath.Join(testdataDir, dir))
36+
})
37+
}
38+
}
39+
2840
func testSourcesFromDir(t *testing.T, dir string) {
2941
t.Helper()
3042

test/testdata/logrlint/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module logrlint
2+
3+
go 1.16
4+
5+
require github.com/go-logr/logr v1.2.3

test/testdata/logrlint/go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/testdata/logrlint/logrlint.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//golangcitest:args -Elogrlint
2+
package logrlint
3+
4+
import (
5+
"fmt"
6+
7+
"github.com/go-logr/logr"
8+
)
9+
10+
func Example() {
11+
log := logr.Discard()
12+
log = log.WithValues("key") // want `odd number of arguments passed as key-value pairs for logging`
13+
log.Info("message", "key1", "value1", "key2", "value2", "key3") // want `odd number of arguments passed as key-value pairs for logging`
14+
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // want `odd number of arguments passed as key-value pairs for logging`
15+
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2")
16+
}

0 commit comments

Comments
 (0)