-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
What version of Go are you using (go version)?
$ go version go version go1.20.6 linux/amd64
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env)?
go env Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/shane/.cache/go-build" GOENV="/home/shane/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/shane/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/shane/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.20.6" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-O2 -g" CGO_CPPFLAGS="" CGO_CXXFLAGS="-O2 -g" CGO_FFLAGS="-O2 -g" CGO_LDFLAGS="-O2 -g" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2538044363=/tmp/go-build -gno-record-gcc-switches"
What did you do?
I created the following three files:
src.go:
package main
// Comment outside a function
func foo() int {
// Comment inside a function
if false {
// Comment inside branch that is not executed
return 0
} else {
// Comment inside branch that is executed
/*
General comment rather than a line comment
See https://go.dev/ref/spec#Comments for the difference
*/
return 1
}
}src_test.go:
package main
import "testing"
func TestFoo(t *testing.T) {
foo()
}go.mod:
module main
go 1.20
Next, I run the test, and generate and view an HTML coverage report:
$ go test -cover -coverprofile cp.out *.go
ok command-line-arguments 0.003s coverage: 66.7% of statements
$ go tool cover -html=cp.out
In my browser (Mozilla Firefox), this is the report I see:

Note that the comment inside the if block is considered not covered, and the comments inside the else block are considered covered. Also note the comment outside the foo function is considered not tracked.
The generated cp.out file has these contents:
mode: set
/home/shane/src/tmp/src.go:5.16,8.11 1 1
/home/shane/src/tmp/src.go:8.11,11.3 1 0
/home/shane/src/tmp/src.go:11.8,18.3 1 1
What did you expect to see?
I expect all lines that consist of only comments, or of only comments and whitespace to be not tracked.
I expect lines that only consist of comments or only consist of comments and whitespace (i.e., lines with no code) to not be tracked, as, according to the Go language specification, comments are documentation:
Comments serve as program documentation
I expect lines like
x = 0 // Sets x to 0to of course count towards coverage, since it has code, but lines without code should not count towards coverage.
It does not make sense to be concerned about coverage of lines that consist of only comments, in the same way that it does not make sense to be concerned about coverage of lines that consist of only whitespace. Therefore, go test -coverprofile should consider comment-only lines as untracked.
What did you see instead?
I see comments within function bodies are covered or not covered, i.e., comments within function bodies are always tracked.