diff --git a/cmd/cover/cover.go b/cmd/cover/cover.go index 3aaf246f..ceaf3cfa 100644 --- a/cmd/cover/cover.go +++ b/cmd/cover/cover.go @@ -220,6 +220,11 @@ func (f *File) Visit(node ast.Node) ast.Visitor { if n.Body == nil || len(n.Body.List) == 0 { return nil } + case *ast.TypeSwitchStmt: + // Don't annotate an empty type switch - creates a syntax error. + if n.Body == nil || len(n.Body.List) == 0 { + return nil + } } return f } diff --git a/cmd/cover/testdata/test.go b/cmd/cover/testdata/test.go index 80b1f6b1..167a341c 100644 --- a/cmd/cover/testdata/test.go +++ b/cmd/cover/testdata/test.go @@ -22,6 +22,7 @@ func testAll() { testTypeSwitch() testSelect1() testSelect2() + testEmptySwitches() } func testSimple() { @@ -175,3 +176,20 @@ func testSelect2() { } } } + +// Empty control statements created syntax errors. This function +// is here just to be sure that those are handled correctly now. +func testEmptySwitches() { + check(LINE, 1) + switch 3 { + } + check(LINE, 1) + switch i := (interface{})(3).(int); i { + } + check(LINE, 1) + go func() { + check(LINE, 1) + select {} + }() + check(LINE, 1) +}