]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer, gofmt: don't print unneeded parentheses around parameter types
authorRobert Griesemer <gri@golang.org>
Wed, 9 Jan 2013 19:32:16 +0000 (11:32 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 9 Jan 2013 19:32:16 +0000 (11:32 -0800)
Fixes #4624.

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

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

index 3bed0cc6572c6beb30d5a561ce06f19686a80b3b..ee0bbf1eda9aba40d6c12cf30afda3ca02638d1e 100644 (file)
@@ -307,7 +307,7 @@ func (p *printer) parameters(fields *ast.FieldList) {
                                p.print(blank)
                        }
                        // parameter type
-                       p.expr(par.Type)
+                       p.expr(stripParensAlways(par.Type))
                        prevLine = parLineEnd
                }
                // if the closing ")" is on a separate line from the last parameter,
@@ -336,7 +336,7 @@ func (p *printer) signature(params, result *ast.FieldList) {
                p.print(blank)
                if n == 1 && result.List[0].Names == nil {
                        // single anonymous result; no ()'s
-                       p.expr(result.List[0].Type)
+                       p.expr(stripParensAlways(result.List[0].Type))
                        return
                }
                p.parameters(result)
@@ -959,6 +959,13 @@ func stripParens(x ast.Expr) ast.Expr {
        return x
 }
 
+func stripParensAlways(x ast.Expr) ast.Expr {
+       if x, ok := x.(*ast.ParenExpr); ok {
+               return stripParensAlways(x.X)
+       }
+       return x
+}
+
 func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, post ast.Stmt) {
        p.print(blank)
        needsBlank := false
index 5d75f09167d6d29d0902861de6e1727107710ae8..f1c07bd3be2d6c993bc624ab017419c365f8be97 100644 (file)
@@ -1230,7 +1230,7 @@ func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{
        }
 
        // flush tabwriter, if any
-       if tw, _ := (output).(*tabwriter.Writer); tw != nil {
+       if tw, _ := output.(*tabwriter.Writer); tw != nil {
                err = tw.Flush()
        }
 
index 21bbf2b2d42d0a8f4b9bc737614fa4cefd13610c..0ad72d349ee440bcb1aa2753929054328350c2fa 100644 (file)
@@ -887,3 +887,28 @@ type _ interface {
                r string,
                x ...int)
 }
+
+// omit superfluous parentheses in parameter lists
+func _(int)
+func _(int)
+func _(x int)
+func _(x int)
+func _(x, y int)
+func _(x, y int)
+
+func _() int
+func _() int
+func _() int
+
+func _() (x int)
+func _() (x int)
+func _() (x int)
+
+// special cases: some channel types require parentheses
+func _(x chan (<-chan int))
+func _(x chan (<-chan int))
+func _(x chan (<-chan int))
+
+func _(x chan<- (chan int))
+func _(x chan<- (chan int))
+func _(x chan<- (chan int))
index 6ac003699261baef1e7867008ef0a7433a1fa6c1..455c0c6c19f1e84f5d5ac8db580537fd5276a6e0 100644 (file)
@@ -896,3 +896,28 @@ p, q,
 r string,
                x ...int)
 }
+
+// omit superfluous parentheses in parameter lists
+func _((int))
+func _((((((int))))))
+func _(x (int))
+func _(x (((((int))))))
+func _(x, y (int))
+func _(x, y (((((int))))))
+
+func _() (int)
+func _() ((int))
+func _() ((((((int))))))
+
+func _() (x int)
+func _() (x (int))
+func _() (x (((((int))))))
+
+// special cases: some channel types require parentheses
+func _(x chan(<-chan int))
+func _(x (chan(<-chan int)))
+func _(x ((((chan(<-chan int))))))
+
+func _(x chan<-(chan int))
+func _(x (chan<-(chan int)))
+func _(x ((((chan<-(chan int))))))