From 0e4fc907c8122a61ea961c10eae4115641de5d82 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 5 Oct 2023 13:35:16 -0400 Subject: [PATCH] internal/refactor/inline: add missing spread context (return) A call f() where f() has tuple type may appear as the sole operand of a return statement. I forgot this case in the "reduce spread-context call" strategy. Plus a test. Fixes golang/go#63398 Change-Id: Ie851c977c3a2d237feabc95dbed4c50e6a1c96ad Reviewed-on: https://go-review.googlesource.com/c/tools/+/533176 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley Auto-Submit: Alan Donovan --- internal/refactor/inline/inline.go | 13 ++++++++++--- internal/refactor/inline/inline_test.go | 6 ++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 9615ab43c..02db0f5eb 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -844,23 +844,30 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu // x, y = f() // or the sole argument to a spread call: // printf(f()) + // or spread return statement: + // return f() res.old = context switch context := context.(type) { case *ast.AssignStmt: - // Inv: the call is in Rhs[0], not Lhs. + // Inv: the call must be in Rhs[0], not Lhs. assign := shallowCopy(context) assign.Rhs = results res.new = assign case *ast.ValueSpec: - // Inv: the call is in Values[0], not Names. + // Inv: the call must be in Values[0], not Names. spec := shallowCopy(context) spec.Values = results res.new = spec case *ast.CallExpr: - // Inv: the Call is Args[0], not Fun. + // Inv: the call must be in Args[0], not Fun. call := shallowCopy(context) call.Args = results res.new = call + case *ast.ReturnStmt: + // Inv: the call must be Results[0]. + ret := shallowCopy(context) + ret.Results = results + res.new = ret default: return nil, fmt.Errorf("internal error: unexpected context %T for spread call", context) } diff --git a/internal/refactor/inline/inline_test.go b/internal/refactor/inline/inline_test.go index 8362e445b..2a20a55d3 100644 --- a/internal/refactor/inline/inline_test.go +++ b/internal/refactor/inline/inline_test.go @@ -617,6 +617,12 @@ func TestSpreadCalls(t *testing.T) { ) }`, }, + { + "Spread call in return (#63398).", + `func f() (int, error) { return 0, nil }`, + `func _() (int, error) { return f() }`, + `func _() (int, error) { return 0, nil }`, + }, }) }