diff options
Diffstat (limited to 'src/cmd/gofmt/simplify.go')
-rw-r--r-- | src/cmd/gofmt/simplify.go | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/cmd/gofmt/simplify.go b/src/cmd/gofmt/simplify.go index d9afc0e7b..470c00625 100644 --- a/src/cmd/gofmt/simplify.go +++ b/src/cmd/gofmt/simplify.go @@ -6,6 +6,7 @@ package main import ( "go/ast" + "go/token" "reflect" ) @@ -26,10 +27,12 @@ func (s *simplifier) Visit(node ast.Node) ast.Visitor { if eltType != nil { typ := reflect.ValueOf(eltType) - for _, x := range outer.Elts { + for i, x := range outer.Elts { + px := &outer.Elts[i] // look at value of indexed/named elements if t, ok := x.(*ast.KeyValueExpr); ok { x = t.Value + px = &t.Value } simplify(x) // if the element is a composite literal and its literal type @@ -40,6 +43,19 @@ func (s *simplifier) Visit(node ast.Node) ast.Visitor { inner.Type = nil } } + // if the outer literal's element type is a pointer type *T + // and the element is & of a composite literal of type T, + // the inner &T may be omitted. + if ptr, ok := eltType.(*ast.StarExpr); ok { + if addr, ok := x.(*ast.UnaryExpr); ok && addr.Op == token.AND { + if inner, ok := addr.X.(*ast.CompositeLit); ok { + if match(nil, reflect.ValueOf(ptr.X), reflect.ValueOf(inner.Type)) { + inner.Type = nil // drop T + *px = inner // drop & + } + } + } + } } // node was simplified - stop walk (there are no subnodes to simplify) |