]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt: fine-tune stripping of parentheses
authorRobert Griesemer <gri@golang.org>
Tue, 27 Apr 2010 20:27:48 +0000 (13:27 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 27 Apr 2010 20:27:48 +0000 (13:27 -0700)
       (composite literals in control clauses only need
       parentheses if the literals start with a type name)

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

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

index dbbcf4e83fbdd6321f4f371577c72d1d874e25e8..dd2b497f5a43a3a7ce765a41d95c27e8b374f71b 100644 (file)
@@ -950,14 +950,25 @@ func (p *printer) block(s *ast.BlockStmt, indent int) {
 }
 
 
+func isTypeName(x ast.Expr) bool {
+       switch t := x.(type) {
+       case *ast.Ident:
+               return true
+       case *ast.SelectorExpr:
+               return isTypeName(t.X)
+       }
+       return false
+}
+
+
 // TODO(gri): Decide if this should be used more broadly. The printing code
 //            knows when to insert parentheses for precedence reasons, but
 //            need to be careful to keep them around type expressions.
 func stripParens(x ast.Expr, inControlClause bool) ast.Expr {
        for px, hasParens := x.(*ast.ParenExpr); hasParens; px, hasParens = x.(*ast.ParenExpr) {
                x = px.X
-               if _, isCompositeLit := x.(*ast.CompositeLit); isCompositeLit && inControlClause {
-                       // composite literals inside control clauses need parens;
+               if cx, isCompositeLit := x.(*ast.CompositeLit); inControlClause && isCompositeLit && isTypeName(cx.Type) {
+                       // composite literals inside control clauses need parens if they start with a type name;
                        // don't strip innermost layer
                        return px
                }
index 455a4d632c40c5b9a1a3156b211bde53b30ab192..e3076aefc3bfe7db4849a6a0310f6d1c509d6849 100644 (file)
@@ -144,7 +144,7 @@ func _() {
        for x := range []int{} {
                use(x)
        }
-       for x := range ([]int{}) {
+       for x := range []int{} {
                use(x)
        }       // no parens printed
 }
@@ -152,39 +152,63 @@ func _() {
 
 // Don't remove mandatory parentheses around composite literals in control clauses.
 func _() {
+       // strip no parentheses - no composite literals or composite literals don't start with a type name
        if x {
-       }       // no ()'s
+       }
        if x {
-       }       // no ()'s
-       if ([]T{}) {
-       }       // ()
-       if ([]T{}) {
-       }       // ()
-       if ([]T{}) {
-       }       // ()
+       }
+       if []T{} {
+       }
+       if []T{} {
+       }
+       if []T{} {
+       }
 
        for x {
-       }       // no ()'s
+       }
        for x {
-       }       // no ()'s
-       for ([]T{}) {
-       }       // ()
-       for ([]T{}) {
-       }       // ()
-       for ([]T{}) {
-       }       // ()
+       }
+       for []T{} {
+       }
+       for []T{} {
+       }
+       for []T{} {
+       }
 
        switch x {
-       }       // no ()'s
+       }
        switch x {
-       }       // no ()'s
-       switch ([]T{}) {
-       }       // ()
-       switch ([]T{}) {
-       }       // ()
-
-       for _ = range ([]T{T{42}}) {
-       }       // ()
+       }
+       switch []T{} {
+       }
+       switch []T{} {
+       }
+
+       for _ = range []T{T{42}} {
+       }
+
+       // leave parentheses - composite literals start with a type name
+       if (T{}) {
+       }
+       if (T{}) {
+       }
+       if (T{}) {
+       }
+
+       for (T{}) {
+       }
+       for (T{}) {
+       }
+       for (T{}) {
+       }
+
+       switch (T{}) {
+       }
+       switch (T{}) {
+       }
+
+       for _ = range (T1{T{42}}) {
+       }
 }
 
 
index 60133ea48589fa8f8e62572620cb06541dbefcb9..a92911a362b31f20c2904ccc70d60fee3630abb8 100644 (file)
@@ -113,24 +113,39 @@ func _() {
 
 // Don't remove mandatory parentheses around composite literals in control clauses.
 func _() {
-       if (x) {}                // no ()'s
-       if (((x))) {}            // no ()'s
-       if ([]T{}) {}            // ()
-       if (([]T{})) {}          // ()
-       if ; (((([]T{})))) {}    // ()
-
-       for (x) {}                // no ()'s
-       for (((x))) {}            // no ()'s
-       for ([]T{}) {}            // ()
-       for (([]T{})) {}          // ()
-       for ; (((([]T{})))) ; {}  // ()
-
-       switch (x) {}                // no ()'s
-       switch (((x))) {}            // no ()'s
-       switch ([]T{}) {}            // ()
-       switch (([]T{})) {}          // ()
-
-       for _ = range ((([]T{T{42}}))) {}  // ()
+       // strip parentheses - no composite literals or composite literals don't start with a type name
+       if (x) {}
+       if (((x))) {}
+       if ([]T{}) {}
+       if (([]T{})) {}
+       if ; (((([]T{})))) {}
+
+       for (x) {}
+       for (((x))) {}
+       for ([]T{}) {}
+       for (([]T{})) {}
+       for ; (((([]T{})))) ; {}
+
+       switch (x) {}
+       switch (((x))) {}
+       switch ([]T{}) {}
+       switch ; (((([]T{})))) {}
+
+       for _ = range ((([]T{T{42}}))) {}
+
+       // leave parentheses - composite literals start with a type name
+       if (T{}) {}
+       if ((T{})) {}
+       if ; ((((T{})))) {}
+
+       for (T{}) {}
+       for ((T{})) {}
+       for ; ((((T{})))) ; {}
+
+       switch (T{}) {}
+       switch ; ((((T{})))) {}
+
+       for _ = range (((T1{T{42}}))) {}
 }