Skip to content

fix(stringbytesroundtrip): reword []byte(string(b)) diagnostic — defensive copy, not redundant round-trip#47926

Merged
pelikhan merged 2 commits into
mainfrom
copilot/fix-stringbytesroundtrip-issue
Jul 25, 2026
Merged

fix(stringbytesroundtrip): reword []byte(string(b)) diagnostic — defensive copy, not redundant round-trip#47926
pelikhan merged 2 commits into
mainfrom
copilot/fix-stringbytesroundtrip-issue

Conversation

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

[]byte(string(b)) produces a non-aliasing copy of b (the pre-slices.Clone clone idiom); dropping it on the advice of "redundant" introduces an aliasing bug. Only string([]byte(s)) is genuinely redundant — the result always equals s.

Changes

  • Diagnostic message (stringbytesroundtrip.go): rewrites the []byte(string(b)) arm from "redundant round-trip" to "makes two copies to clone b; use slices.Clone(b) or bytes.Clone(b)". The string([]byte(s)) arm is unchanged.
  • Package/analyzer docs: updated to distinguish the two arms — redundant vs. wasteful-but-intentional.
  • Golden test data: want regexps updated to match the new message.
  • ADR (47375-add-stringbytesroundtrip-linter.md): context and consequences sections corrected to reflect the semantic distinction.

Before/after for the flagged message:

// before
[]byte(string(b)) is a redundant round-trip; the inner string conversion copies the bytes unnecessarily

// after
[]byte(string(b)) makes two copies to clone b; use slices.Clone(b) or bytes.Clone(b) for a single-copy independent slice

…lect defensive-copy semantics

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix false positive for defensive copy in stringbytesroundtrip linter fix(stringbytesroundtrip): reword []byte(string(b)) diagnostic — defensive copy, not redundant round-trip Jul 25, 2026
Copilot AI requested a review from pelikhan July 25, 2026 06:16
@github-actions

Copy link
Copy Markdown
Contributor

🤖 PR Triage

Field Value
Category bug
Risk medium
Score 55 / 100
Score breakdown impact 38 + urgency 10 + quality 7
Action fast_track

Rationale: Corrects a linter diagnostic reword to prevent removal of a legitimate defensive []byte(string(b)) copy that guards against aliasing bugs. Still DRAFT with no CI runs — needs undraft and human review of the correctness claim.

Next step: Undraft, run CI, then fast-track for human approval.

Generated by 🔧 PR Triage Agent · sonnet46 · 44.5 AIC · ⌖ 5.13 AIC · ⊞ 5.7K ·

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Corrects the linter’s misleading treatment of defensive byte-slice copies.

Changes:

  • Rewords diagnostics and documentation to distinguish redundant conversions from defensive clones.
  • Updates golden test expectations.
  • Corrects the related ADR.
Show a summary per file
File Description
pkg/linters/stringbytesroundtrip/stringbytesroundtrip.go Revises analyzer documentation and diagnostics.
pkg/linters/stringbytesroundtrip/testdata/src/stringbytesroundtrip/stringbytesroundtrip.go Updates diagnostic expectations.
docs/adr/47375-add-stringbytesroundtrip-linter.md Documents the semantic distinction.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 3
  • Review effort level: Medium

Comment on lines +128 to +129
"[]byte(string(%s)) makes two copies to clone %s; use slices.Clone(%s) or bytes.Clone(%s) for a single-copy independent slice",
argText, argText, argText, argText,
Comment on lines +1 to +2
// Package stringbytesroundtrip implements a Go analysis linter that flags two
// related but semantically distinct patterns:

#### Positive
- Redundant `string([]byte(s))` and `[]byte(string(b))` patterns are caught automatically at CI time, preventing unnecessary memory allocations from reaching the main branch.
- `string([]byte(s))` patterns are caught as genuinely redundant at CI time, preventing unnecessary allocations from reaching the main branch.
@pelikhan
pelikhan merged commit 2feed75 into main Jul 25, 2026
25 checks passed
@pelikhan
pelikhan deleted the copilot/fix-stringbytesroundtrip-issue branch July 25, 2026 07:51
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.83.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants