cmd/cover: treats empty lambdas as being executed even when not executed #46825
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
This problem came to my attention because I was surprised the coverage reported by
go test -coverprofile=cover
was significantly higher than that reported by the Codecov service. After carefully looking at a couple of files in the project which should have had identical coverage I came to the conclusion there appears to be an off by one bug, or something akin to it, in the go coverage calculations.Create a directory with the following two files, run
go mod example.com/coverage
thengo test -coverprofile=cover ./...
.File x.go:
File x_test.go:
What did you expect to see?
Correct coverage of 66.7%.
What did you see instead?
Incorrect coverage of 80% due to treating the lambda in the
BadCoverage
function as if it had been executed. Changing the body of the lambda to do anything at all "fixes" the coverage. Interestingly, the HTML produced bygo tool cover -html=cover
highlights the right-paren in red in theBadCoverage
case. Which suggests a disconnect between the code highlighting and coverage calculation. Also, the incorrect value of 80% is also wrong, it seems to me, since it is treating the code as if 5/6 of the lines were executed which is 83.3%. It looks like it is treating the incorrect case as if 4/5 of the lines were executed. Which is another clue how the coverage data is being misinterpreted.For the record, the cover data is (compare lines 3 and 5):
The text was updated successfully, but these errors were encountered: