From 26365e4e90c98f4b41230c50615e3a856598c66a Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Mon, 16 Mar 2015 14:52:01 -0700 Subject: [PATCH] cmd/cover: fix handling of empty type switch Just missed a case (ha!) in the tree walk. Dup the code for an empty switch, add test. Fixes #10163. Change-Id: I3d50ab6cb450ca21e87213291eaab8cbe924fac5 Reviewed-on: https://go-review.googlesource.com/7641 Reviewed-by: Andrew Gerrand --- cmd/cover/cover.go | 5 +++++ cmd/cover/testdata/test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) 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) +}