Skip to content

Commit c3b9042

Browse files
zikaerohBryan C. Mills
authored and
Bryan C. Mills
committed
cmd/cover: skip function declarations with blank names
Function declarations with blank ("_") names do not introduce a binding, and therefore cannot be referenced or executed (in fact, they do not make it into the final compiled binary at all). As such, counters defined while annotating their bodies will always be zero. These types of functions are commonly used to create compile-time checks (e.g., stringer) which are not expected to be executed. Skip over these functions when annotating a file, preventing the unused counters from being generated and appearing as uncovered lines in coverage reports. Fixes #36264 Change-Id: I6b516cf43c430a6248d68d5f483a3902253fbdab Reviewed-on: https://go-review.googlesource.com/c/go/+/223117 Reviewed-by: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent c6bcdea commit c3b9042

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/cmd/cover/cover.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ func (f *File) Visit(node ast.Node) ast.Visitor {
293293
ast.Walk(f, n.Assign)
294294
return nil
295295
}
296+
case *ast.FuncDecl:
297+
// Don't annotate functions with blank names - they cannot be executed.
298+
if n.Name.Name == "_" {
299+
return nil
300+
}
296301
}
297302
return f
298303
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[short] skip
2+
go test -cover ./coverblank
3+
stdout 'coverage: 100.0% of statements'
4+
5+
6+
-- coverblank/a.go --
7+
package coverblank
8+
9+
func _() {
10+
println("unreachable")
11+
}
12+
13+
type X int
14+
15+
func (x X) Print() {
16+
println(x)
17+
}
18+
19+
func (x X) _() {
20+
println("unreachable")
21+
}
22+
23+
-- coverblank/a_test.go --
24+
package coverblank
25+
26+
import "testing"
27+
28+
func TestX(t *testing.T) {
29+
var x X
30+
x.Print()
31+
}

0 commit comments

Comments
 (0)