Change information
Commit message:
cmd/cover: exclude commented-out code from coverage instrumentation
Add logic to exclude purely commented lines from coverage instrumentation.
When instrumenting Go code for coverage, the cover tool now identifies
and excludes lines that contain only comments from coverage blocks.
This prevents commented-out code from being reported as "uncovered"
in coverage reports, which can be misleading.
The implementation adds a splitBlockByComments function that parses
source code character by character to identify segments containing
executable code versus segments containing only comments. The
addCounters function now uses this to create coverage counters only
for segments that contain actual executable code.
The parser correctly handles:
- Single-line comments (//)
- Multi-line comments (/* */)
- String literals containing comment-like sequences
- Raw string literals with fake comments
- Mixed lines with both code and comments
This improves the accuracy of coverage reports by ensuring that
commented-out code, TODOs, and documentation comments don't inflate
the count of uncovered lines.
Fixes #22545
Change-Id: Ib428e6569011abb5f315387e81547147a2dadd2b
GitHub-Last-Rev: 915058146bb5f929f08d63ee191edebd51b2ab56
GitHub-Pull-Request: golang/go#76692
Files:
- M src/cmd/cover/cover.go
- M src/cmd/cover/cover_test.go
- M src/cmd/cover/testdata/html/html.golden
- M src/cmd/cover/testdata/main.go
- A src/cmd/cover/testdata/ranges/ranges.go
Change size: L
Delta: 5 files changed, 506 insertions(+), 14 deletions(-)
Branch: refs/heads/master
Submit Requirements:
Code-Review: +2 by Alan Donovan, +1 by Nicholas Husin
TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI