diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 5a88fa85..67481a64 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -231,7 +231,15 @@ func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePa // Get all required imports, and generate unique names for them all. im := pkg.Imports() im[gomockImportPath] = true - im["reflect"] = true + + // Only import reflect if it's used. We only use reflect in mocked methods + // so only import if any of the mocked interfaces have methods. + for _, intf := range pkg.Interfaces { + if len(intf.Methods) > 0 { + im["reflect"] = true + break + } + } // Sort keys to make import alias generation predictable sorted_paths := make([]string, len(im), len(im)) diff --git a/mockgen/tests/empty_interface/input.go b/mockgen/tests/empty_interface/input.go new file mode 100644 index 00000000..bbcb3fc8 --- /dev/null +++ b/mockgen/tests/empty_interface/input.go @@ -0,0 +1,4 @@ +//go:generate mockgen -package empty_interface -destination mock.go -source input.go +package empty_interface + +type Empty interface{} diff --git a/mockgen/tests/empty_interface/mock.go b/mockgen/tests/empty_interface/mock.go new file mode 100644 index 00000000..0fc5b0c7 --- /dev/null +++ b/mockgen/tests/empty_interface/mock.go @@ -0,0 +1,32 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: input.go + +// Package empty_interface is a generated GoMock package. +package empty_interface + +import ( + gomock "github.com/golang/mock/gomock" +) + +// MockEmpty is a mock of Empty interface +type MockEmpty struct { + ctrl *gomock.Controller + recorder *MockEmptyMockRecorder +} + +// MockEmptyMockRecorder is the mock recorder for MockEmpty +type MockEmptyMockRecorder struct { + mock *MockEmpty +} + +// NewMockEmpty creates a new mock instance +func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { + mock := &MockEmpty{ctrl: ctrl} + mock.recorder = &MockEmptyMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { + return m.recorder +}