Skip to content

Commit c2bb25f

Browse files
committed
feat: add recv linter
1 parent dcb6a57 commit c2bb25f

13 files changed

+153
-0
lines changed

.golangci.next.reference.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,14 @@ linters-settings:
15401540
patterns:
15411541
- ".*"
15421542

1543+
recv:
1544+
# Maximum length for a receiver name.
1545+
# Default: 3
1546+
max-name-length: 6
1547+
# Allow to use pointer receiver and non pointer receiver on the same struct.
1548+
# Default: true
1549+
type-consistency: false
1550+
15431551
revive:
15441552
# Maximum number of open files at the same time.
15451553
# See https://github.com/mgechev/revive#command-line-flags
@@ -2689,6 +2697,7 @@ linters:
26892697
- promlinter
26902698
- protogetter
26912699
- reassign
2700+
- recv
26922701
- revive
26932702
- rowserrcheck
26942703
- sloglint
@@ -2804,6 +2813,7 @@ linters:
28042813
- promlinter
28052814
- protogetter
28062815
- reassign
2816+
- recv
28072817
- revive
28082818
- rowserrcheck
28092819
- sloglint

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ require (
6767
github.com/kyoh86/exportloopref v0.1.11
6868
github.com/lasiar/canonicalheader v1.1.1
6969
github.com/ldez/gomoddirectives v0.2.4
70+
github.com/ldez/recv v0.1.1
7071
github.com/ldez/tagliatelle v0.5.0
7172
github.com/leonklingele/grouper v1.1.2
7273
github.com/lufeee/execinquery v1.2.1

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.

jsonschema/golangci.next.jsonschema.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@
388388
"promlinter",
389389
"protogetter",
390390
"reassign",
391+
"recv",
391392
"revive",
392393
"rowserrcheck",
393394
"scopelint",
@@ -2283,6 +2284,22 @@
22832284
}
22842285
}
22852286
},
2287+
"recv": {
2288+
"type": "object",
2289+
"additionalProperties": false,
2290+
"properties": {
2291+
"max-name-length": {
2292+
"description": "Maximum length for a receiver name.",
2293+
"type": "integer",
2294+
"default": 3
2295+
},
2296+
"type-consistency": {
2297+
"description": "Allow to use pointer receiver and non pointer receiver on the same struct.",
2298+
"type": "boolean",
2299+
"default": true
2300+
}
2301+
}
2302+
},
22862303
"nonamedreturns": {
22872304
"type": "object",
22882305
"additionalProperties": false,

pkg/config/linters_settings.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ var defaultLintersSettings = LintersSettings{
137137
Ignore: "",
138138
Qualified: false,
139139
},
140+
Recv: RecvSettings{
141+
MaxNameLength: 3,
142+
TypeConsistency: true,
143+
},
140144
SlogLint: SlogLintSettings{
141145
NoMixedArgs: true,
142146
KVOnly: false,
@@ -257,6 +261,7 @@ type LintersSettings struct {
257261
Promlinter PromlinterSettings
258262
ProtoGetter ProtoGetterSettings
259263
Reassign ReassignSettings
264+
Recv RecvSettings
260265
Revive ReviveSettings
261266
RowsErrCheck RowsErrCheckSettings
262267
SlogLint SlogLintSettings
@@ -791,6 +796,11 @@ type ReassignSettings struct {
791796
Patterns []string `mapstructure:"patterns"`
792797
}
793798

799+
type RecvSettings struct {
800+
MaxNameLength int `mapstructure:"max-name-length"`
801+
TypeConsistency bool `mapstructure:"type-consistency"`
802+
}
803+
794804
type ReviveSettings struct {
795805
Go string `mapstructure:"-"`
796806
MaxOpenFiles int `mapstructure:"max-open-files"`

pkg/golinters/recv/recv.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package recv
2+
3+
import (
4+
"github.com/ldez/recv"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/goanalysis"
9+
)
10+
11+
func New(settings *config.RecvSettings) *goanalysis.Linter {
12+
cfg := recv.Config{}
13+
14+
if settings != nil {
15+
cfg.MaxNameLength = settings.MaxNameLength
16+
cfg.TypeConsistency = settings.TypeConsistency
17+
}
18+
19+
a := recv.New(cfg)
20+
21+
return goanalysis.NewLinter(
22+
a.Name,
23+
a.Doc,
24+
[]*analysis.Analyzer{a},
25+
nil,
26+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package recv
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}

pkg/golinters/recv/testdata/recv.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//golangcitest:args -Erecv
2+
package testdata
3+
4+
import "fmt"
5+
6+
type Foo struct { // want `the methods of "Foo" use different receiver names: f, fo.`
7+
Name string
8+
}
9+
10+
func (f Foo) A() {}
11+
func (fo Foo) B() {}
12+
13+
type Bar struct{} // want `the methods of "Bar" use pointer receiver and non pointer receiver.`
14+
15+
func (b Bar) A() {}
16+
func (b *Bar) B() {}
17+
18+
type Fuu struct{}
19+
20+
func (faaa Fuu) A() { // want `the receiver name "faaa" is too long.`
21+
fmt.Println("a")
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//golangcitest:args -Erecv
2+
//golangcitest:config_path testdata/recv_disable_type_consistency.yml
3+
package testdata
4+
5+
import "fmt"
6+
7+
type Foo struct { // want `the methods of "Foo" use different receiver names: f, fo.`
8+
Name string
9+
}
10+
11+
func (f Foo) A() {}
12+
func (fo Foo) B() {}
13+
14+
type Bar struct{}
15+
16+
func (b Bar) A() {}
17+
func (b *Bar) B() {}
18+
19+
type Fuu struct{}
20+
21+
func (faaa Fuu) A() { // want `the receiver name "faaa" is too long.`
22+
fmt.Println("a")
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
recv:
3+
type-consistency: false

0 commit comments

Comments
 (0)