Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit c34cdb4

Browse files
authored
Merge pull request #170 from golang/iss102
Fix output for empty interfaces. Added a test.
2 parents 44f7022 + b391ab3 commit c34cdb4

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

mockgen/mockgen.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,15 @@ func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePa
231231
// Get all required imports, and generate unique names for them all.
232232
im := pkg.Imports()
233233
im[gomockImportPath] = true
234-
im["reflect"] = true
234+
235+
// Only import reflect if it's used. We only use reflect in mocked methods
236+
// so only import if any of the mocked interfaces have methods.
237+
for _, intf := range pkg.Interfaces {
238+
if len(intf.Methods) > 0 {
239+
im["reflect"] = true
240+
break
241+
}
242+
}
235243

236244
// Sort keys to make import alias generation predictable
237245
sorted_paths := make([]string, len(im), len(im))
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//go:generate mockgen -package empty_interface -destination mock.go -source input.go
2+
package empty_interface
3+
4+
type Empty interface{}

mockgen/tests/empty_interface/mock.go

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)