Skip to content

feat: add recv linter #5013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,14 @@ linters-settings:
patterns:
- ".*"

recv:
# Maximum length for a receiver name.
# Default: 3
max-name-length: 6
# Allow to use pointer receiver and non pointer receiver on the same struct.
# Default: true
type-consistency: false

revive:
# Maximum number of open files at the same time.
# See https://github.com/mgechev/revive#command-line-flags
Expand Down Expand Up @@ -2689,6 +2697,7 @@ linters:
- promlinter
- protogetter
- reassign
- recv
- revive
- rowserrcheck
- sloglint
Expand Down Expand Up @@ -2804,6 +2813,7 @@ linters:
- promlinter
- protogetter
- reassign
- recv
- revive
- rowserrcheck
- sloglint
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
github.com/kyoh86/exportloopref v0.1.11
github.com/lasiar/canonicalheader v1.1.1
github.com/ldez/gomoddirectives v0.2.4
github.com/ldez/recv v0.1.1
github.com/ldez/tagliatelle v0.5.0
github.com/leonklingele/grouper v1.1.2
github.com/lufeee/execinquery v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@
"promlinter",
"protogetter",
"reassign",
"recv",
"revive",
"rowserrcheck",
"scopelint",
Expand Down Expand Up @@ -2283,6 +2284,22 @@
}
}
},
"recv": {
"type": "object",
"additionalProperties": false,
"properties": {
"max-name-length": {
"description": "Maximum length for a receiver name.",
"type": "integer",
"default": 3
},
"type-consistency": {
"description": "Allow to use pointer receiver and non pointer receiver on the same struct.",
"type": "boolean",
"default": true
}
}
},
"nonamedreturns": {
"type": "object",
"additionalProperties": false,
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ var defaultLintersSettings = LintersSettings{
Ignore: "",
Qualified: false,
},
Recv: RecvSettings{
MaxNameLength: 3,
TypeConsistency: true,
},
SlogLint: SlogLintSettings{
NoMixedArgs: true,
KVOnly: false,
Expand Down Expand Up @@ -257,6 +261,7 @@ type LintersSettings struct {
Promlinter PromlinterSettings
ProtoGetter ProtoGetterSettings
Reassign ReassignSettings
Recv RecvSettings
Revive ReviveSettings
RowsErrCheck RowsErrCheckSettings
SlogLint SlogLintSettings
Expand Down Expand Up @@ -791,6 +796,11 @@ type ReassignSettings struct {
Patterns []string `mapstructure:"patterns"`
}

type RecvSettings struct {
MaxNameLength int `mapstructure:"max-name-length"`
TypeConsistency bool `mapstructure:"type-consistency"`
}

type ReviveSettings struct {
Go string `mapstructure:"-"`
MaxOpenFiles int `mapstructure:"max-open-files"`
Expand Down
27 changes: 27 additions & 0 deletions pkg/golinters/recv/recv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package recv

import (
"github.com/ldez/recv"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func New(settings *config.RecvSettings) *goanalysis.Linter {
cfg := recv.Config{}

if settings != nil {
cfg.MaxNameLength = settings.MaxNameLength
cfg.TypeConsistency = settings.TypeConsistency
}

a := recv.New(cfg)

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
11 changes: 11 additions & 0 deletions pkg/golinters/recv/recv_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package recv

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
22 changes: 22 additions & 0 deletions pkg/golinters/recv/testdata/recv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//golangcitest:args -Erecv
package testdata

import "fmt"

type Foo struct { // want `the methods of "Foo" use different receiver names: f, fo.`
Name string
}

func (f Foo) A() {}
func (fo Foo) B() {}

type Bar struct{} // want `the methods of "Bar" use pointer receiver and non pointer receiver.`

func (b Bar) A() {}
func (b *Bar) B() {}

type Fuu struct{}

func (faaa Fuu) A() { // want `the receiver name "faaa" is too long.`
fmt.Println("a")
}
23 changes: 23 additions & 0 deletions pkg/golinters/recv/testdata/recv_disable_type_consistency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//golangcitest:args -Erecv
//golangcitest:config_path testdata/recv_disable_type_consistency.yml
package testdata

import "fmt"

type Foo struct { // want `the methods of "Foo" use different receiver names: f, fo.`
Name string
}

func (f Foo) A() {}
func (fo Foo) B() {}

type Bar struct{}

func (b Bar) A() {}
func (b *Bar) B() {}

type Fuu struct{}

func (faaa Fuu) A() { // want `the receiver name "faaa" is too long.`
fmt.Println("a")
}
3 changes: 3 additions & 0 deletions pkg/golinters/recv/testdata/recv_disable_type_consistency.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
recv:
type-consistency: false
17 changes: 17 additions & 0 deletions pkg/golinters/recv/testdata/recv_max_name_length.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//golangcitest:args -Erecv
//golangcitest:config_path testdata/recv_max_name_length.yml
package testdata

import "fmt"

type Fuu struct{}

func (faaa Fuu) A() {
fmt.Println("a")
}

type Foo struct{}

func (faaaaaa Foo) A() { // want `the receiver name "faaaaaa" is too long.`
fmt.Println("a")
}
3 changes: 3 additions & 0 deletions pkg/golinters/recv/testdata/recv_max_name_length.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
recv:
max-name-length: 6
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import (
"github.com/golangci/golangci-lint/pkg/golinters/promlinter"
"github.com/golangci/golangci-lint/pkg/golinters/protogetter"
"github.com/golangci/golangci-lint/pkg/golinters/reassign"
"github.com/golangci/golangci-lint/pkg/golinters/recv"
"github.com/golangci/golangci-lint/pkg/golinters/revive"
"github.com/golangci/golangci-lint/pkg/golinters/rowserrcheck"
"github.com/golangci/golangci-lint/pkg/golinters/sloglint"
Expand Down Expand Up @@ -657,6 +658,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/curioswitch/go-reassign"),

linter.NewConfig(recv.New(&cfg.LintersSettings.Recv)).
WithSince("1.62.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/ldez/recv"),

linter.NewConfig(revive.New(&cfg.LintersSettings.Revive)).
WithSince("v1.37.0").
WithPresets(linter.PresetStyle, linter.PresetMetaLinter).
Expand Down
Loading