| 1 | // Copyright 2012 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 loopclosurescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/ |
| 6 | |
| 7 | import ( |
| 8 | _ "embedscip-go gomod github.com/golang/go/src go1.25.0 embed/" |
| 9 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 11 | |
| 12 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 13 | "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/" |
| 14 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 15 | "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/" |
| 16 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 17 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 18 | "golang.org/x/tools/internal/versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/" |
| 19 | ) |
| 20 | |
| 21 | //go:embed doc.go |
| 22 | var docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/doc. string |
| 23 | |
| 24 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/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#{ |
| 25 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "loopclosure", |
| 26 | 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/loopclosure`/doc., "loopclosure"), |
| 27 | 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/loopclosure", |
| 28 | 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.}, |
| 29 | 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/loopclosure`/run()., |
| 30 | } |
| 31 | |
| 32 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/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) { |
| 33 | 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#) |
| 34 | |
| 35 | nodeFilter := []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#{ |
| 36 | (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#)(nil), |
| 37 | (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.RangeStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#)(nil), |
| 38 | (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ForStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#)(nil), |
| 39 | } |
| 40 | inspect.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Nodes().(nodeFilter, 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#, push bool) bool { |
| 41 | if !push { |
| 42 | // inspect.Nodes is slightly suboptimal as we only use push=true. |
| 43 | return true |
| 44 | } |
| 45 | // Find the variables updated by the loop statement. |
| 46 | var vars []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# |
| 47 | addVar := func(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#) { |
| 48 | if id, _ := 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#); id != nil { |
| 49 | if obj := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..ObjectOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#ObjectOf().(id); obj != nil { |
| 50 | vars = append(vars, obj) |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | var body *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt# |
| 55 | switch n := n.(type) { |
| 56 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#: |
| 57 | // Only traverse the file if its goversion is strictly before go1.22. |
| 58 | return !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, n, versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.Go1_22scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/Go1_22.) |
| 59 | |
| 60 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.RangeStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#: |
| 61 | body = n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Body. |
| 62 | addVar(n.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Key.) |
| 63 | addVar(n.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Value.) |
| 64 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ForStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#: |
| 65 | body = n.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Body. |
| 66 | switch post := n.Postscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Post..(type) { |
| 67 | case *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#: |
| 68 | // e.g. for p = head; p != nil; p = p.next |
| 69 | for _, lhs := range post.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs. { |
| 70 | addVar(lhs) |
| 71 | } |
| 72 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IncDecStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IncDecStmt#: |
| 73 | // e.g. for i := 0; i < n; i++ |
| 74 | addVar(post.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IncDecStmt#X.) |
| 75 | } |
| 76 | } |
| 77 | if vars == nil { |
| 78 | return true |
| 79 | } |
| 80 | |
| 81 | // Inspect statements to find function literals that may be run outside of |
| 82 | // the current loop iteration. |
| 83 | // |
| 84 | // For go, defer, and errgroup.Group.Go, we ignore all but the last |
| 85 | // statement, because it's hard to prove go isn't followed by wait, or |
| 86 | // defer by return. "Last" is defined recursively. |
| 87 | // |
| 88 | // TODO: consider allowing the "last" go/defer/Go statement to be followed by |
| 89 | // N "trivial" statements, possibly under a recursive definition of "trivial" |
| 90 | // so that checker could, for example, conclude that a go statement is |
| 91 | // followed by an if statement made of only trivial statements and trivial expressions, |
| 92 | // and hence the go statement could still be checked. |
| 93 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(body.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List., func(last astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#) { |
| 94 | var stmts []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt# |
| 95 | switch s := last.(type) { |
| 96 | 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#: |
| 97 | stmts = litStmtsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/litStmts().(s.Callscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GoStmt#Call..Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun.) |
| 98 | case *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#: |
| 99 | stmts = litStmtsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/litStmts().(s.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.) |
| 100 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ExprStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#: // check for errgroup.Group.Go |
| 101 | if call, ok := s.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#X..(*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#); ok { |
| 102 | stmts = litStmtsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/litStmts().(goInvokescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/goInvoke().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., call)) |
| 103 | } |
| 104 | } |
| 105 | for _, stmt := range stmts { |
| 106 | reportCapturedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/reportCaptured().(pass, vars, stmt) |
| 107 | } |
| 108 | }) |
| 109 | |
| 110 | // Also check for testing.T.Run (with T.Parallel). |
| 111 | // We consider every t.Run statement in the loop body, because there is |
| 112 | // no commonly used mechanism for synchronizing parallel subtests. |
| 113 | // It is of course theoretically possible to synchronize parallel subtests, |
| 114 | // though such a pattern is likely to be exceedingly rare as it would be |
| 115 | // fighting against the test runner. |
| 116 | for _, s := range body.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. { |
| 117 | switch s := s.(type) { |
| 118 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ExprStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#: |
| 119 | if call, ok := s.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#X..(*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#); ok { |
| 120 | for _, stmt := range parallelSubtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/parallelSubtest().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo., call) { |
| 121 | reportCapturedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/reportCaptured().(pass, vars, stmt) |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return true |
| 128 | }) |
| 129 | return nil, nil |
| 130 | } |
| 131 | |
| 132 | // reportCaptured reports a diagnostic stating a loop variable |
| 133 | // has been captured by a func literal if checkStmt has escaping |
| 134 | // references to vars. vars is expected to be variables updated by a loop statement, |
| 135 | // and checkStmt is expected to be a statements from the body of a func literal in the loop. |
| 136 | func reportCapturedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/reportCaptured().(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#, vars []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#, checkStmt astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#) { |
| 137 | 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().(checkStmt, 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 { |
| 138 | id, ok := n.(*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#) |
| 139 | if !ok { |
| 140 | return true |
| 141 | } |
| 142 | obj := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Usesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Uses.[id] |
| 143 | if obj == nil { |
| 144 | return true |
| 145 | } |
| 146 | for _, v := range vars { |
| 147 | if v == obj { |
| 148 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(id, "loop variable %s captured by func literal", id.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) |
| 149 | } |
| 150 | } |
| 151 | return true |
| 152 | }) |
| 153 | } |
| 154 | |
| 155 | // forEachLastStmt calls onLast on each "last" statement in a list of statements. |
| 156 | // "Last" is defined recursively so, for example, if the last statement is |
| 157 | // a switch statement, then each switch case is also visited to examine |
| 158 | // its last statements. |
| 159 | func forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(stmts []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#, onLast func(last astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#)) { |
| 160 | if len(stmts) == 0 { |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | s := stmts[len(stmts)-1] |
| 165 | switch s := s.(type) { |
| 166 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IfStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#: |
| 167 | loop: |
| 168 | for { |
| 169 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(s.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List., onLast) |
| 170 | switch e := s.Elsescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Else..(type) { |
| 171 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BlockStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#: |
| 172 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(e.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List., onLast) |
| 173 | break loop |
| 174 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IfStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#: |
| 175 | s = e |
| 176 | case nil: |
| 177 | break loop |
| 178 | } |
| 179 | } |
| 180 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ForStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#: |
| 181 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(s.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ForStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List., onLast) |
| 182 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.RangeStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#: |
| 183 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(s.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/RangeStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List., onLast) |
| 184 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#: |
| 185 | for _, c := range s.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SwitchStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. { |
| 186 | cc := c.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CaseClausescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#) |
| 187 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(cc.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#Body., onLast) |
| 188 | } |
| 189 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSwitchStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#: |
| 190 | for _, c := range s.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSwitchStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. { |
| 191 | cc := c.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CaseClausescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#) |
| 192 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(cc.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CaseClause#Body., onLast) |
| 193 | } |
| 194 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectStmt#: |
| 195 | for _, c := range s.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. { |
| 196 | cc := c.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CommClausescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#) |
| 197 | forEachLastStmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/forEachLastStmt().(cc.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommClause#Body., onLast) |
| 198 | } |
| 199 | default: |
| 200 | onLast(s) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // litStmts returns all statements from the function body of a function |
| 205 | // literal. |
| 206 | // |
| 207 | // If fun is not a function literal, it returns nil. |
| 208 | func litStmtsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/litStmts().(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#) []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt# { |
| 209 | lit, _ := 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#) |
| 210 | if lit == nil { |
| 211 | return nil |
| 212 | } |
| 213 | return lit.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. |
| 214 | } |
| 215 | |
| 216 | // goInvoke returns a function expression that would be called asynchronously |
| 217 | // (but not awaited) in another goroutine as a consequence of the call. |
| 218 | // For example, given the g.Go call below, it returns the function literal expression. |
| 219 | // |
| 220 | // import "sync/errgroup" |
| 221 | // var g errgroup.Group |
| 222 | // g.Go(func() error { ... }) |
| 223 | // |
| 224 | // Currently only "golang.org/x/sync/errgroup.Group()" is considered. |
| 225 | func goInvokescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/goInvoke().(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#) 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# { |
| 226 | if !isMethodCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/isMethodCall().(info, call, "golang.org/x/sync/errgroup", "Group", "Go") { |
| 227 | return nil |
| 228 | } |
| 229 | return call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0] |
| 230 | } |
| 231 | |
| 232 | // parallelSubtest returns statements that can be easily proven to execute |
| 233 | // concurrently via the go test runner, as t.Run has been invoked with a |
| 234 | // function literal that calls t.Parallel. |
| 235 | // |
| 236 | // In practice, users rely on the fact that statements before the call to |
| 237 | // t.Parallel are synchronous. For example by declaring test := test inside the |
| 238 | // function literal, but before the call to t.Parallel. |
| 239 | // |
| 240 | // Therefore, we only flag references in statements that are obviously |
| 241 | // dominated by a call to t.Parallel. As a simple heuristic, we only consider |
| 242 | // statements following the final labeled statement in the function body, to |
| 243 | // avoid scenarios where a jump would cause either the call to t.Parallel or |
| 244 | // the problematic reference to be skipped. |
| 245 | // |
| 246 | // import "testing" |
| 247 | // |
| 248 | // func TestFoo(t *testing.T) { |
| 249 | // tests := []int{0, 1, 2} |
| 250 | // for i, test := range tests { |
| 251 | // t.Run("subtest", func(t *testing.T) { |
| 252 | // println(i, test) // OK |
| 253 | // t.Parallel() |
| 254 | // println(i, test) // Not OK |
| 255 | // }) |
| 256 | // } |
| 257 | // } |
| 258 | func parallelSubtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/parallelSubtest().(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#) []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt# { |
| 259 | if !isMethodCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/isMethodCall().(info, call, "testing", "T", "Run") { |
| 260 | return nil |
| 261 | } |
| 262 | |
| 263 | if len(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.) != 2 { |
| 264 | // Ignore calls such as t.Run(fn()). |
| 265 | return nil |
| 266 | } |
| 267 | |
| 268 | lit, _ := call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[1].(*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#) |
| 269 | if lit == nil { |
| 270 | return nil |
| 271 | } |
| 272 | |
| 273 | // Capture the *testing.T object for the first argument to the function |
| 274 | // literal. |
| 275 | if len(lit.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 { |
| 276 | return nil |
| 277 | } |
| 278 | |
| 279 | tObj := info.Defsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Defs.[lit.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]] |
| 280 | if tObj == nil { |
| 281 | return nil |
| 282 | } |
| 283 | |
| 284 | // Match statements that occur after a call to t.Parallel following the final |
| 285 | // labeled statement in the function body. |
| 286 | // |
| 287 | // We iterate over lit.Body.List to have a simple, fast and "frequent enough" |
| 288 | // dominance relationship for t.Parallel(): lit.Body.List[i] dominates |
| 289 | // lit.Body.List[j] for i < j unless there is a jump. |
| 290 | var stmts []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt# |
| 291 | afterParallel := false |
| 292 | for _, stmt := range lit.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List. { |
| 293 | stmt, labeled := unlabelscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/unlabel().(stmt) |
| 294 | if labeled { |
| 295 | // Reset: naively we don't know if a jump could have caused the |
| 296 | // previously considered statements to be skipped. |
| 297 | stmts = nil |
| 298 | afterParallel = false |
| 299 | } |
| 300 | |
| 301 | if afterParallel { |
| 302 | stmts = append(stmts, stmt) |
| 303 | continue |
| 304 | } |
| 305 | |
| 306 | // Check if stmt is a call to t.Parallel(), for the correct t. |
| 307 | exprStmt, ok := stmt.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ExprStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#) |
| 308 | if !ok { |
| 309 | continue |
| 310 | } |
| 311 | call, ok := exprStmt.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ExprStmt#X..(*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#) |
| 312 | if !ok { |
| 313 | continue |
| 314 | } |
| 315 | if isMethodCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/isMethodCall().(info, call, "testing", "T", "Parallel") { |
| 316 | x, _ := 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`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#) |
| 317 | if x == nil { |
| 318 | continue |
| 319 | } |
| 320 | id, _ := x.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#) |
| 321 | if id == nil { |
| 322 | continue |
| 323 | } |
| 324 | if info.Usesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Uses.[id] == tObj { |
| 325 | afterParallel = true |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return stmts |
| 331 | } |
| 332 | |
| 333 | // unlabel returns the inner statement for the possibly labeled statement stmt, |
| 334 | // stripping any (possibly nested) *ast.LabeledStmt wrapper. |
| 335 | // |
| 336 | // The second result reports whether stmt was an *ast.LabeledStmt. |
| 337 | func unlabelscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/unlabel().(stmt astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#) (astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Stmt#, bool) { |
| 338 | labeled := false |
| 339 | for { |
| 340 | labelStmt, ok := stmt.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.LabeledStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/LabeledStmt#) |
| 341 | if !ok { |
| 342 | return stmt, labeled |
| 343 | } |
| 344 | labeled = true |
| 345 | stmt = labelStmt.Stmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/LabeledStmt#Stmt. |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | func isMethodCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/loopclosure`/isMethodCall().(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#, pkgPath, typeName, method string) bool { |
| 350 | return 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().(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), pkgPath, typeName, method) |
| 351 | } |