| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package modernizescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 11 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/" |
| 14 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/" |
| 15 | |
| 16 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 17 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 18 | "golang.org/x/tools/go/ast/edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/" |
| 19 | "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/" |
| 20 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 21 | typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/ "golang.org/x/tools/internal/analysis/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/" |
| 22 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 23 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 24 | "golang.org/x/tools/internal/typesinternal/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/" |
| 25 | "golang.org/x/tools/internal/versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/" |
| 26 | ) |
| 27 | |
| 28 | var TestingContextAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/TestingContextAnalyzer. = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 29 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "testingcontext", |
| 30 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.MustExtractDocscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/MustExtractDoc().(docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/doc., "testingcontext"), |
| 31 | Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 32 | inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer., |
| 33 | typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer., |
| 34 | }, |
| 35 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: testingContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/testingContext()., |
| 36 | URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#testingcontext", |
| 37 | } |
| 38 | |
| 39 | // The testingContext pass replaces calls to context.WithCancel from within |
| 40 | // tests to a use of testing.{T,B,F}.Context(), added in Go 1.24. |
| 41 | // |
| 42 | // Specifically, the testingContext pass suggests to replace: |
| 43 | // |
| 44 | // ctx, cancel := context.WithCancel(context.Background()) // or context.TODO |
| 45 | // defer cancel() |
| 46 | // |
| 47 | // with: |
| 48 | // |
| 49 | // ctx := t.Context() |
| 50 | // |
| 51 | // provided: |
| 52 | // |
| 53 | // - ctx and cancel are declared by the assignment |
| 54 | // - the deferred call is the only use of cancel |
| 55 | // - the call is within a test or subtest function |
| 56 | // - the relevant testing.{T,B,F} is named and not shadowed at the call |
| 57 | func testingContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/testingContext().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 58 | var ( |
| 59 | index = pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer.].(*typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#) |
| 60 | info = pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo. |
| 61 | |
| 62 | contextWithCancel = index.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().("context", "WithCancel") |
| 63 | ) |
| 64 | |
| 65 | calls: |
| 66 | for cur := range index.Callsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Calls().(contextWithCancel) { |
| 67 | call := cur.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 68 | // Have: context.WithCancel(...) |
| 69 | |
| 70 | arg, ok := call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 71 | if !ok { |
| 72 | continue |
| 73 | } |
| 74 | if !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsFunctionNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsFunctionNamed().(typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(info, arg), "context", "Background", "TODO") { |
| 75 | continue |
| 76 | } |
| 77 | // Have: context.WithCancel(context.{Background,TODO}()) |
| 78 | |
| 79 | parent := cur.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().() |
| 80 | assign, ok := parent.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#) |
| 81 | if !ok || assign.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Tok. != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.DEFINEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/DEFINE. { |
| 82 | continue |
| 83 | } |
| 84 | // Have: a, b := context.WithCancel(context.{Background,TODO}()) |
| 85 | |
| 86 | // Check that both a and b are declared, not redeclarations. |
| 87 | var lhs []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# |
| 88 | for _, expr := range assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs. { |
| 89 | id, ok := expr.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 90 | if !ok { |
| 91 | continue calls |
| 92 | } |
| 93 | obj, ok := info.Defsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Defs.[id] |
| 94 | if !ok { |
| 95 | continue calls |
| 96 | } |
| 97 | lhs = append(lhs, obj) |
| 98 | } |
| 99 | |
| 100 | next, ok := parent.NextSiblingscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#NextSibling().() |
| 101 | if !ok { |
| 102 | continue |
| 103 | } |
| 104 | defr, ok := next.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.DeferStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/DeferStmt#) |
| 105 | if !ok { |
| 106 | continue |
| 107 | } |
| 108 | deferId, ok := defr.Callscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/DeferStmt#Call..Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 109 | if !ok || !soleUseIsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/soleUseIs().(index, lhs[1], deferId) { |
| 110 | continue // b is used elsewhere |
| 111 | } |
| 112 | // Have: |
| 113 | // a, b := context.WithCancel(context.{Background,TODO}()) |
| 114 | // defer b() |
| 115 | |
| 116 | // Check that we are in a test func. |
| 117 | var testObj typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# // relevant testing.{T,B,F}, or nil |
| 118 | if curFunc, ok := enclosingFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/enclosingFunc().(cur); ok { |
| 119 | switch n := curFunc.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(type) { |
| 120 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#: |
| 121 | if ek, idx := curFunc.ParentEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#ParentEdge().(); ek == edgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/.CallExpr_Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/edge`/CallExpr_Args. && idx == 1 { |
| 122 | // Have: call(..., func(...) { ...context.WithCancel(...)... }) |
| 123 | obj := typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(info, curFunc.Parentscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Parent().().Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#)) |
| 124 | if (typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsMethodNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsMethodNamed().(obj, "testing", "T", "Run") || |
| 125 | typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsMethodNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsMethodNamed().(obj, "testing", "B", "Run")) && |
| 126 | len(n.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Names.) == 1 { |
| 127 | |
| 128 | // Have tb.Run(..., func(..., tb *testing.[TB]) { ...context.WithCancel(...)... } |
| 129 | testObj = info.Defsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Defs.[n.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Names.[0]] |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#: |
| 134 | testObj = isTestFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/isTestFn().(info, n) |
| 135 | } |
| 136 | } |
| 137 | if testObj != nil && analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.FileUsesGoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/FileUsesGoVersion().(pass, astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EnclosingFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EnclosingFile().(cur), versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.Go1_24scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/Go1_24.) { |
| 138 | // Have a test function. Check that we can resolve the relevant |
| 139 | // testing.{T,B,F} at the current position. |
| 140 | if _, obj := lhs[0].Parentscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Parent().().LookupParentscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#LookupParent().(testObj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), lhs[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pos().()); obj == testObj { |
| 141 | pass.Reportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Report.(analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#{ |
| 142 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(), |
| 143 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(), |
| 144 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("context.WithCancel can be modernized using %s.Context", testObj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().()), |
| 145 | SuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#SuggestedFixes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#{{ |
| 146 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("Replace context.WithCancel with %s.Context", testObj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().()), |
| 147 | TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{{ |
| 148 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Pos().(), |
| 149 | Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: defr.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/DeferStmt#End().(), |
| 150 | NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Appendfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Appendf().(nil, "%s := %s.Context()", lhs[0].Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), testObj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().()), |
| 151 | }}, |
| 152 | }}, |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | return nil, nil |
| 158 | } |
| 159 | |
| 160 | // soleUseIs reports whether id is the sole Ident that uses obj. |
| 161 | // (It returns false if there were no uses of obj.) |
| 162 | func soleUseIsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/soleUseIs().(index *typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#, obj typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#, id *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) bool { |
| 163 | empty := true |
| 164 | for use := range index.Usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Uses().(obj) { |
| 165 | empty = false |
| 166 | if use.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().() != id { |
| 167 | return false |
| 168 | } |
| 169 | } |
| 170 | return !empty |
| 171 | } |
| 172 | |
| 173 | // isTestFn checks whether fn is a test function (TestX, BenchmarkX, FuzzX), |
| 174 | // returning the corresponding types.Object of the *testing.{T,B,F} argument. |
| 175 | // Returns nil if fn is a test function, but the testing.{T,B,F} argument is |
| 176 | // unnamed (or _). |
| 177 | // |
| 178 | // TODO(rfindley): consider handling the case of an unnamed argument, by adding |
| 179 | // an edit to give the argument a name. |
| 180 | // |
| 181 | // Adapted from go/analysis/passes/tests. |
| 182 | // TODO(rfindley): consider refactoring to share logic. |
| 183 | func isTestFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/isTestFn().(info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#, fn *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# { |
| 184 | // Want functions with 0 results and 1 parameter. |
| 185 | if fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results. != nil && len(fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) > 0 || |
| 186 | fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params. == nil || |
| 187 | len(fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) != 1 || |
| 188 | len(fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Names.) != 1 { |
| 189 | |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | prefix := testKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/testKind().(fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) |
| 194 | if prefix == "" { |
| 195 | return nil |
| 196 | } |
| 197 | |
| 198 | if tparams := fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#TypeParams.; tparams != nil && len(tparams.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) > 0 { |
| 199 | return nil // test functions must not be generic |
| 200 | } |
| 201 | |
| 202 | obj := info.Defsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Defs.[fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Names.[0]] |
| 203 | if obj == nil { |
| 204 | return nil // e.g. _ *testing.T |
| 205 | } |
| 206 | |
| 207 | var name string |
| 208 | switch prefix { |
| 209 | case "Test": |
| 210 | name = "T" |
| 211 | case "Benchmark": |
| 212 | name = "B" |
| 213 | case "Fuzz": |
| 214 | name = "F" |
| 215 | } |
| 216 | |
| 217 | if !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsPointerToNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsPointerToNamed().(obj.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().(), "testing", name) { |
| 218 | return nil |
| 219 | } |
| 220 | return obj |
| 221 | } |
| 222 | |
| 223 | // testKind returns "Test", "Benchmark", or "Fuzz" if name is a valid resp. |
| 224 | // test, benchmark, or fuzz function name. Otherwise, isTestName returns "". |
| 225 | // |
| 226 | // Adapted from go/analysis/passes/tests.isTestName. |
| 227 | func testKindscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/testKind().(name string) string { |
| 228 | var prefix string |
| 229 | switch { |
| 230 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(name, "Test"): |
| 231 | prefix = "Test" |
| 232 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(name, "Benchmark"): |
| 233 | prefix = "Benchmark" |
| 234 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(name, "Fuzz"): |
| 235 | prefix = "Fuzz" |
| 236 | } |
| 237 | if prefix == "" { |
| 238 | return "" |
| 239 | } |
| 240 | suffix := name[len(prefix):] |
| 241 | if len(suffix) == 0 { |
| 242 | // "Test" is ok. |
| 243 | return prefix |
| 244 | } |
| 245 | r, _ := utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/DecodeRuneInString().(suffix) |
| 246 | if unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/.IsLowerscip-go gomod github.com/golang/go/src go1.25.0 unicode/IsLower().(r) { |
| 247 | return "" |
| 248 | } |
| 249 | return prefix |
| 250 | } |