diff --git a/.golangci.reference.yml b/.golangci.reference.yml index d2fd03079557..d94c1a35c0d5 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1073,9 +1073,9 @@ linters-settings: alias: $1$2 interfacebloat: - # The number of allowed methods for an interface. + # The maximum number of methods allowed for an interface. # Default: 10 - len: 5 + max: 5 ireturn: # ireturn allows using `allow` and `reject` settings at the same time. diff --git a/go.mod b/go.mod index 2abb70188e18..93e54954d588 100644 --- a/go.mod +++ b/go.mod @@ -74,7 +74,7 @@ require ( github.com/ryancurrah/gomodguard v1.2.4 github.com/ryanrolds/sqlclosecheck v0.3.0 github.com/sanposhiho/wastedassign/v2 v2.0.6 - github.com/sashamelentyev/interfacebloat v1.0.0 + github.com/sashamelentyev/interfacebloat v1.1.0 github.com/sashamelentyev/usestdlibvars v1.10.0 github.com/securego/gosec/v2 v2.12.0 github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c diff --git a/go.sum b/go.sum index c902f02304f6..5fe90323cc35 100644 --- a/go.sum +++ b/go.sum @@ -600,8 +600,8 @@ github.com/ryanrolds/sqlclosecheck v0.3.0 h1:AZx+Bixh8zdUBxUA1NxbxVAS78vTPq4rCb8 github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA= github.com/sanposhiho/wastedassign/v2 v2.0.6 h1:+6/hQIHKNJAUixEj6EmOngGIisyeI+T3335lYTyxRoA= github.com/sanposhiho/wastedassign/v2 v2.0.6/go.mod h1:KyZ0MWTwxxBmfwn33zh3k1dmsbF2ud9pAAGfoLfjhtI= -github.com/sashamelentyev/interfacebloat v1.0.0 h1:p2qI+0Oj5KbLqpukhkJGP24GSyWPLWd3CgT74F7Tzg4= -github.com/sashamelentyev/interfacebloat v1.0.0/go.mod h1:v46OiPGzx/3fDMyC4gq2h6ecsJBAVdbMMQ854YCl574= +github.com/sashamelentyev/interfacebloat v1.1.0 h1:xdRdJp0irL086OyW1H/RTZTr1h/tMEOsumirXcOJqAw= +github.com/sashamelentyev/interfacebloat v1.1.0/go.mod h1:+Y9yU5YdTkrNvoX0xHc84dxiN1iBi9+G8zZIhPVoNjQ= github.com/sashamelentyev/usestdlibvars v1.10.0 h1:hBW8NyJSmHImE/vI1r3gmGWiFkHtnqwd3DnoqoPxxj4= github.com/sashamelentyev/usestdlibvars v1.10.0/go.mod h1:nBjNFNgqwmaJuFKBvv/KoewMqAQJ8Ijq0z1u2W3nQIo= github.com/securego/gosec/v2 v2.12.0 h1:CQWdW7ATFpvLSohMVsajscfyHJ5rsGmEXmsNcsDNmAg= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 2e5cef9707d2..242a1de1f3d8 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -456,7 +456,7 @@ type ImportAsAlias struct { } type InterfaceBloatSettings struct { - Len int `mapstructure:"len"` + Max int `mapstructure:"max"` } type IreturnSettings struct { diff --git a/pkg/golinters/interfacebloat.go b/pkg/golinters/interfacebloat.go index 709436c7e74a..f9cf81c8130c 100644 --- a/pkg/golinters/interfacebloat.go +++ b/pkg/golinters/interfacebloat.go @@ -14,7 +14,7 @@ func NewInterfaceBloat(settings *config.InterfaceBloatSettings) *goanalysis.Lint cfgMap := make(map[string]map[string]interface{}) if settings != nil { cfgMap[a.Name] = map[string]interface{}{ - analyzer.InterfaceLenFlag: settings.Len, + analyzer.InterfaceMaxMethodsFlag: settings.Max, } } diff --git a/test/testdata/interfacebloat.go b/test/testdata/interfacebloat.go index 696e220b79fd..bee050ff7458 100644 --- a/test/testdata/interfacebloat.go +++ b/test/testdata/interfacebloat.go @@ -3,48 +3,124 @@ package testdata import "time" -type _ interface { // ERROR "length of interface greater than 10" - a() time.Duration - b() - c() - d() - f() - g() - h() - i() - j() - k() - l() -} - -func _() { - var _ interface { // ERROR "length of interface greater than 10" - a() time.Duration - b() - c() - d() - f() - g() - h() - i() - j() - k() - l() +type InterfaceBloatExample01 interface { // ERROR "the interface has more than 10 methods: 11" + a01() time.Duration + a02() + a03() + a04() + a05() + a06() + a07() + a08() + a09() + a10() + a11() +} + +func InterfaceBloatExample02() { + var _ interface { // ERROR "the interface has more than 10 methods: 11" + a01() time.Duration + a02() + a03() + a04() + a05() + a06() + a07() + a08() + a09() + a10() + a11() + } +} + +func InterfaceBloatExample03() interface { // ERROR "the interface has more than 10 methods: 11" + a01() time.Duration + a02() + a03() + a04() + a05() + a06() + a07() + a08() + a09() + a10() + a11() +} { + return nil +} + +type InterfaceBloatExample04 struct { + Foo interface { // ERROR "the interface has more than 10 methods: 11" + a01() time.Duration + a02() + a03() + a04() + a05() + a06() + a07() + a08() + a09() + a10() + a11() + } +} + +type InterfaceBloatSmall01 interface { + a01() time.Duration + a02() + a03() + a04() + a05() +} + +type InterfaceBloatSmall02 interface { + a06() + a07() + a08() + a09() + a10() + a11() +} + +type InterfaceBloatExample05 interface { + InterfaceBloatSmall01 + InterfaceBloatSmall02 +} + +type InterfaceBloatExample06 interface { + interface { // ERROR "the interface has more than 10 methods: 11" + a01() time.Duration + a02() + a03() + a04() + a05() + a06() + a07() + a08() + a09() + a10() + a11() } } -func __() interface { // ERROR "length of interface greater than 10" - a() time.Duration - b() - c() - d() - f() - g() - h() - i() - j() - k() - l() +type InterfaceBloatTypeGeneric interface { + ~uint8 | ~uint16 | ~uint32 | ~uint64 | uint | + ~int8 | ~int16 | ~int32 | ~int64 | int | + ~float32 | ~float64 | + ~string +} + +func InterfaceBloatExampleNoProblem() interface { + a01() time.Duration + a02() + a03() + a04() + a05() + a06() + a07() + a08() + a09() + a10() } { return nil }