From: Robert Griesemer Date: Tue, 15 May 2012 19:21:21 +0000 (-0700) Subject: go/printer: don't print newlines for empty statements X-Git-Tag: go1.1rc2~3199 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=03e1d4bc2262de4d50e0fe9ebcf743f1f1aef479;p=gostls13.git go/printer: don't print newlines for empty statements Fixes #3466. gofmt -w src misc causes no changes. R=rsc CC=golang-dev https://golang.org/cl/6206073 --- diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index 727d2a3714..71dfccdcd5 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -868,28 +868,32 @@ func (p *printer) expr(x ast.Expr) { // Print the statement list indented, but without a newline after the last statement. // Extra line breaks between statements in the source are respected but at most one // empty line is printed between statements. -func (p *printer) stmtList(list []ast.Stmt, _indent int, nextIsRBrace bool) { - // TODO(gri): fix _indent code - if _indent > 0 { +func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) { + if nindent > 0 { p.print(indent) } multiLine := false - for i, s := range list { - // _indent == 0 only for lists of switch/select case clauses; - // in those cases each clause is a new section - p.linebreak(p.lineFor(s.Pos()), 1, ignore, i == 0 || _indent == 0 || multiLine) - p.stmt(s, nextIsRBrace && i == len(list)-1) - multiLine = p.isMultiLine(s) - } - if _indent > 0 { + i := 0 + for _, s := range list { + // ignore empty statements (was issue 3466) + if _, isEmpty := s.(*ast.EmptyStmt); !isEmpty { + // _indent == 0 only for lists of switch/select case clauses; + // in those cases each clause is a new section + p.linebreak(p.lineFor(s.Pos()), 1, ignore, i == 0 || nindent == 0 || multiLine) + p.stmt(s, nextIsRBrace && i == len(list)-1) + multiLine = p.isMultiLine(s) + i++ + } + } + if nindent > 0 { p.print(unindent) } } // block prints an *ast.BlockStmt; it always spans at least two lines. -func (p *printer) block(s *ast.BlockStmt, indent int) { +func (p *printer) block(s *ast.BlockStmt, nindent int) { p.print(s.Pos(), token.LBRACE) - p.stmtList(s.List, indent, true) + p.stmtList(s.List, nindent, true) p.linebreak(p.lineFor(s.Rbrace), 1, ignore, true) p.print(s.Rbrace, token.RBRACE) } diff --git a/src/pkg/go/printer/testdata/statements.golden b/src/pkg/go/printer/testdata/statements.golden index 4d70617bf1..1f3cabe75e 100644 --- a/src/pkg/go/printer/testdata/statements.golden +++ b/src/pkg/go/printer/testdata/statements.golden @@ -527,3 +527,29 @@ AVeryLongLabelThatShouldNotAffectFormatting: // There should be a single empty line before this comment. MoreCode() } + +// Formatting of empty statements. +func _() { + +} + +func _() { +} + +func _() { +} + +func _() { + f() +} + +func _() { +L: + ; +} + +func _() { +L: + ; + f() +} diff --git a/src/pkg/go/printer/testdata/statements.input b/src/pkg/go/printer/testdata/statements.input index bd03bc98b7..f93eea8925 100644 --- a/src/pkg/go/printer/testdata/statements.input +++ b/src/pkg/go/printer/testdata/statements.input @@ -468,3 +468,27 @@ AVeryLongLabelThatShouldNotAffectFormatting: // There should be a single empty line before this comment. MoreCode() } + + +// Formatting of empty statements. +func _() { + ;;;;;;;;;;;;;;;;;;;;;;;;; +} + +func _() {;;;;;;;;;;;;;;;;;;;;;;;;; +} + +func _() {;;;;;;;;;;;;;;;;;;;;;;;;;} + +func _() { +f();;;;;;;;;;;;;;;;;;;;;;;;; +} + +func _() { +L:;;;;;;;;;;;; +} + +func _() { +L:;;;;;;;;;;;; + f() +}