Skip to content

Commit 7004736

Browse files
committed
Enable the unexported analyzer for iface
1 parent 2691aac commit 7004736

13 files changed

+219
-31
lines changed

.golangci.next.reference.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,7 @@ linters-settings:
19881988
- identical # Identifies interfaces in the same package that have identical method sets.
19891989
- unused # Identifies interfaces that are not used anywhere in the same package where the interface is defined.
19901990
- opaque # Identifies functions that return interfaces, but the actual returned value is always a single concrete implementation.
1991+
- unexported # Identifies interfaces that are not exported but are used in exported functions or methods as parameters or return values.
19911992
settings:
19921993
unused:
19931994
# List of packages path to exclude from the check.

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ require (
115115
github.com/ultraware/funlen v0.2.0
116116
github.com/ultraware/whitespace v0.2.0
117117
github.com/uudashr/gocognit v1.2.0
118-
github.com/uudashr/iface v1.3.1
118+
github.com/uudashr/iface v1.4.0
119119
github.com/valyala/quicktemplate v1.8.0
120120
github.com/xen0n/gosmopolitan v1.2.2
121121
github.com/yagipy/maintidx v1.0.0
@@ -126,8 +126,8 @@ require (
126126
go-simpler.org/sloglint v0.9.0
127127
go.uber.org/automaxprocs v1.6.0
128128
golang.org/x/mod v0.24.0
129-
golang.org/x/sys v0.31.0
130-
golang.org/x/tools v0.31.0
129+
golang.org/x/sys v0.33.0
130+
golang.org/x/tools v0.33.0
131131
gopkg.in/yaml.v3 v3.0.1
132132
honnef.co/go/tools v0.6.1
133133
mvdan.cc/gofumpt v0.7.0
@@ -196,7 +196,7 @@ require (
196196
go.uber.org/zap v1.24.0 // indirect
197197
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
198198
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
199-
golang.org/x/sync v0.12.0 // indirect
199+
golang.org/x/sync v0.14.0 // indirect
200200
golang.org/x/text v0.22.0 // indirect
201201
google.golang.org/protobuf v1.36.5 // indirect
202202
gopkg.in/ini.v1 v1.67.0 // indirect

go.sum

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/iface/iface.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/uudashr/iface/identical"
77
"github.com/uudashr/iface/opaque"
8+
"github.com/uudashr/iface/unexported"
89
"github.com/uudashr/iface/unused"
910
"golang.org/x/tools/go/analysis"
1011

@@ -28,9 +29,10 @@ func New(settings *config.IfaceSettings) *goanalysis.Linter {
2829

2930
func analyzersFromSettings(settings *config.IfaceSettings) []*analysis.Analyzer {
3031
allAnalyzers := map[string]*analysis.Analyzer{
31-
"identical": identical.Analyzer,
32-
"unused": unused.Analyzer,
33-
"opaque": opaque.Analyzer,
32+
"identical": identical.Analyzer,
33+
"unused": unused.Analyzer,
34+
"opaque": opaque.Analyzer,
35+
"unexported": unexported.Analyzer,
3436
}
3537

3638
if settings == nil || len(settings.Enable) == 0 {

pkg/golinters/iface/testdata/iface_all.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import "fmt"
66

77
// identical
88

9-
type Pinger interface { // want "identical: interface Pinger contains identical methods or type constraints from another interface, causing redundancy"
9+
type Pinger interface { // want "identical: interface 'Pinger' contains identical methods or type constraints with another interface, causing redundancy"
1010
Ping() error
1111
}
1212

13-
type Healthcheck interface { // want "identical: interface Healthcheck contains identical methods or type constraints from another interface, causing redundancy"
13+
type Healthcheck interface { // want "identical: interface 'Healthcheck' contains identical methods or type constraints with another interface, causing redundancy"
1414
Ping() error
1515
}
1616

@@ -28,7 +28,7 @@ func (s server) Serve() error {
2828
return nil
2929
}
3030

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

@@ -39,7 +39,7 @@ type User struct {
3939
Name string
4040
}
4141

42-
type UserRepository interface { // want "unused: interface UserRepository is declared but not used within the package"
42+
type UserRepository interface { // want "unused: interface 'UserRepository' is declared but not used within the package"
4343
UserOf(id string) (*User, error)
4444
}
4545

@@ -66,3 +66,19 @@ func Allow(x any) {
6666
_ = x.(Allower)
6767
fmt.Println("allow")
6868
}
69+
70+
// unexported
71+
72+
type unexportedReader interface {
73+
Read([]byte) (int, error)
74+
}
75+
76+
func ReadAll(r unexportedReader) ([]byte, error) { // want "unexported interface 'unexportedReader' used as parameter in exported function 'ReadAll'"
77+
buf := make([]byte, 1024)
78+
_, err := r.Read(buf)
79+
return buf, err
80+
}
81+
82+
func NewUnexportedReader() unexportedReader { // want "unexported interface 'unexportedReader' used as return value in exported function 'NewUnexportedReader'"
83+
return nil // stub
84+
}

pkg/golinters/iface/testdata/iface_all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ linters-settings:
44
- unused
55
- identical
66
- opaque
7+
- unexported

pkg/golinters/iface/testdata/iface_cgo.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ func _() {
2424

2525
// identical
2626

27-
type Pinger interface { // want "identical: interface Pinger contains identical methods or type constraints from another interface, causing redundancy"
27+
type Pinger interface { // want "identical: interface 'Pinger' contains identical methods or type constraints with another interface, causing redundancy"
2828
Ping() error
2929
}
3030

31-
type Healthcheck interface { // want "identical: interface Healthcheck contains identical methods or type constraints from another interface, causing redundancy"
31+
type Healthcheck interface { // want "identical: interface 'Healthcheck' contains identical methods or type constraints with another interface, causing redundancy"
3232
Ping() error
3333
}
3434

@@ -84,3 +84,19 @@ func Allow(x any) {
8484
_ = x.(Allower)
8585
fmt.Println("allow")
8686
}
87+
88+
// unexported
89+
90+
type unexportedReader interface {
91+
Read([]byte) (int, error)
92+
}
93+
94+
func ReadAll(r unexportedReader) ([]byte, error) {
95+
buf := make([]byte, 1024)
96+
_, err := r.Read(buf)
97+
return buf, err
98+
}
99+
100+
func NewUnexportedReader() unexportedReader {
101+
return nil // stub
102+
}

pkg/golinters/iface/testdata/iface_default.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import "fmt"
55

66
// identical
77

8-
type Pinger interface { // want "identical: interface Pinger contains identical methods or type constraints from another interface, causing redundancy"
8+
type Pinger interface { // want "identical: interface 'Pinger' contains identical methods or type constraints with another interface, causing redundancy"
99
Ping() error
1010
}
1111

12-
type Healthcheck interface { // want "identical: interface Healthcheck contains identical methods or type constraints from another interface, causing redundancy"
12+
type Healthcheck interface { // want "identical: interface 'Healthcheck' contains identical methods or type constraints with another interface, causing redundancy"
1313
Ping() error
1414
}
1515

@@ -65,3 +65,19 @@ func Allow(x any) {
6565
_ = x.(Allower)
6666
fmt.Println("allow")
6767
}
68+
69+
// unexported
70+
71+
type unexportedReader interface {
72+
Read([]byte) (int, error)
73+
}
74+
75+
func ReadAll(r unexportedReader) ([]byte, error) {
76+
buf := make([]byte, 1024)
77+
_, err := r.Read(buf)
78+
return buf, err
79+
}
80+
81+
func NewUnexportedReader() unexportedReader {
82+
return nil // stub
83+
}

pkg/golinters/iface/testdata/iface_identical.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import "fmt"
66

77
// identical
88

9-
type Pinger interface { // want "identical: interface Pinger contains identical methods or type constraints from another interface, causing redundancy"
9+
type Pinger interface { // want "identical: interface 'Pinger' contains identical methods or type constraints with another interface, causing redundancy"
1010
Ping() error
1111
}
1212

13-
type Healthcheck interface { // want "identical: interface Healthcheck contains identical methods or type constraints from another interface, causing redundancy"
13+
type Healthcheck interface { // want "identical: interface 'Healthcheck' contains identical methods or type constraints with another interface, causing redundancy"
1414
Ping() error
1515
}
1616

@@ -66,3 +66,19 @@ func Allow(x any) {
6666
_ = x.(Allower)
6767
fmt.Println("allow")
6868
}
69+
70+
// unexported
71+
72+
type unexportedReader interface {
73+
Read([]byte) (int, error)
74+
}
75+
76+
func ReadAll(r unexportedReader) ([]byte, error) {
77+
buf := make([]byte, 1024)
78+
_, err := r.Read(buf)
79+
return buf, err
80+
}
81+
82+
func NewUnexportedReader() unexportedReader {
83+
return nil // stub
84+
}

pkg/golinters/iface/testdata/iface_opaque.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (s server) Serve() error {
2828
return nil
2929
}
3030

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

@@ -66,3 +66,19 @@ func Allow(x any) {
6666
_ = x.(Allower)
6767
fmt.Println("allow")
6868
}
69+
70+
// unexported
71+
72+
type unexportedReader interface {
73+
Read([]byte) (int, error)
74+
}
75+
76+
func ReadAll(r unexportedReader) ([]byte, error) {
77+
buf := make([]byte, 1024)
78+
_, err := r.Read(buf)
79+
return buf, err
80+
}
81+
82+
func NewUnexportedReader() unexportedReader {
83+
return nil // stub
84+
}

0 commit comments

Comments
 (0)