Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new linter gomockcontrollerfinish #4202

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,7 @@ linters:
- goimports
- golint
- gomnd
- gomockcontrollerfinish
- gomoddirectives
- gomodguard
- goprintffuncname
Expand Down Expand Up @@ -2465,6 +2466,7 @@ linters:
- goimports
- golint
- gomnd
- gomockcontrollerfinish
- gomoddirectives
- gomodguard
- goprintffuncname
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/golinters/gomockcontrollerfinish.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions test/linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestSourcesFromTestdataSubDir(t *testing.T) {
"ginkgolinter",
"zerologlint",
"protogetter",
"gomockcontrollerfinish",
}

for _, dir := range subDirs {
Expand Down
5 changes: 5 additions & 0 deletions test/testdata/gomockcontrollerfinish/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module gomockcontrollerfinish

go 1.20

require github.com/golang/mock v1.6.0
25 changes: 25 additions & 0 deletions test/testdata/gomockcontrollerfinish/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
Loading