diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
commit | 505c19580e0f43fe5224431459cacb7c21edd93d (patch) | |
tree | 79e2634c253d60afc0cc0b2f510dc7dcbb48497b /src/cmd/gofmt/simplify.go | |
parent | 1336a7c91e596c423a49d1194ea42d98bca0d958 (diff) | |
download | golang-505c19580e0f43fe5224431459cacb7c21edd93d.tar.gz |
Imported Upstream version 1upstream/1
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) |