diff options
Diffstat (limited to 'src/cmd/gofmt/simplify.go')
| -rw-r--r-- | src/cmd/gofmt/simplify.go | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/src/cmd/gofmt/simplify.go b/src/cmd/gofmt/simplify.go index 69f7bf23c0..2ebf4cde0b 100644 --- a/src/cmd/gofmt/simplify.go +++ b/src/cmd/gofmt/simplify.go @@ -10,11 +10,9 @@ import ( "reflect" ) -type simplifier struct { - hasDotImport bool // package file contains: import . "some/import/path" -} +type simplifier struct{} -func (s *simplifier) Visit(node ast.Node) ast.Visitor { +func (s simplifier) Visit(node ast.Node) ast.Visitor { switch n := node.(type) { case *ast.CompositeLit: // array, slice, and map composite literals may be simplified @@ -68,10 +66,13 @@ func (s *simplifier) Visit(node ast.Node) ast.Visitor { // a slice expression of the form: s[a:len(s)] // can be simplified to: s[a:] // if s is "simple enough" (for now we only accept identifiers) - if n.Max != nil || s.hasDotImport { + // + // Note: This may not be correct because len may have been redeclared in another + // file belonging to the same package. However, this is extremely unlikely + // and so far (April 2016, after years of supporting this rewrite feature) + // has never come up, so let's keep it working as is (see also #15153). + if n.Max != nil { // - 3-index slices always require the 2nd and 3rd index - // - if dot imports are present, we cannot be certain that an - // unresolved "len" identifier refers to the predefined len() break } if s, _ := n.X.(*ast.Ident); s != nil && s.Obj != nil { @@ -118,20 +119,11 @@ func isBlank(x ast.Expr) bool { } func simplify(f *ast.File) { - var s simplifier - - // determine if f contains dot imports - for _, imp := range f.Imports { - if imp.Name != nil && imp.Name.Name == "." { - s.hasDotImport = true - break - } - } - // remove empty declarations such as "const ()", etc removeEmptyDeclGroups(f) - ast.Walk(&s, f) + var s simplifier + ast.Walk(s, f) } func removeEmptyDeclGroups(f *ast.File) { |
