Skip to content

testing: coverage calculation is wrong when runtime error occurs #62258

@Eiwinda

Description

@Eiwinda

What version of Go are you using (go version)?

$ go version
go1.20.5 linux/amd64

Does this issue reproduce with the latest release?

YES

What operating system and processor architecture are you using (go env)?

$ go env
<details><summary><code>GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jade/.cache/go-build"
GOENV="/home/jade/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/jade/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jade/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.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/jade/pasercover/go.mod"
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-build3250049939=/tmp/go-build -gno-record-gcc-switches"
</code> Output</summary><br><pre>
$ go env

</pre></details>

What did you do?

Here is the code to reproduce the issue.

package main

import (
	"fmt"
)

type Node struct {
	data int
	next *Node
}

type LinkedList struct {
	head *Node
}

func (ll *LinkedList) Insert(data int) {
	newNode := &Node{data: data, next: nil}

	if ll.head == nil {
		ll.head = newNode
	} else {
		current := ll.head
		for current.next != nil {
			current = current.next
		}
		current.next = newNode
	}
}

func (ll *LinkedList) Display() {
	current := ll.head
	for current != nil {
		fmt.Printf("%d ", current.data)
		current = current.next
	}
	fmt.Println()
}
func main() {
	divideByZero()
	fmt.Println("Main function continues to execute...")
}

func divideByZero() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("Recovered from:", err)
		}
	}()
	n := Node{1, nil}
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println(n.next.data)
        // lines below will be included in coverage, even n.next.data will trigger a nil error.
	fmt.Println("Result:") 
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println("Result:")
	fmt.Println("Result:")
}

What did you expect to see?

Lines below "fmt.Println(n.next.data)" will be excluded from the coverage.

What did you see instead?

parsecover.go:146: divideByZero 100.0%

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions