-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Fernandez Ludovic <[email protected]>
- Loading branch information
Showing
17 changed files
with
496 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package iface | ||
|
||
import ( | ||
"slices" | ||
|
||
"github.com/uudashr/iface/identical" | ||
"github.com/uudashr/iface/opaque" | ||
"github.com/uudashr/iface/unused" | ||
"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{ | ||
"identical": identical.Analyzer, | ||
"unused": unused.Analyzer, | ||
"opaque": opaque.Analyzer, | ||
} | ||
|
||
if settings == nil || len(settings.Enable) == 0 { | ||
// Default enable `identical` analyzer only | ||
return []*analysis.Analyzer{identical.Analyzer} | ||
} | ||
|
||
var analyzers []*analysis.Analyzer | ||
for _, name := range uniqueNames(settings.Enable) { | ||
if _, ok := allAnalyzers[name]; !ok { | ||
// skip unknown analyzer | ||
continue | ||
} | ||
|
||
analyzers = append(analyzers, allAnalyzers[name]) | ||
} | ||
|
||
return analyzers | ||
} | ||
|
||
func uniqueNames(names []string) []string { | ||
slices.Sort(names) | ||
return slices.Compact(names) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//golangcitest:args -Eiface | ||
//golangcitest:config_path testdata/iface_all.yml | ||
package testdata | ||
|
||
import "fmt" | ||
|
||
// 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 any) { | ||
_ = x.(Allower) | ||
fmt.Println("allow") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
linters-settings: | ||
iface: | ||
enable: | ||
- unused | ||
- identical | ||
- opaque |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//golangcitest:args -Eiface | ||
package testdata | ||
|
||
import "fmt" | ||
|
||
// 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 any) { | ||
_ = x.(Allower) | ||
fmt.Println("allow") | ||
} |
Oops, something went wrong.