Skip to content

Commit

Permalink
go/printer, gofmt: don't print unneeded parentheses around parameter …
Browse files Browse the repository at this point in the history
…types

Fixes #4624.

R=rsc
CC=golang-dev
https://golang.org/cl/7058052
  • Loading branch information
griesemer committed Jan 9, 2013
1 parent 82accf4 commit 0141c92
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/pkg/go/printer/nodes.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/go/printer/printer.go
Expand Up @@ -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()
}

Expand Down
25 changes: 25 additions & 0 deletions src/pkg/go/printer/testdata/declarations.golden
Expand Up @@ -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))
25 changes: 25 additions & 0 deletions src/pkg/go/printer/testdata/declarations.input
Expand Up @@ -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))))))

0 comments on commit 0141c92

Please sign in to comment.