From fdb594c0ebd078460b8b926958e7016acdbe0169 Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Fri, 12 Aug 2022 13:29:20 +0800 Subject: [PATCH 1/7] feat: Add logrlint Signed-off-by: Timon Wong --- .golangci.reference.yml | 1 + go.mod | 1 + go.sum | 2 ++ pkg/golinters/logrlint.go | 19 +++++++++++++++++++ pkg/lint/lintersdb/manager.go | 6 ++++++ test/linters_test.go | 4 ++++ test/testdata/logrlint/example.go | 16 ++++++++++++++++ test/testdata/logrlint/go.mod | 5 +++++ test/testdata/logrlint/go.sum | 2 ++ 9 files changed, 56 insertions(+) create mode 100644 pkg/golinters/logrlint.go create mode 100644 test/testdata/logrlint/example.go create mode 100644 test/testdata/logrlint/go.mod create mode 100644 test/testdata/logrlint/go.sum diff --git a/.golangci.reference.yml b/.golangci.reference.yml index ac5dedea8183..bc0e83fa6f95 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1932,6 +1932,7 @@ linters: - interfacer - ireturn - lll + - logrlint - maintidx - makezero - maligned diff --git a/go.mod b/go.mod index dc598b75d309..0d2205117483 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.0.0-20220809041504-d0f840667f4f 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..f670a119568f 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.0.0-20220809041504-d0f840667f4f h1:BrUYVvxVZjh0yBWdZuY8cHMa2SqGLAW1S3U0KdjGmIk= +github.com/timonwong/logrlint v0.0.0-20220809041504-d0f840667f4f/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..bffe3d4f4202 --- /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" +) + +const LogrLintName = "logrlint" + +func NewLogrLint() *goanalysis.Linter { + return goanalysis.NewLinter( + LogrLintName, + logrlint.Doc, + []*analysis.Analyzer{logrlint.Analyzer}, + 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..8d98e27e8394 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -117,3 +117,7 @@ func findSources(t *testing.T, pathPatterns ...string) []string { return sources } + +func TestLogrLint(t *testing.T) { + testSourcesFromDir(t, filepath.Join(testdataDir, "logrlint")) +} diff --git a/test/testdata/logrlint/example.go b/test/testdata/logrlint/example.go new file mode 100644 index 000000000000..365982981624 --- /dev/null +++ b/test/testdata/logrlint/example.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") // Error `odd number of arguments passed as key-value pairs for logging` + log.Info("message", "key1", "value1", "key2", "value2", "key3") // Error `odd number of arguments passed as key-value pairs for logging` + log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // Error `odd number of arguments passed as key-value pairs for logging` + log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2") +} 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= From 4f83e7304356f564a84b69e5b9ee60bc44545aa1 Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Mon, 15 Aug 2022 20:21:08 +0800 Subject: [PATCH 2/7] fix missing entry in .golangci.reference.yml Signed-off-by: Timon Wong --- .golangci.reference.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index bc0e83fa6f95..e02897245170 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2036,6 +2036,7 @@ linters: - interfacer - ireturn - lll + - logrlint - maintidx - makezero - maligned From a510533752b22cdd9bfa943bc469fe85218494b4 Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Mon, 15 Aug 2022 20:23:31 +0800 Subject: [PATCH 3/7] Add missing tag for logrlint --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 0d2205117483..4069dee67f7b 100644 --- a/go.mod +++ b/go.mod @@ -94,7 +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.0.0-20220809041504-d0f840667f4f + 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 f670a119568f..d615d7da055f 100644 --- a/go.sum +++ b/go.sum @@ -530,8 +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.0.0-20220809041504-d0f840667f4f h1:BrUYVvxVZjh0yBWdZuY8cHMa2SqGLAW1S3U0KdjGmIk= -github.com/timonwong/logrlint v0.0.0-20220809041504-d0f840667f4f/go.mod h1:Zleg4Gw+kRxNej+Ra7o+tEaW5k1qthTaYKU7rSD39LU= +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= From 7f250ba13e1611dd2fcf405709b2d20f02cb3a54 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 21 Aug 2022 16:54:34 +0200 Subject: [PATCH 4/7] review: use new test framework --- test/linters_test.go | 12 ++++++++++-- test/testdata/logrlint/{example.go => logrlint.go} | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) rename test/testdata/logrlint/{example.go => logrlint.go} (63%) diff --git a/test/linters_test.go b/test/linters_test.go index 8d98e27e8394..e8ae4a8f485d 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -118,6 +118,14 @@ func findSources(t *testing.T, pathPatterns ...string) []string { return sources } -func TestLogrLint(t *testing.T) { - testSourcesFromDir(t, filepath.Join(testdataDir, "logrlint")) +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)) + }) + } } diff --git a/test/testdata/logrlint/example.go b/test/testdata/logrlint/logrlint.go similarity index 63% rename from test/testdata/logrlint/example.go rename to test/testdata/logrlint/logrlint.go index 365982981624..6277dea4e146 100644 --- a/test/testdata/logrlint/example.go +++ b/test/testdata/logrlint/logrlint.go @@ -9,8 +9,8 @@ import ( func Example() { log := logr.Discard() - log = log.WithValues("key") // Error `odd number of arguments passed as key-value pairs for logging` - log.Info("message", "key1", "value1", "key2", "value2", "key3") // Error `odd number of arguments passed as key-value pairs for logging` - log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // Error `odd number of arguments passed as key-value pairs for logging` + 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") } From f7685783bfbbc2158e537d657fb96153edcd22d2 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 21 Aug 2022 17:03:06 +0200 Subject: [PATCH 5/7] review --- pkg/golinters/logrlint.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/golinters/logrlint.go b/pkg/golinters/logrlint.go index bffe3d4f4202..9899808c7476 100644 --- a/pkg/golinters/logrlint.go +++ b/pkg/golinters/logrlint.go @@ -7,13 +7,13 @@ import ( "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" ) -const LogrLintName = "logrlint" - func NewLogrLint() *goanalysis.Linter { + a := logrlint.Analyzer + return goanalysis.NewLinter( - LogrLintName, - logrlint.Doc, - []*analysis.Analyzer{logrlint.Analyzer}, + a.Name, + a.Doc, + []*analysis.Analyzer{a}, nil, ).WithLoadMode(goanalysis.LoadModeTypesInfo) } From a49eade435e0dd819e5c3171eab19659ab21b727 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 21 Aug 2022 20:23:51 +0200 Subject: [PATCH 6/7] chore: add test_linters_sub target --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) 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 From 5fe368020fc0eb8f15debf33b642d82895c50fae Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 22 Aug 2022 00:05:02 +0200 Subject: [PATCH 7/7] chore: move test --- test/linters_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/linters_test.go b/test/linters_test.go index e8ae4a8f485d..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() @@ -117,15 +129,3 @@ func findSources(t *testing.T, pathPatterns ...string) []string { return sources } - -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)) - }) - } -}