Skip to content

cmd/cover: wrong coverage reported when func fields in struct #74350

Open
@gaston-haro

Description

@gaston-haro

Go version

go1.24.4 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/gaston.haro/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/gaston.haro/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/nl/_76lxc_523q8j859grpc9kv40000gr/T/go-build50665777=/tmp/
go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/gaston.haro/Development/go/src/playground/go.mod'
GOMODCACHE='/Users/gaston.haro/Development/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH=''
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.24.4/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/gaston.haro/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.24.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

main.go

package playground

type MyStruct struct {
	fieldA string
	fieldB func() int
	fieldC string
}

func MyFunc() MyStruct {
	return MyStruct{
		fieldA: "i am field a",
		fieldB: func() int {
			return 0
		},
		fieldC: "i am field c",
	}
}

func MyFunc2() MyStruct {
	return MyStruct{
		fieldA: "i am field a",
		fieldC: "i am field c",
		fieldB: func() int {
			return 0
		},
	}
}

func MyFunc3() MyStruct {
	return MyStruct{
		fieldA: "i am field a",
		fieldB: nil,
		fieldC: "i am field c",
	}
}

main_test.go

package playground

import (
	"testing"
)

func TestMyFunc(t *testing.T) {
	MyFunc()
	MyFunc2()
	MyFunc3()
}

What did you see happen?

Now run go test -cover -coverprofile=coverage.out ./...

cat coverage.out

mode: set
playground/main.go:9.24,12.22 1 1
playground/main.go:12.22,14.4 1 0
playground/main.go:19.25,23.22 1 1
playground/main.go:23.22,25.4 1 0
playground/main.go:29.25,35.2 1 1

Coverage is not calculated correctly for lines after each return 0 lines. Screenshot attached.

Image

What did you expect to see?

Coverage to be 100%.

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