1
2
3
4
5 package modernize
6
7 import (
8 "go/ast"
9
10 "golang.org/x/tools/go/analysis"
11 "golang.org/x/tools/go/analysis/passes/inspect"
12 "golang.org/x/tools/internal/analysis/analyzerutil"
13 "golang.org/x/tools/internal/versions"
14 )
15
16 var AnyAnalyzer = &analysis.Analyzer{
17 Name: "any",
18 Doc: analyzerutil.MustExtractDoc(doc, "any"),
19 Requires: []*analysis.Analyzer{inspect.Analyzer},
20 Run: runAny,
21 URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#any",
22 }
23
24
25 func runAny(pass *analysis.Pass) (any, error) {
26 for curFile := range filesUsingGoVersion(pass, versions.Go1_18) {
27 for curIface := range curFile.Preorder((*ast.InterfaceType)(nil)) {
28 iface := curIface.Node().(*ast.InterfaceType)
29
30 if iface.Methods.NumFields() == 0 {
31
32 if lookup(pass.TypesInfo, curIface, "any") == builtinAny {
33 pass.Report(analysis.Diagnostic{
34 Pos: iface.Pos(),
35 End: iface.End(),
36 Message: "interface{} can be replaced by any",
37 SuggestedFixes: []analysis.SuggestedFix{{
38 Message: "Replace interface{} by any",
39 TextEdits: []analysis.TextEdit{
40 {
41 Pos: iface.Pos(),
42 End: iface.End(),
43 NewText: []byte("any"),
44 },
45 },
46 }},
47 })
48 }
49 }
50 }
51 }
52 return nil, nil
53 }
54
View as plain text