1
0
mirror of https://github.com/golang/go synced 2024-11-24 04:20:03 -07:00

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
This commit is contained in:
Simon Whitehead 2014-07-01 09:32:03 -07:00 committed by Robert Griesemer
parent 5005c29ae0
commit 138099ae96
4 changed files with 54 additions and 2 deletions

View File

@ -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) {

View File

@ -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
}

10
src/cmd/gofmt/testdata/emptydecl.golden vendored Normal file
View File

@ -0,0 +1,10 @@
package main
// Keep this declaration
var ()
const (
// Keep this declaration
)
func main() {}

12
src/cmd/gofmt/testdata/emptydecl.input vendored Normal file
View File

@ -0,0 +1,12 @@
package main
// Keep this declaration
var ()
const (
// Keep this declaration
)
type ()
func main() {}