From f48530e25ea0b1db098c025a27348a720c4492b7 Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Tue, 23 Aug 2022 04:49:39 +0800 Subject: [PATCH] feat: add logrlint (#3093) Co-authored-by: Fernandez Ludovic --- .golangci.reference.yml | 2 ++ Makefile | 4 ++++ go.mod | 1 + go.sum | 2 ++ pkg/golinters/logrlint.go | 19 +++++++++++++++++++ pkg/lint/lintersdb/manager.go | 6 ++++++ test/linters_test.go | 12 ++++++++++++ test/testdata/logrlint/go.mod | 5 +++++ test/testdata/logrlint/go.sum | 2 ++ test/testdata/logrlint/logrlint.go | 16 ++++++++++++++++ 10 files changed, 69 insertions(+) create mode 100644 pkg/golinters/logrlint.go create mode 100644 test/testdata/logrlint/go.mod create mode 100644 test/testdata/logrlint/go.sum create mode 100644 test/testdata/logrlint/logrlint.go diff --git a/.golangci.reference.yml b/.golangci.reference.yml index ac5dedea8183..e02897245170 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1932,6 +1932,7 @@ linters: - interfacer - ireturn - lll + - logrlint - maintidx - makezero - maligned @@ -2035,6 +2036,7 @@ linters: - interfacer - ireturn - lll + - logrlint - maintidx - makezero - maligned diff --git a/Makefile b/Makefile index 0933674f1e87..9561191f9990 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/go.mod b/go.mod index dc598b75d309..4069dee67f7b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c348e99fe7fc..d615d7da055f 100644 --- a/go.sum +++ b/go.sum @@ -530,6 +530,8 @@ github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro= github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk= +github.com/timonwong/logrlint v0.1.0 h1:phZCcypL/vtx6cGxObJgWZ5wexZF5SXFPLOM+ru0e/M= +github.com/timonwong/logrlint v0.1.0/go.mod h1:Zleg4Gw+kRxNej+Ra7o+tEaW5k1qthTaYKU7rSD39LU= github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw= github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk= github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o= diff --git a/pkg/golinters/logrlint.go b/pkg/golinters/logrlint.go new file mode 100644 index 000000000000..9899808c7476 --- /dev/null +++ b/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) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 668eae22ff0a..9a1f1fa4d5f0 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -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). diff --git a/test/linters_test.go b/test/linters_test.go index 6794efa10045..db2ce7d0eb21 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -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() diff --git a/test/testdata/logrlint/go.mod b/test/testdata/logrlint/go.mod new file mode 100644 index 000000000000..a9d8d16ad374 --- /dev/null +++ b/test/testdata/logrlint/go.mod @@ -0,0 +1,5 @@ +module logrlint + +go 1.16 + +require github.com/go-logr/logr v1.2.3 diff --git a/test/testdata/logrlint/go.sum b/test/testdata/logrlint/go.sum new file mode 100644 index 000000000000..6da913857d04 --- /dev/null +++ b/test/testdata/logrlint/go.sum @@ -0,0 +1,2 @@ +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= diff --git a/test/testdata/logrlint/logrlint.go b/test/testdata/logrlint/logrlint.go new file mode 100644 index 000000000000..6277dea4e146 --- /dev/null +++ b/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") +}