-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemptyifaces.go
60 lines (46 loc) · 1.16 KB
/
emptyifaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package consistent
import (
"go/ast"
"github.com/go-toolsmith/astcast"
"golang.org/x/tools/go/analysis"
)
const (
emptyIfacesAny = "any"
emptyIfacesIface = "iface"
)
var emptyIfacesFlagAllowedValues = []string{flagIgnore, emptyIfacesAny, emptyIfacesIface}
func checkEmptyIface(pass *analysis.Pass, node ast.Node, mode string) {
switch mode {
case flagIgnore:
return
case emptyIfacesAny:
checkEmptyIfaceAny(pass, node)
case emptyIfacesIface:
checkEmptyIfaceIface(pass, node)
}
}
func checkEmptyIfaceAny(pass *analysis.Pass, node ast.Node) {
itype := astcast.ToInterfaceType(node)
if itype == astcast.NilInterfaceType {
return
}
if itype.Methods != nil && itype.Methods.List != nil && len(itype.Methods.List) != 0 {
return
}
reportf(pass, node.Pos(), "use any instead of interface{}")
}
func checkEmptyIfaceIface(pass *analysis.Pass, node ast.Node) {
var typ ast.Expr
switch node2 := node.(type) {
case *ast.Field:
typ = node2.Type
case *ast.TypeSpec:
typ = node2.Type
case *ast.ValueSpec:
typ = node2.Type
}
if ident := astcast.ToIdent(typ); ident.Name != "any" {
return
}
reportf(pass, typ.Pos(), "use interface{} instead of any")
}