Skip to content

Commit a06609c

Browse files
committed
feat: Add logrlint
Signed-off-by: Timon Wong <timon86.wang@gmail.com>
1 parent 235df6e commit a06609c

File tree

9 files changed

+56
-0
lines changed

9 files changed

+56
-0
lines changed

.golangci.reference.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,7 @@ linters:
19271927
- interfacer
19281928
- ireturn
19291929
- lll
1930+
- logrlint
19301931
- maintidx
19311932
- makezero
19321933
- maligned

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ require (
9393
github.com/tdakkota/asciicheck v0.1.1
9494
github.com/tetafro/godot v1.4.11
9595
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
96+
github.com/timonwong/logrlint v0.0.0-20220809041504-d0f840667f4f
9697
github.com/tomarrell/wrapcheck/v2 v2.6.2
9798
github.com/tommy-muehle/go-mnd/v2 v2.5.0
9899
github.com/ultraware/funlen v0.0.3

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/logrlint.go

Lines changed: 19 additions & 0 deletions
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+
const LogrLintName = "logrlint"
11+
12+
func NewLogrLint() *goanalysis.Linter {
13+
return goanalysis.NewLinter(
14+
LogrLintName,
15+
logrlint.Doc,
16+
[]*analysis.Analyzer{logrlint.Analyzer},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
19+
}

pkg/lint/lintersdb/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
574574
WithSince("v1.8.0").
575575
WithPresets(linter.PresetStyle),
576576

577+
linter.NewConfig(golinters.NewLogrLint()).
578+
WithSince("v1.49.0").
579+
WithLoadForGoAnalysis().
580+
WithPresets(linter.PresetBugs).
581+
WithURL("https://github.com/timonwong/logrlint"),
582+
577583
linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
578584
WithSince("v1.44.0").
579585
WithPresets(linter.PresetComplexity).

test/linters_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func TestGoimportsLocal(t *testing.T) {
8989
ExpectHasIssue("testdata/goimports/goimports.go:8: File is not `goimports`-ed")
9090
}
9191

92+
func TestLogrLint(t *testing.T) {
93+
testSourcesFromDir(t, filepath.Join(testdataDir, "logrlint"))
94+
}
95+
9296
func TestGciLocal(t *testing.T) {
9397
sourcePath := filepath.Join(testdataDir, "gci", "gci.go")
9498
args := []string{

test/testdata/logrlint/example.go

Lines changed: 16 additions & 0 deletions
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") // Error `odd number of arguments passed as key-value pairs for logging`
13+
log.Info("message", "key1", "value1", "key2", "value2", "key3") // Error `odd number of arguments passed as key-value pairs for logging`
14+
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2") // Error `odd number of arguments passed as key-value pairs for logging`
15+
log.Error(fmt.Errorf("error"), "message", "key1", "value1", "key2", "value2")
16+
}

test/testdata/logrlint/go.mod

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)