Skip to content

Commit 9cc3432

Browse files
committed
review
1 parent 76a37b6 commit 9cc3432

17 files changed

+338
-140
lines changed

.golangci.next.reference.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1223,15 +1223,16 @@ linters-settings:
12231223
var-require-grouping: true
12241224

12251225
iface:
1226-
# By default set to empty. Leave it empty means all analyzers are enabled.
1226+
# By default, set to empty.
1227+
# Empty means that all analyzers are enabled.
12271228
# Default: []
12281229
enable:
12291230
- unused
12301231
- identical
12311232
- opaque
12321233
settings:
12331234
unused:
1234-
# Comma-separated list of packages to exclude from the check.
1235+
# List of packages path to exclude from the check.
12351236
# Default: []
12361237
exclude:
12371238
- github.com/example/log

jsonschema/golangci.next.jsonschema.json

+11
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,17 @@
19281928
"items": {
19291929
"$ref": "#/definitions/iface-analyzers"
19301930
}
1931+
},
1932+
"settings": {
1933+
"type": "object",
1934+
"propertyNames": {
1935+
"$ref": "#/definitions/iface-analyzers"
1936+
},
1937+
"patternProperties": {
1938+
"^.*$": {
1939+
"type": "object"
1940+
}
1941+
}
19311942
}
19321943
}
19331944
},

pkg/config/linters_settings.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,8 @@ type GrouperSettings struct {
650650
}
651651

652652
type IfaceSettings struct {
653-
Enable []string `mapstructure:"enable"`
653+
Enable []string `mapstructure:"enable"`
654+
Settings map[string]map[string]any `mapstructure:"settings"`
654655
}
655656

656657
type ImportAsSettings struct {

pkg/golinters/iface/iface.go

+16-38
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,47 @@ import (
66
"github.com/uudashr/iface/identical"
77
"github.com/uudashr/iface/opaque"
88
"github.com/uudashr/iface/unused"
9+
"golang.org/x/exp/maps"
910
"golang.org/x/tools/go/analysis"
1011

1112
"github.com/golangci/golangci-lint/pkg/config"
1213
"github.com/golangci/golangci-lint/pkg/goanalysis"
1314
)
1415

15-
var allAnalyzers = []*analysis.Analyzer{
16-
unused.Analyzer,
17-
identical.Analyzer,
18-
opaque.Analyzer,
19-
}
20-
2116
func New(settings *config.IfaceSettings) *goanalysis.Linter {
2217
var conf map[string]map[string]any
23-
24-
analyzers := analyzersFromSettings(settings)
18+
if settings != nil {
19+
conf = settings.Settings
20+
}
2521

2622
return goanalysis.NewLinter(
2723
"iface",
2824
"Detect the incorrect use of interfaces, helping developers avoid interface pollution.",
29-
analyzers,
25+
analyzersFromSettings(settings),
3026
conf,
3127
).WithLoadMode(goanalysis.LoadModeTypesInfo)
3228
}
3329

3430
func analyzersFromSettings(settings *config.IfaceSettings) []*analysis.Analyzer {
35-
if settings == nil || len(settings.Enable) == 0 {
36-
return allAnalyzers
31+
allAnalyzers := map[string]*analysis.Analyzer{
32+
"unused": unused.Analyzer,
33+
"identical": identical.Analyzer,
34+
"opaque": opaque.Analyzer,
3735
}
3836

39-
enabledNames := uniqueNames(settings.Enable)
37+
if settings == nil || len(settings.Enable) == 0 {
38+
return maps.Values(allAnalyzers)
39+
}
4040

4141
var analyzers []*analysis.Analyzer
42-
43-
for _, a := range allAnalyzers {
44-
found := slices.ContainsFunc(enabledNames, func(name string) bool {
45-
return name == a.Name
46-
})
47-
48-
if !found {
49-
continue
50-
}
51-
52-
analyzers = append(analyzers, a)
42+
for _, name := range uniqueNames(settings.Enable) {
43+
analyzers = append(analyzers, allAnalyzers[name])
5344
}
5445

5546
return analyzers
5647
}
5748

5849
func uniqueNames(names []string) []string {
59-
if len(names) == 0 {
60-
return nil
61-
}
62-
63-
namesMap := map[string]struct{}{}
64-
for _, name := range names {
65-
namesMap[name] = struct{}{}
66-
}
67-
68-
uniqueNames := make([]string, 0, len(namesMap))
69-
70-
for name := range namesMap {
71-
uniqueNames = append(uniqueNames, name)
72-
}
73-
return uniqueNames
50+
slices.Sort(names)
51+
return slices.Compact(names)
7452
}

pkg/golinters/iface/iface_test.go

-61
This file was deleted.

pkg/golinters/iface/testdata/identical.go

-11
This file was deleted.

pkg/golinters/iface/testdata/iface.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//golangcitest:args -Eiface
2+
package testdata
3+
4+
// identical
5+
6+
type Pinger interface { // want "unused: interface Pinger is declared but not used within the package"
7+
Ping() error
8+
}
9+
10+
type Healthcheck interface { // want "unused: interface Healthcheck is declared but not used within the package"
11+
Ping() error
12+
}
13+
14+
// opaque
15+
16+
type Server interface {
17+
Serve() error
18+
}
19+
20+
type server struct {
21+
addr string
22+
}
23+
24+
func (s server) Serve() error {
25+
return nil
26+
}
27+
28+
func NewServer(addr string) Server { // want "opaque: NewServer function return Server interface at the 1st result, abstract a single concrete implementation of \\*server"
29+
return &server{addr: addr}
30+
}
31+
32+
// unused
33+
34+
type User struct {
35+
ID string
36+
Name string
37+
}
38+
39+
type UserRepository interface { // want "unused: interface UserRepository is declared but not used within the package"
40+
UserOf(id string) (*User, error)
41+
}
42+
43+
type UserRepositorySQL struct {
44+
}
45+
46+
func (r *UserRepositorySQL) UserOf(id string) (*User, error) {
47+
return nil, nil
48+
}
49+
50+
type Granter interface {
51+
Grant(permission string) error
52+
}
53+
54+
func AllowAll(g Granter) error {
55+
return g.Grant("all")
56+
}
57+
58+
type Allower interface {
59+
Allow(permission string) error
60+
}
61+
62+
func Allow(x interface{}) {
63+
_ = x.(Allower)
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//golangcitest:args -Eiface
2+
//golangcitest:config_path testdata/iface_identical.yml
3+
package testdata
4+
5+
// identical
6+
7+
type Pinger interface { // want "identical: interface Pinger contains identical methods or type constraints from another interface, causing redundancy"
8+
Ping() error
9+
}
10+
11+
type Healthcheck interface { // want "identical: interface Healthcheck contains identical methods or type constraints from another interface, causing redundancy"
12+
Ping() error
13+
}
14+
15+
// opaque
16+
17+
type Server interface {
18+
Serve() error
19+
}
20+
21+
type server struct {
22+
addr string
23+
}
24+
25+
func (s server) Serve() error {
26+
return nil
27+
}
28+
29+
func NewServer(addr string) Server {
30+
return &server{addr: addr}
31+
}
32+
33+
// unused
34+
35+
type User struct {
36+
ID string
37+
Name string
38+
}
39+
40+
type UserRepository interface {
41+
UserOf(id string) (*User, error)
42+
}
43+
44+
type UserRepositorySQL struct {
45+
}
46+
47+
func (r *UserRepositorySQL) UserOf(id string) (*User, error) {
48+
return nil, nil
49+
}
50+
51+
type Granter interface {
52+
Grant(permission string) error
53+
}
54+
55+
func AllowAll(g Granter) error {
56+
return g.Grant("all")
57+
}
58+
59+
type Allower interface {
60+
Allow(permission string) error
61+
}
62+
63+
func Allow(x interface{}) {
64+
_ = x.(Allower)
65+
}

0 commit comments

Comments
 (0)