Skip to content

testing: inconsistent coverage on anonymous functions in slice #73939

Open
@rliebz

Description

@rliebz

Go version

go version go1.24.3 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/rob/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/rob/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/ry/6bttgkt51f14fhb1cwsg_6r00000gp/T/go-build1866195821=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/rob/Sandbox/covertest/go.mod'
GOMODCACHE='/Users/rob/.local/share/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/rob/.local/share/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/rob/.local/share/mise/installs/go/1.24.3'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/rob/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/rob/.local/share/mise/installs/go/1.24.3/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

cover.go:

package covertest

func run() error {
	runFuncs := []func() error{
		func() error { return nil },
		func() error { return nil },
		func() error { return nil },
	}

	for _, f := range runFuncs {
		if err := f(); err != nil {
			return err
		}
	}

	return nil
}

cover_test.go:

package covertest

import "testing"

func Test_run(t *testing.T) {
	if err := run(); err != nil {
		t.Fatal(err)
	}
}
go test -coverprofile=profile.cov
go tool cover -html=profile.cov

What did you see happen?

Coverage reporting for the first anonymous function declaration is different from the next two, despite them having the same coverage.

It appears to include the function signature only if it is the first element of the slice.

profile.cov:

mode: set
example.com/covertest/cover.go:3.18,5.16 1 1
example.com/covertest/cover.go:5.16,5.30 1 1
example.com/covertest/cover.go:6.16,6.30 1 1
example.com/covertest/cover.go:7.16,7.30 1 1
example.com/covertest/cover.go:10.2,10.29 1 1
example.com/covertest/cover.go:10.29,11.29 1 1
example.com/covertest/cover.go:11.29,13.4 1 0
example.com/covertest/cover.go:16.2,16.12 1 1
Image

What did you expect to see?

I would expect that the function signature of the anonymous functions, which in this case would be:

func() error

to either be reported as "green" across all 3 slice elements, or to not be reported across any slice elements. Either behavior seems justifiable, but I would expect it to be consistent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions