1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00
Files
revive/rule/unexported_return.go

114 lines
2.5 KiB
Go
Raw Normal View History

2018-01-25 11:34:38 -08:00
package rule
import (
"fmt"
"go/ast"
"go/types"
"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 {
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
file.Pkg.TypeCheck()
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
for _, ret := range fn.Type.Results.List {
typ := file.Pkg.TypeOf(ret.Type)
if exportedType(typ) {
continue
}
failures = append(failures, lint.Failure{
Category: lint.FailureCategoryUnexportedTypeInAPI,
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 {
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()
switch {
2018-01-25 11:34:38 -08:00
// Builtin types have no package.
case obj.Pkg() == nil:
case obj.Exported():
default:
2022-04-10 11:55:13 +02:00
_, ok := t.Underlying().(*types.Interface)
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
}