Skip to content

Commit fdb594c

Browse files
timonwongldez
authored andcommitted
feat: Add logrlint
Signed-off-by: Timon Wong <timon86.wang@gmail.com>
1 parent 320a18e commit fdb594c

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
@@ -1932,6 +1932,7 @@ linters:
19321932
- interfacer
19331933
- ireturn
19341934
- lll
1935+
- logrlint
19351936
- maintidx
19361937
- makezero
19371938
- maligned

go.mod

Lines changed: 1 addition & 0 deletions
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.0.0-20220809041504-d0f840667f4f
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

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
@@ -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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ func findSources(t *testing.T, pathPatterns ...string) []string {
117117

118118
return sources
119119
}
120+
121+
func TestLogrLint(t *testing.T) {
122+
testSourcesFromDir(t, filepath.Join(testdataDir, "logrlint"))
123+
}

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)