// lead to more nicely formatted code in general.
if p.indentList(s.Results) {
p.print(indent)
- p.exprList(s.Pos(), s.Results, 1, noIndent, token.NoPos, false)
+ // Use NoPos so that a newline never goes before
+ // the results (see issue #32854).
+ p.exprList(token.NoPos, s.Results, 1, noIndent, token.NoPos, false)
p.print(unindent)
} else {
- p.exprList(s.Pos(), s.Results, 1, 0, token.NoPos, false)
+ p.exprList(token.NoPos, s.Results, 1, 0, token.NoPos, false)
}
}
t.Errorf("got %q, want %q", noparen, original)
}
}
+
+// Verify that we don't print a newline between "return" and its results, as
+// that would incorrectly cause a naked return.
+func TestIssue32854(t *testing.T) {
+ src := `package foo
+
+func f() {
+ return Composite{
+ call(),
+ }
+}`
+ fset := token.NewFileSet()
+ file, err := parser.ParseFile(fset, "", src, 0)
+ if err != nil {
+ panic(err)
+ }
+
+ // Replace the result with call(), which is on the next line.
+ fd := file.Decls[0].(*ast.FuncDecl)
+ ret := fd.Body.List[0].(*ast.ReturnStmt)
+ ret.Results[0] = ret.Results[0].(*ast.CompositeLit).Elts[0]
+
+ var buf bytes.Buffer
+ if err := Fprint(&buf, fset, ret); err != nil {
+ t.Fatal(err)
+ }
+ want := "return call()"
+ if got := buf.String(); got != want {
+ t.Fatalf("got %q, want %q", got, want)
+ }
+}