Skip to content
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

Add iface linter #4871

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,21 @@ linters-settings:
# Default: false
var-require-grouping: true

iface:
# By default, set to empty.
# Empty means that all analyzers are enabled.
# Default: []
enable:
- unused
- identical
- opaque
settings:
unused:
# List of packages path to exclude from the check.
# Default: []
exclude:
- github.com/example/log

importas:
# Do not allow unaliased imports of aliased packages.
# Default: false
Expand Down Expand Up @@ -2659,6 +2674,7 @@ linters:
- gosmopolitan
- govet
- grouper
- iface
- importas
- inamedparam
- ineffassign
Expand Down Expand Up @@ -2775,6 +2791,7 @@ linters:
- gosmopolitan
- govet
- grouper
- iface
- importas
- inamedparam
- ineffassign
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ require (
github.com/ultraware/funlen v0.1.0
github.com/ultraware/whitespace v0.1.1
github.com/uudashr/gocognit v1.1.3
github.com/uudashr/iface v1.2.0
github.com/valyala/quicktemplate v1.8.0
github.com/xen0n/gosmopolitan v1.2.2
github.com/yagipy/maintidx v1.0.0
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.

32 changes: 32 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,13 @@
"waitgroup-by-value"
]
},
"iface-analyzers": {
"enum": [
"unused",
"identical",
"opaque"
]
},
"linters": {
"$comment": "anyOf with enum is used to allow auto completion of non-custom linters",
"description": "Linters usable.",
Expand Down Expand Up @@ -354,6 +361,7 @@
"gosmopolitan",
"govet",
"grouper",
"iface",
"ifshort",
"importas",
"inamedparam",
Expand Down Expand Up @@ -1910,6 +1918,30 @@
}
}
},
"iface": {
"type": "object",
"additionalProperties": false,
"properties": {
"enable": {
"description": "Enable analyzers by name.",
"type": "array",
"items": {
"$ref": "#/definitions/iface-analyzers"
}
},
"settings": {
"type": "object",
"propertyNames": {
"$ref": "#/definitions/iface-analyzers"
},
"patternProperties": {
"^.*$": {
"type": "object"
}
}
}
}
},
"importas": {
"type": "object",
"additionalProperties": false,
Expand Down
6 changes: 6 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ type LintersSettings struct {
Gosmopolitan GosmopolitanSettings
Govet GovetSettings
Grouper GrouperSettings
Iface IfaceSettings
ImportAs ImportAsSettings
Inamedparam INamedParamSettings
InterfaceBloat InterfaceBloatSettings
Expand Down Expand Up @@ -648,6 +649,11 @@ type GrouperSettings struct {
VarRequireGrouping bool `mapstructure:"var-require-grouping"`
}

type IfaceSettings struct {
Enable []string `mapstructure:"enable"`
Settings map[string]map[string]any `mapstructure:"settings"`
}

type ImportAsSettings struct {
Alias []ImportAsAlias
NoUnaliased bool `mapstructure:"no-unaliased"`
Expand Down
60 changes: 60 additions & 0 deletions pkg/golinters/iface/iface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package iface

import (
"slices"
"strings"

"github.com/uudashr/iface/identical"
"github.com/uudashr/iface/opaque"
"github.com/uudashr/iface/unused"
"golang.org/x/exp/maps"
"golang.org/x/tools/go/analysis"

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

func New(settings *config.IfaceSettings) *goanalysis.Linter {
var conf map[string]map[string]any
if settings != nil {
conf = settings.Settings
}

return goanalysis.NewLinter(
"iface",
"Detect the incorrect use of interfaces, helping developers avoid interface pollution.",
analyzersFromSettings(settings),
conf,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}

func analyzersFromSettings(settings *config.IfaceSettings) []*analysis.Analyzer {
allAnalyzers := map[string]*analysis.Analyzer{
"unused": unused.Analyzer,
"identical": identical.Analyzer,
"opaque": opaque.Analyzer,
}

if settings == nil || len(settings.Enable) == 0 {
analyzers := maps.Values(allAnalyzers)

// To have a deterministic order.
slices.SortFunc(analyzers, func(a *analysis.Analyzer, b *analysis.Analyzer) int {
return strings.Compare(a.Name, b.Name)
})

return analyzers
}

var analyzers []*analysis.Analyzer
for _, name := range uniqueNames(settings.Enable) {
analyzers = append(analyzers, allAnalyzers[name])
}

return analyzers
}

func uniqueNames(names []string) []string {
slices.Sort(names)
return slices.Compact(names)
}
11 changes: 11 additions & 0 deletions pkg/golinters/iface/iface_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package iface

import (
"testing"

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

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

// identical

type Pinger interface { // want "identical: interface Pinger contains identical methods or type constraints from another interface, causing redundancy"
Ping() error
}

type Healthcheck interface { // want "identical: interface Healthcheck contains identical methods or type constraints from another interface, causing redundancy"
Ping() error
}

// opaque

type Server interface {
Serve() error
}

type server struct {
addr string
}

func (s server) Serve() error {
return nil
}

func NewServer(addr string) Server { // want "opaque: NewServer function return Server interface at the 1st result, abstract a single concrete implementation of \\*server"
return &server{addr: addr}
}

// unused

type User struct {
ID string
Name string
}

type UserRepository interface { // want "unused: interface UserRepository is declared but not used within the package"
UserOf(id string) (*User, error)
}

type UserRepositorySQL struct {
}

func (r *UserRepositorySQL) UserOf(id string) (*User, error) {
return nil, nil
}

type Granter interface {
Grant(permission string) error
}

func AllowAll(g Granter) error {
return g.Grant("all")
}

type Allower interface {
Allow(permission string) error
}

func Allow(x interface{}) {
_ = x.(Allower)
}
65 changes: 65 additions & 0 deletions pkg/golinters/iface/testdata/iface_identical.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//golangcitest:args -Eiface
//golangcitest:config_path testdata/iface_identical.yml
package testdata

// identical

type Pinger interface { // want "identical: interface Pinger contains identical methods or type constraints from another interface, causing redundancy"
Ping() error
}

type Healthcheck interface { // want "identical: interface Healthcheck contains identical methods or type constraints from another interface, causing redundancy"
Ping() error
}

// opaque

type Server interface {
Serve() error
}

type server struct {
addr string
}

func (s server) Serve() error {
return nil
}

func NewServer(addr string) Server {
return &server{addr: addr}
}

// unused

type User struct {
ID string
Name string
}

type UserRepository interface {
UserOf(id string) (*User, error)
}

type UserRepositorySQL struct {
}

func (r *UserRepositorySQL) UserOf(id string) (*User, error) {
return nil, nil
}

type Granter interface {
Grant(permission string) error
}

func AllowAll(g Granter) error {
return g.Grant("all")
}

type Allower interface {
Allow(permission string) error
}

func Allow(x interface{}) {
_ = x.(Allower)
}
4 changes: 4 additions & 0 deletions pkg/golinters/iface/testdata/iface_identical.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters-settings:
iface:
enable:
- identical
Loading
Loading