Skip to content

Commit

Permalink
Invert a couple conditionals to reduce indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Jan 29, 2024
1 parent def7745 commit 71eb4a2
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,42 +705,44 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {

// Clothe naked returns
case *ast.ReturnStmt:
if node.Results == nil { // We have either a naked return, or a function with no return values
parents, _ := astutil.PathEnclosingInterval(f.astFile, node.Pos(), node.End())
var results *ast.FieldList
// Find the nearest ancestor that is either a func declaration or func literal
parentLoop:
for _, parent := range parents {
switch p := parent.(type) {
case *ast.FuncDecl:
results = p.Type.Results
break parentLoop
case *ast.FuncLit:
results = p.Type.Results
break parentLoop
}
if node.Results != nil {
break
} // We have either a naked return, or a function with no return values
parents, _ := astutil.PathEnclosingInterval(f.astFile, node.Pos(), node.End())
var results *ast.FieldList
// Find the nearest ancestor that is either a func declaration or func literal
parentLoop:
for _, parent := range parents {
switch p := parent.(type) {
case *ast.FuncDecl:
results = p.Type.Results
break parentLoop
case *ast.FuncLit:
results = p.Type.Results
break parentLoop
}
if fields := results.NumFields(); fields > 0 { // The function has return values; let's clothe the return
node.Results = make([]ast.Expr, 0, fields)
nameLoop:
for _, result := range results.List {
for _, ident := range result.Names {
name := ident.Name
if name == "_" { // we can't handle blank names just yet, abort the transform
node.Results = nil
break nameLoop
}
node.Results = append(node.Results, &ast.Ident{
NamePos: node.Pos(), // Use the Pos of the return statement, to not interfere with comment placement
Name: name,
})
}
}
if len(node.Results) > 0 {
c.Replace(node)
}
if fields := results.NumFields(); fields <= 0 {
break
} // The function has return values; let's clothe the return
node.Results = make([]ast.Expr, 0, results.NumFields())
nameLoop:
for _, result := range results.List {
for _, ident := range result.Names {
name := ident.Name
if name == "_" { // we can't handle blank names just yet, abort the transform
node.Results = nil
break nameLoop
}
node.Results = append(node.Results, &ast.Ident{
NamePos: node.Pos(), // Use the Pos of the return statement, to not interfere with comment placement
Name: name,
})
}
}
if len(node.Results) > 0 {
c.Replace(node)
}
}
}

Expand Down

0 comments on commit 71eb4a2

Please sign in to comment.