Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clothe returns #287

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Invert a couple conditionals to reduce indentation
flimzy committed Jan 29, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
aramase Anish Ramasekar
commit 324c000d5579374542007ae9b92bf74bf41ec68c
70 changes: 38 additions & 32 deletions format/format.go
Original file line number Diff line number Diff line change
@@ -705,42 +705,48 @@ 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 results.NumFields() == 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)
}
}
}