From 87bd73b28e89671d2215402fc3bffb5a321b2ad9 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Wed, 18 Jun 2025 16:58:16 +0200 Subject: [PATCH] unexported-return: fix type aliases support (#1408) --- rule/unexported_return.go | 13 ++++++++++++- testdata/golint/unexported_return.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/rule/unexported_return.go b/rule/unexported_return.go index 7a51d71..e82dcff 100644 --- a/rule/unexported_return.go +++ b/rule/unexported_return.go @@ -78,7 +78,18 @@ func (*UnexportedReturnRule) Name() string { // It is imprecise, and will err on the side of returning true, // such as for composite types. func exportedType(typ types.Type) bool { - switch t := types.Unalias(typ).(type) { + switch t := typ.(type) { + case *types.Alias: + obj := t.Obj() + switch { + // Builtin types have no package. + case obj.Pkg() == nil: + case obj.Exported(): + default: + _, ok := t.Underlying().(*types.Interface) + return ok + } + return true case *types.Named: obj := t.Obj() switch { diff --git a/testdata/golint/unexported_return.go b/testdata/golint/unexported_return.go index 226e5d3..56329d2 100644 --- a/testdata/golint/unexported_return.go +++ b/testdata/golint/unexported_return.go @@ -58,3 +58,31 @@ type int struct{} func ExportedIntReturner() int { // MATCH /exported func ExportedIntReturner returns unexported type foo.int, which can be annoying to use/ return int{} } + +type config struct { + N int +} + +// Option ... +type Option = option + +type option func(*config) + +// WithN ... +func WithN(n int) Option { + return func(c *config) { + c.N = n + } +} + +type b = A + +// A ... +type A func(*config) + +// WithA ... +func WithA(n int) b { // MATCH /exported func WithA returns unexported type foo.b, which can be annoying to use/ + return func(c *config) { + c.N = n + } +}