Skip to content

Commit 2b81ca8

Browse files
committed
fix: support any
1 parent 27114bd commit 2b81ca8

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

pkg/analyzer/analyzer.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var Analyzer = &analysis.Analyzer{
1616
Requires: []*analysis.Analyzer{inspect.Analyzer},
1717
}
1818

19-
func run(pass *analysis.Pass) (interface{}, error) {
19+
func run(pass *analysis.Pass) (any, error) {
2020
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
2121

2222
nodeFilter := []ast.Node{
@@ -50,19 +50,15 @@ func run(pass *analysis.Pass) (interface{}, error) {
5050
}
5151

5252
argsParamType, ok := params[len(params)-1].Type.(*ast.Ellipsis)
53-
if !ok { // args are not ellipsis (...args)
53+
if !ok {
54+
// args are not ellipsis (...args)
5455
return
5556
}
5657

57-
elementType, ok := argsParamType.Elt.(*ast.InterfaceType)
58-
if !ok { // args are not of interface type, but we need interface{}
58+
if !isAny(argsParamType) {
5959
return
6060
}
6161

62-
if elementType.Methods != nil && len(elementType.Methods.List) != 0 {
63-
return // has >= 1 method in interface, but we need an empty interface "interface{}"
64-
}
65-
6662
if strings.HasSuffix(funcDecl.Name.Name, "f") {
6763
return
6864
}
@@ -73,3 +69,22 @@ func run(pass *analysis.Pass) (interface{}, error) {
7369

7470
return nil, nil
7571
}
72+
73+
func isAny(ell *ast.Ellipsis) bool {
74+
switch elt := ell.Elt.(type) {
75+
case *ast.InterfaceType:
76+
if elt.Methods != nil && len(elt.Methods.List) != 0 {
77+
// has >= 1 method in interface, but we need an empty interface "interface{}"
78+
return false
79+
}
80+
81+
return true
82+
83+
case *ast.Ident:
84+
if elt.Name == "any" {
85+
return true
86+
}
87+
}
88+
89+
return false
90+
}

testdata/src/p/p.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ func prinfLikeFunc(format string, args ...interface{}) {} // want "printf-like f
2828
func prinfLikeFuncWithExtraArgs1(extraArg, format string, args ...interface{}) {} // want "printf-like formatting function"
2929

3030
func prinfLikeFuncWithExtraArgs2(extraArg int, format string, args ...interface{}) {} // want "printf-like formatting function"
31+
32+
func prinfLikeFuncAny(format string, args ...any) {} // want "printf-like formatting function"
33+
34+
func prinfLikeFuncWithExtraArgs1Any(extraArg, format string, args ...any) {} // want "printf-like formatting function"
35+
36+
func prinfLikeFuncWithExtraArgs2Any(extraArg int, format string, args ...any) {} // want "printf-like formatting function"

0 commit comments

Comments
 (0)