]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: don't print newlines for empty statements
authorRobert Griesemer <gri@golang.org>
Tue, 15 May 2012 19:21:21 +0000 (12:21 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 15 May 2012 19:21:21 +0000 (12:21 -0700)
Fixes #3466.

gofmt -w src misc causes no changes.

R=rsc
CC=golang-dev
https://golang.org/cl/6206073

src/pkg/go/printer/nodes.go
src/pkg/go/printer/testdata/statements.golden
src/pkg/go/printer/testdata/statements.input

index 727d2a37147286845bb7e37748ceb4fd3a627e49..71dfccdcd508d4b428c9ee7a3b24b58683ab4a36 100644 (file)
@@ -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)
 }
index 4d70617bf13ec191658ecce11dcfb266cbb95711..1f3cabe75ecf26221f3d1c579ea4af51524abbcb 100644 (file)
@@ -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()
+}
index bd03bc98b7791fa4b8ab6e4cadba687d20764a31..f93eea8925347f923bb1b6a12f9d1bce665b7c39 100644 (file)
@@ -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()
+}