Skip to content

Commit 44200bf

Browse files
committed
go/analysis/passes/loopclosure: shorten some if checks and use a for loop for if statements
1 parent 20cbf14 commit 44200bf

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

go/analysis/passes/loopclosure/loopclosure.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,14 @@ func lastStmts(pass *analysis.Pass, stmts []ast.Stmt) []ast.Stmt {
169169
s := stmts[len(stmts)-1]
170170
switch s := s.(type) {
171171
case *ast.IfStmt:
172-
res = append(res, lastStmts(pass, s.Body.List)...)
173-
if s.Else != nil {
174-
switch s := s.Else.(type) {
172+
var next *ast.IfStmt
173+
for ; s != nil; s, next = next, nil {
174+
res = append(res, lastStmts(pass, s.Body.List)...)
175+
switch e := s.Else.(type) {
175176
case *ast.BlockStmt:
176-
res = append(res, lastStmts(pass, s.List)...)
177+
res = append(res, lastStmts(pass, e.List)...)
177178
case *ast.IfStmt:
178-
res = append(res, lastStmts(pass, []ast.Stmt{s})...)
179+
next = e
179180
}
180181
}
181182
case *ast.ForStmt:
@@ -184,27 +185,21 @@ func lastStmts(pass *analysis.Pass, stmts []ast.Stmt) []ast.Stmt {
184185
res = append(res, lastStmts(pass, s.Body.List)...)
185186
case *ast.SwitchStmt:
186187
for _, c := range s.Body.List {
187-
c, ok := c.(*ast.CaseClause)
188-
if !ok {
189-
continue
188+
if c, ok := c.(*ast.CaseClause); ok {
189+
res = append(res, lastStmts(pass, c.Body)...)
190190
}
191-
res = append(res, lastStmts(pass, c.Body)...)
192191
}
193192
case *ast.TypeSwitchStmt:
194193
for _, c := range s.Body.List {
195-
c, ok := c.(*ast.CaseClause)
196-
if !ok {
197-
continue
194+
if c, ok := c.(*ast.CaseClause); ok {
195+
res = append(res, lastStmts(pass, c.Body)...)
198196
}
199-
res = append(res, lastStmts(pass, c.Body)...)
200197
}
201198
case *ast.SelectStmt:
202199
for _, c := range s.Body.List {
203-
c, ok := c.(*ast.CommClause)
204-
if !ok {
205-
continue
200+
if c, ok := c.(*ast.CommClause); ok {
201+
res = append(res, lastStmts(pass, c.Body)...)
206202
}
207-
res = append(res, lastStmts(pass, c.Body)...)
208203
}
209204
case *ast.GoStmt:
210205
res = append(res, litStmts(s.Call.Fun)...)

0 commit comments

Comments
 (0)