2018-01-25 11:34:38 -08:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"go/ast"
|
|
|
|
|
"go/types"
|
|
|
|
|
|
2022-06-18 19:47:53 +03:00
|
|
|
"github.com/mgechev/revive/internal/typeparams"
|
2018-01-25 11:34:38 -08:00
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-01 17:44:41 +02:00
|
|
|
// UnexportedReturnRule warns when a public return is from unexported type.
|
2018-01-25 11:34:38 -08:00
|
|
|
type UnexportedReturnRule struct{}
|
|
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*UnexportedReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2025-06-06 09:48:30 +02:00
|
|
|
if !file.IsImportable() {
|
|
|
|
|
// Symbols defined in such files cannot be used in other packages.
|
|
|
|
|
// Therefore, we don't need to check for unexported return types.
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 11:34:38 -08:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
2025-04-10 07:57:56 +02:00
|
|
|
file.Pkg.TypeCheck()
|
|
|
|
|
|
2024-12-05 13:09:07 +01:00
|
|
|
for _, decl := range file.AST.Decls {
|
|
|
|
|
fn, ok := decl.(*ast.FuncDecl)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fn.Type.Results == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !fn.Name.IsExported() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thing := "func"
|
|
|
|
|
if fn.Recv != nil && len(fn.Recv.List) > 0 {
|
|
|
|
|
thing = "method"
|
|
|
|
|
if !ast.IsExported(typeparams.ReceiverType(fn)) {
|
|
|
|
|
// Don't report exported methods of unexported types,
|
|
|
|
|
// such as private implementations of sort.Interface.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-25 11:34:38 -08:00
|
|
|
|
2024-12-05 13:09:07 +01:00
|
|
|
for _, ret := range fn.Type.Results.List {
|
|
|
|
|
typ := file.Pkg.TypeOf(ret.Type)
|
|
|
|
|
if exportedType(typ) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
failures = append(failures, lint.Failure{
|
2025-01-18 13:16:19 +02:00
|
|
|
Category: lint.FailureCategoryUnexportedTypeInAPI,
|
2024-12-05 13:09:07 +01:00
|
|
|
Node: ret.Type,
|
|
|
|
|
Confidence: 0.8,
|
|
|
|
|
Failure: fmt.Sprintf("exported %s %s returns unexported type %s, which can be annoying to use",
|
|
|
|
|
thing, fn.Name.Name, typ),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
break // only flag one
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-25 11:34:38 -08:00
|
|
|
|
|
|
|
|
return failures
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*UnexportedReturnRule) Name() string {
|
2018-01-25 11:34:38 -08:00
|
|
|
return "unexported-return"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// exportedType reports whether typ is an exported type.
|
|
|
|
|
// It is imprecise, and will err on the side of returning true,
|
|
|
|
|
// such as for composite types.
|
|
|
|
|
func exportedType(typ types.Type) bool {
|
2025-06-18 16:58:16 +02:00
|
|
|
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
|
2018-01-25 11:34:38 -08:00
|
|
|
case *types.Named:
|
2022-04-10 11:55:13 +02:00
|
|
|
obj := t.Obj()
|
2018-06-11 14:22:33 -04:00
|
|
|
switch {
|
2018-01-25 11:34:38 -08:00
|
|
|
// Builtin types have no package.
|
2018-06-11 14:22:33 -04:00
|
|
|
case obj.Pkg() == nil:
|
|
|
|
|
case obj.Exported():
|
|
|
|
|
default:
|
2022-04-10 11:55:13 +02:00
|
|
|
_, ok := t.Underlying().(*types.Interface)
|
2018-06-11 14:22:33 -04:00
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
return true
|
2018-01-25 11:34:38 -08:00
|
|
|
case *types.Map:
|
2022-04-10 11:55:13 +02:00
|
|
|
return exportedType(t.Key()) && exportedType(t.Elem())
|
2018-01-25 11:34:38 -08:00
|
|
|
case interface {
|
|
|
|
|
Elem() types.Type
|
|
|
|
|
}: // array, slice, pointer, chan
|
2022-04-10 11:55:13 +02:00
|
|
|
return exportedType(t.Elem())
|
2018-01-25 11:34:38 -08:00
|
|
|
}
|
|
|
|
|
// Be conservative about other types, such as struct, interface, etc.
|
|
|
|
|
return true
|
|
|
|
|
}
|