Commit 0141c92a authored by Robert Griesemer's avatar Robert Griesemer

go/printer, gofmt: don't print unneeded parentheses around parameter types

Fixes #4624.

R=rsc
CC=golang-dev
https://golang.org/cl/7058052
parent 82accf44
......@@ -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
......
......@@ -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()
}
......
......@@ -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))
......@@ -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))))))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment