diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 81d342e7dc9d..d2d99e234700 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2345,6 +2345,7 @@ linters: - goimports - golint - gomnd + - gomockcontrollerfinish - gomoddirectives - gomodguard - goprintffuncname @@ -2465,6 +2466,7 @@ linters: - goimports - golint - gomnd + - gomockcontrollerfinish - gomoddirectives - gomodguard - goprintffuncname diff --git a/go.mod b/go.mod index 2bb5c0cf5e7b..3f7c31c7ceb5 100644 --- a/go.mod +++ b/go.mod @@ -54,6 +54,7 @@ require ( github.com/gostaticanalysis/nilerr v0.1.1 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-version v1.6.0 + github.com/hendrywiranto/gomockcontrollerfinish v0.1.0 github.com/hexops/gotextdiff v1.0.3 github.com/jgautheron/goconst v1.6.0 github.com/jingyugao/rowserrcheck v1.1.1 diff --git a/go.sum b/go.sum index b991df1a9f65..20c95b31d54f 100644 --- a/go.sum +++ b/go.sum @@ -204,6 +204,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= +github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -302,6 +303,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hendrywiranto/gomockcontrollerfinish v0.1.0 h1:ihIJrKRGZxafMxEJ6AYBtuIoaK5ARvZbcF2M8aXRNF8= +github.com/hendrywiranto/gomockcontrollerfinish v0.1.0/go.mod h1:yjM+7ZeTeKedi2n9Ovgnurm5lt7ptjpUuD79AkZ8Mgw= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/pkg/golinters/gomockcontrollerfinish.go b/pkg/golinters/gomockcontrollerfinish.go new file mode 100644 index 000000000000..21dfc1a536c0 --- /dev/null +++ b/pkg/golinters/gomockcontrollerfinish.go @@ -0,0 +1,21 @@ +package golinters + +import ( + "github.com/hendrywiranto/gomockcontrollerfinish/pkg/analyzer" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewGomockControllerFinish() *goanalysis.Linter { + analyzers := []*analysis.Analyzer{ + analyzer.New(), + } + + return goanalysis.NewLinter( + "gomockcontrollerfinish", + "Checks whether an unnecessary call to .Finish() on gomock.Controller exists", + analyzers, + nil, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index bdccfd350a77..036f74518d16 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -829,6 +829,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithLoadForGoAnalysis(). WithURL("https://github.com/Antonboom/testifylint"), + linter.NewConfig(golinters.NewGomockControllerFinish()). + WithSince("v1.56.0"). + WithPresets(linter.PresetTest). + WithLoadForGoAnalysis(). + WithURL("https://github.com/hendrywiranto/gomockcontrollerfinish"), + linter.NewConfig(golinters.NewTestpackage(testpackageCfg)). WithSince("v1.25.0"). WithPresets(linter.PresetStyle, linter.PresetTest). diff --git a/test/linters_test.go b/test/linters_test.go index 75d4f44adf1d..9f7a7e4ce599 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -33,6 +33,7 @@ func TestSourcesFromTestdataSubDir(t *testing.T) { "ginkgolinter", "zerologlint", "protogetter", + "gomockcontrollerfinish", } for _, dir := range subDirs { diff --git a/test/testdata/gomockcontrollerfinish/go.mod b/test/testdata/gomockcontrollerfinish/go.mod new file mode 100644 index 000000000000..d5a72746c2a6 --- /dev/null +++ b/test/testdata/gomockcontrollerfinish/go.mod @@ -0,0 +1,5 @@ +module gomockcontrollerfinish + +go 1.20 + +require github.com/golang/mock v1.6.0 diff --git a/test/testdata/gomockcontrollerfinish/go.sum b/test/testdata/gomockcontrollerfinish/go.sum new file mode 100644 index 000000000000..d0671277e22f --- /dev/null +++ b/test/testdata/gomockcontrollerfinish/go.sum @@ -0,0 +1,25 @@ +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/test/testdata/gomockcontrollerfinish/gomockcontrollerfinish_original_test.go b/test/testdata/gomockcontrollerfinish/gomockcontrollerfinish_original_test.go new file mode 100644 index 000000000000..fc8f539edbf9 --- /dev/null +++ b/test/testdata/gomockcontrollerfinish/gomockcontrollerfinish_original_test.go @@ -0,0 +1,36 @@ +//golangcitest:args -Egomockcontrollerfinish +package gomockcontrollerfinish + +import ( + "testing" + + "github.com/golang/mock/gomock" +) + +func TestFinishCall(t *testing.T) { + ctrl := gomock.NewController(t) + ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestFinishCallDefer(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestFinishCallWithoutT(t *testing.T) { + ctrl := gomock.NewController(nil) + ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestFinsihCallInAnotherFunction(t *testing.T) { + ctrl := gomock.NewController(t) + callFinish(ctrl) +} + +func callFinish(ctrl *gomock.Controller) { + ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestNoFinishCall(t *testing.T) { + gomock.NewController(t) +} diff --git a/test/testdata/gomockcontrollerfinish/gomockcontrollerfinish_renamed_pkg_test.go b/test/testdata/gomockcontrollerfinish/gomockcontrollerfinish_renamed_pkg_test.go new file mode 100644 index 000000000000..c8dcff5cefde --- /dev/null +++ b/test/testdata/gomockcontrollerfinish/gomockcontrollerfinish_renamed_pkg_test.go @@ -0,0 +1,36 @@ +//golangcitest:args -Egomockcontrollerfinish +package gomockcontrollerfinish + +import ( + "testing" + + gomockRenamed "github.com/golang/mock/gomock" +) + +func TestRenamedFinishCall(t *testing.T) { + ctrl := gomockRenamed.NewController(t) + ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestRenamedFinishCallDefer(t *testing.T) { + ctrl := gomockRenamed.NewController(t) + defer ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestRenamedFinishCallWithoutT(t *testing.T) { + ctrl := gomockRenamed.NewController(nil) + ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestRenamedFinsihCallInAnotherFunction(t *testing.T) { + ctrl := gomockRenamed.NewController(t) + callFinish(ctrl) +} + +func callFinish(ctrl *gomockRenamed.Controller) { + ctrl.Finish() // want "calling Finish on gomock.Controller is no longer needed" +} + +func TestNoFinishCall(t *testing.T) { + gomockRenamed.NewController(t) +}