diff --git a/src/cmd/cover/cover.go b/src/cmd/cover/cover.go index 31ec4345469..c5d16826516 100644 --- a/src/cmd/cover/cover.go +++ b/src/cmd/cover/cover.go @@ -181,6 +181,10 @@ func (f *File) Visit(node ast.Node) ast.Visitor { } n.List = f.addCounters(n.Lbrace, n.Rbrace+1, n.List, true) // +1 to step past closing brace. case *ast.IfStmt: + if n.Init != nil { + ast.Walk(f, n.Init) + } + ast.Walk(f, n.Cond) ast.Walk(f, n.Body) if n.Else == nil { return nil @@ -219,11 +223,21 @@ func (f *File) Visit(node ast.Node) ast.Visitor { case *ast.SwitchStmt: // Don't annotate an empty switch - creates a syntax error. if n.Body == nil || len(n.Body.List) == 0 { + if n.Init != nil { + ast.Walk(f, n.Init) + } + if n.Tag != nil { + ast.Walk(f, n.Tag) + } 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 { + if n.Init != nil { + ast.Walk(f, n.Init) + } + ast.Walk(f, n.Assign) return nil } } diff --git a/src/cmd/cover/testdata/test.go b/src/cmd/cover/testdata/test.go index 9013950a2b3..c4c0e15b0be 100644 --- a/src/cmd/cover/testdata/test.go +++ b/src/cmd/cover/testdata/test.go @@ -24,6 +24,7 @@ func testAll() { testSelect2() testPanic() testEmptySwitches() + testFunctionLiteral() } // The indexes of the counters in testPanic are known to main.go @@ -216,3 +217,32 @@ func testEmptySwitches() { <-c check(LINE, 1) } + +func testFunctionLiteral() { + a := func(f func()) error { + f() + f() + return nil + } + + b := func(f func()) bool { + f() + f() + return true + } + + check(LINE, 1) + a(func() { + check(LINE, 2) + }) + + if err := a(func() { + check(LINE, 2) + }); err != nil { + } + + switch b(func() { + check(LINE, 2) + }) { + } +}