| 1 | // Copyright 2020 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 testinggoroutinescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/ |
| 6 | |
| 7 | import ( |
| 8 | _ "embedscip-go gomod github.com/golang/go/src go1.25.0 embed/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 11 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 12 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 13 | |
| 14 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 15 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 16 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 17 | "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/" |
| 18 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 19 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 20 | ) |
| 21 | |
| 22 | //go:embed doc.go |
| 23 | var docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/doc. string |
| 24 | |
| 25 | var reportSubtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/reportSubtest. bool |
| 26 | |
| 27 | func initscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/init().() { |
| 28 | Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/Analyzer..Flagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Flags..BoolVarscip-go gomod github.com/golang/go/src go1.25.0 flag/FlagSet#BoolVar().(&reportSubtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/reportSubtest., "subtest", false, "whether to check if t.Run subtest is terminated correctly; experimental") |
| 29 | } |
| 30 | |
| 31 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/Analyzer. = &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 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "testinggoroutine", |
| 33 | 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/testinggoroutine`/doc., "testinggoroutine"), |
| 34 | 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/testinggoroutine", |
| 35 | 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#{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.}, |
| 36 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/run()., |
| 37 | } |
| 38 | |
| 39 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/run().(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) { |
| 40 | inspect := pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[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.].(*inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#) |
| 41 | |
| 42 | if !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.Importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/Imports().(pass.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Pkg., "testing") { |
| 43 | return nil, nil |
| 44 | } |
| 45 | |
| 46 | toDecl := localFunctionDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/localFunctionDecls().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., pass.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files.) |
| 47 | |
| 48 | // asyncs maps nodes whose statements will be executed concurrently |
| 49 | // with respect to some test function, to the call sites where they |
| 50 | // are invoked asynchronously. There may be multiple such call sites |
| 51 | // for e.g. test helpers. |
| 52 | asyncs := make(map[astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#][]*asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#) |
| 53 | var regions []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node# |
| 54 | addCall := func(c *asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#) { |
| 55 | if c != nil { |
| 56 | r := c.regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region. |
| 57 | if asyncs[r] == nil { |
| 58 | regions = append(regions, r) |
| 59 | } |
| 60 | asyncs[r] = append(asyncs[r], c) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Collect all of the go callee() and t.Run(name, callee) extents. |
| 65 | inspect.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Nodes().([]astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#{ |
| 66 | (*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#)(nil), |
| 67 | (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GoStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GoStmt#)(nil), |
| 68 | (*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#)(nil), |
| 69 | }, func(node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, push bool) bool { |
| 70 | if !push { |
| 71 | return false |
| 72 | } |
| 73 | switch node := node.(type) { |
| 74 | 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#: |
| 75 | return hasBenchmarkOrTestParamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/hasBenchmarkOrTestParams().(node) |
| 76 | |
| 77 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GoStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GoStmt#: |
| 78 | c := goAsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/goAsyncCall().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., node, toDecl) |
| 79 | addCall(c) |
| 80 | |
| 81 | case *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#: |
| 82 | c := tRunAsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/tRunAsyncCall().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., node) |
| 83 | addCall(c) |
| 84 | } |
| 85 | return true |
| 86 | }) |
| 87 | |
| 88 | // Check for t.Forbidden() calls within each region r that is a |
| 89 | // callee in some go r() or a t.Run("name", r). |
| 90 | // |
| 91 | // Also considers a special case when r is a go t.Forbidden() call. |
| 92 | for _, region := range regions { |
| 93 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(region, func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) bool { |
| 94 | if n == region { |
| 95 | return true // always descend into the region itself. |
| 96 | } else if asyncs[n] != nil { |
| 97 | return false // will be visited by another region. |
| 98 | } |
| 99 | |
| 100 | call, ok := n.(*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#) |
| 101 | if !ok { |
| 102 | return true |
| 103 | } |
| 104 | x, sel, fn := forbiddenMethodscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/forbiddenMethod().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., call) |
| 105 | if x == nil { |
| 106 | return true |
| 107 | } |
| 108 | |
| 109 | for _, e := range asyncs[region] { |
| 110 | if !withinScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/withinScope().(e.scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope., x) { |
| 111 | forbidden := formatMethodscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/formatMethod().(sel, fn) // e.g. "(*testing.T).Forbidden |
| 112 | |
| 113 | var context string |
| 114 | var where analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Rangescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Range# = e.asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async. // Put the report at the go fun() or t.Run(name, fun). |
| 115 | if _, local := e.funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun..(*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#); local { |
| 116 | where = call // Put the report at the t.Forbidden() call. |
| 117 | } else if id, ok := e.funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#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#); ok { |
| 118 | context = 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().(" (%s calls %s)", id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., forbidden) |
| 119 | } |
| 120 | if _, ok := e.asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GoStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GoStmt#); ok { |
| 121 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(where, "call to %s from a non-test goroutine%s", forbidden, context) |
| 122 | } else if reportSubtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/reportSubtest. { |
| 123 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(where, "call to %s on %s defined outside of the subtest%s", forbidden, x.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().(), context) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return true |
| 128 | }) |
| 129 | } |
| 130 | |
| 131 | return nil, nil |
| 132 | } |
| 133 | |
| 134 | func hasBenchmarkOrTestParamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/hasBenchmarkOrTestParams().(fnDecl *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#) bool { |
| 135 | // Check that the function's arguments include "*testing.T" or "*testing.B". |
| 136 | params := fnDecl.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. |
| 137 | |
| 138 | for _, param := range params { |
| 139 | if _, ok := typeIsTestingDotTOrBscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/typeIsTestingDotTOrB().(param.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type.); ok { |
| 140 | return true |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return false |
| 145 | } |
| 146 | |
| 147 | func typeIsTestingDotTOrBscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/typeIsTestingDotTOrB().(expr astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) (string, bool) { |
| 148 | starExpr, ok := expr.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.StarExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#) |
| 149 | if !ok { |
| 150 | return "", false |
| 151 | } |
| 152 | selExpr, ok := starExpr.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#X..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#) |
| 153 | if !ok { |
| 154 | return "", false |
| 155 | } |
| 156 | varPkg := selExpr.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X..(*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#) |
| 157 | if varPkg.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. != "testing" { |
| 158 | return "", false |
| 159 | } |
| 160 | |
| 161 | varTypeName := selExpr.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 162 | ok = varTypeName == "B" || varTypeName == "T" |
| 163 | return varTypeName, ok |
| 164 | } |
| 165 | |
| 166 | // asyncCall describes a region of code that needs to be checked for |
| 167 | // t.Forbidden() calls as it is started asynchronously from an async |
| 168 | // node go fun() or t.Run(name, fun). |
| 169 | type asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall# struct { |
| 170 | regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region. astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node# // region of code to check for t.Forbidden() calls. |
| 171 | asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async. astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node# // *ast.GoStmt or *ast.CallExpr (for t.Run) |
| 172 | scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope. astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node# // Report t.Forbidden() if t is not declared within scope. |
| 173 | funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun. astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr# // fun in go fun() or t.Run(name, fun) |
| 174 | } |
| 175 | |
| 176 | // withinScope returns true if x.Pos() is in [scope.Pos(), scope.End()]. |
| 177 | func withinScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/withinScope().(scope astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#, x *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#) bool { |
| 178 | if scope != nil { |
| 179 | return x.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Pos().() != tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos. && scope.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().() <= x.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Pos().() && x.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Pos().() <= scope.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().() |
| 180 | } |
| 181 | return false |
| 182 | } |
| 183 | |
| 184 | // goAsyncCall returns the extent of a call from a go fun() statement. |
| 185 | func goAsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/goAsyncCall().(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#, goStmt *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GoStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GoStmt#, toDecl func(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#) *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#) *asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall# { |
| 186 | call := goStmt.Callscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GoStmt#Call. |
| 187 | |
| 188 | fun := astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Unparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Unparen().(call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun.) |
| 189 | if id := typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.UsedIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/UsedIdent().(info, fun); id != nil { |
| 190 | if lit := funcLitInScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/funcLitInScope().(id); lit != nil { |
| 191 | return &asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#{regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region.: lit, asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async.: goStmt, scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope.: nil, funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun.: fun} |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if fn := typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.StaticCalleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/StaticCallee().(info, call); fn != nil { // static call or method in the package? |
| 196 | if decl := toDecl(fn); decl != nil { |
| 197 | return &asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#{regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region.: decl, asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async.: goStmt, scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope.: nil, funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun.: fun} |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Check go statement for go t.Forbidden() or go func(){t.Forbidden()}(). |
| 202 | return &asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#{regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region.: goStmt, asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async.: goStmt, scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope.: nil, funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun.: fun} |
| 203 | } |
| 204 | |
| 205 | // tRunAsyncCall returns the extent of a call from a t.Run("name", fun) expression. |
| 206 | func tRunAsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/tRunAsyncCall().(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#, call *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#) *asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall# { |
| 207 | if len(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.) != 2 { |
| 208 | return nil |
| 209 | } |
| 210 | run := 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, call) |
| 211 | if run, ok := run.(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#); !ok || !isMethodNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/isMethodNamed().(run, "testing", "Run") { |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | fun := astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Unparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Unparen().(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[1]) |
| 216 | if lit, ok := fun.(*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#); ok { // function lit? |
| 217 | return &asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#{regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region.: lit, asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async.: call, scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope.: lit, funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun.: fun} |
| 218 | } |
| 219 | |
| 220 | if id := typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.UsedIdentscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/UsedIdent().(info, fun); id != nil { |
| 221 | if lit := funcLitInScopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/funcLitInScope().(id); lit != nil { // function lit in variable? |
| 222 | return &asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#{regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region.: lit, asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async.: call, scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope.: lit, funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun.: fun} |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Check within t.Run(name, fun) for calls to t.Forbidden, |
| 227 | // e.g. t.Run(name, func(t *testing.T){ t.Forbidden() }) |
| 228 | return &asyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#{regionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#region.: call, asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#async.: call, scopescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#scope.: fun, funscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/asyncCall#fun.: fun} |
| 229 | } |
| 230 | |
| 231 | var forbiddenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/forbidden. = []string{ |
| 232 | "FailNow", |
| 233 | "Fatal", |
| 234 | "Fatalf", |
| 235 | "Skip", |
| 236 | "Skipf", |
| 237 | "SkipNow", |
| 238 | } |
| 239 | |
| 240 | // forbiddenMethod decomposes a call x.m() into (x, x.m, m) where |
| 241 | // x is a variable, x.m is a selection, and m is the static callee m. |
| 242 | // Returns (nil, nil, nil) if call is not of this form. |
| 243 | func forbiddenMethodscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/forbiddenMethod().(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#, call *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#) (*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#, *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Selectionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#, *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#) { |
| 244 | // Compare to typeutil.StaticCallee. |
| 245 | fun := astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Unparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Unparen().(call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun.) |
| 246 | selExpr, ok := fun.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#) |
| 247 | if !ok { |
| 248 | return nil, nil, nil |
| 249 | } |
| 250 | sel := info.Selectionsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Selections.[selExpr] |
| 251 | if sel == nil { |
| 252 | return nil, nil, nil |
| 253 | } |
| 254 | |
| 255 | var x *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var# |
| 256 | if id, ok := astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Unparenscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Unparen().(selExpr.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X.).(*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#); ok { |
| 257 | x, _ = info.Usesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Uses.[id].(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Var#) |
| 258 | } |
| 259 | if x == nil { |
| 260 | return nil, nil, nil |
| 261 | } |
| 262 | |
| 263 | fn, _ := sel.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Obj().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#) |
| 264 | if fn == nil || !isMethodNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/isMethodNamed().(fn, "testing", forbiddenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/forbidden....) { |
| 265 | return nil, nil, nil |
| 266 | } |
| 267 | return x, sel, fn |
| 268 | } |
| 269 | |
| 270 | func formatMethodscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/testinggoroutine`/formatMethod().(sel *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Selectionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#, fn *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#) string { |
| 271 | var ptr string |
| 272 | rtype := sel.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Recv().() |
| 273 | if p, ok := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(rtype).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#); ok { |
| 274 | ptr = "*" |
| 275 | rtype = p.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().() |
| 276 | } |
| 277 | return 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().("(%s%s).%s", ptr, rtype.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().(), fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Name().()) |
| 278 | } |