-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Problem
The Go coverage tool is very useful, but there's no way to mark a line as not needing coverage. It is common to add panic("unreachable") in place where one expects code never to run unless there is a programmatic error. These lines show up in code coverage output as untested and must be manually inspected in order to be noted that they can be ignored and don't need to be tested.
Proposal
If a line is prefaced by //go:cover ignore the following expression or block will be marked as "ignored".
// some switch ...
case 1:
return "1"
//go:cover ignore
default: // this line and the next will be marked as ignored
panic("unreachable")
// ...
$ go test -coverprofile=coverage.out
PASS
coverage: 42% of statements (2% ignored)
ok size 0.030s
In the HTML output of the cover tool, ignored lines should be marked with yellow. If an ignored line is covered during a test, it should be reported as "ignored but covered" and marked in purple for investigation of whether a programmatic error has taken place.
Cf #31280