[go] cmd/cover: exclude commented-out code from coverage instrumentation

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Feb 13, 2026, 1:09:54 PMFeb 13
to Rudy Regazzoni, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Nicholas Husin, Go LUCI, Alan Donovan, t hepudds, Than McIntosh, golang-co...@googlegroups.com

Gopher Robot submitted the change

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
Reviewed-by: Alan Donovan <adon...@google.com>
Auto-Submit: Alan Donovan <adon...@google.com>
Reviewed-by: Nicholas Husin <hu...@google.com>
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:
  • requirement satisfiedCode-Review: +2 by Alan Donovan, +1 by Nicholas Husin
  • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib428e6569011abb5f315387e81547147a2dadd2b
Gerrit-Change-Number: 726800
Gerrit-PatchSet: 10
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Nicholas Husin <hu...@google.com>
Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
Gerrit-CC: Rudy Regazzoni <rudy.re...@sonarsource.com>
Gerrit-CC: Than McIntosh <th...@golang.org>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages