Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(698)

Unified Diff: src/cmd/gofmt/simplify.go

Issue 101410046: code review 101410046: gofmt/main: Added removal of empty declaration groups
Patch Set: diff -r 1fcd603852b0 https://code.google.com/p/go Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/cmd/gofmt/gofmt_test.go ('k') | src/cmd/gofmt/testdata/emptydecl.golden » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/cmd/gofmt/simplify.go
===================================================================
--- a/src/cmd/gofmt/simplify.go
+++ b/src/cmd/gofmt/simplify.go
@@ -117,5 +117,34 @@
}
}
+ // remove empty declarations such as "const ()", etc
+ removeEmptyDeclGroups(f)
+
ast.Walk(&s, f)
}
+
+func removeEmptyDeclGroups(f *ast.File) {
+ i := 0
+ for _, d := range f.Decls {
+ if g, ok := d.(*ast.GenDecl); !ok || !isEmpty(f, g) {
+ f.Decls[i] = d
+ i++
+ }
+ }
+ f.Decls = f.Decls[:i]
+}
+
+func isEmpty(f *ast.File, g *ast.GenDecl) bool {
+ if g.Doc != nil || g.Specs != nil {
+ return false
+ }
+
+ for _, c := range f.Comments {
+ // if there is a comment in the declaration, it is not considered empty
+ if g.Pos() <= c.Pos() && c.End() <= g.End() {
+ return false
+ }
+ }
+
+ return true
+}
« no previous file with comments | « src/cmd/gofmt/gofmt_test.go ('k') | src/cmd/gofmt/testdata/emptydecl.golden » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b