Commit 138099ae authored by Simon Whitehead's avatar Simon Whitehead Committed by Robert Griesemer

gofmt/main: Added removal of empty declaration groups.

Fixes #7631.

LGTM=gri
R=golang-codereviews, bradfitz, gri
CC=golang-codereviews
https://golang.org/cl/101410046
parent 5005c29a
......@@ -89,6 +89,7 @@ var tests = []struct {
{"testdata/import.input", ""},
{"testdata/crlf.input", ""}, // test case for issue 3961; see also TestCRLF
{"testdata/typeswitch.input", ""}, // test case for issue 4470
{"testdata/emptydecl.input", "-s"}, // test case for issue 7631
}
func TestRewrite(t *testing.T) {
......
......@@ -117,5 +117,34 @@ func simplify(f *ast.File) {
}
}
// 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
}
package main
// Keep this declaration
var ()
const (
// Keep this declaration
)
func main() {}
package main
// Keep this declaration
var ()
const (
// Keep this declaration
)
type ()
func main() {}
\ No newline at end of file
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