fix(stringbytesroundtrip): reword []byte(string(b)) diagnostic — defensive copy, not redundant round-trip#47926
Merged
Merged
Conversation
5 tasks
…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
Contributor
🤖 PR Triage
Rationale: Corrects a linter diagnostic reword to prevent removal of a legitimate defensive Next step: Undraft, run CI, then fast-track for human approval.
|
Contributor
There was a problem hiding this comment.
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. |
Contributor
|
🎉 This pull request is included in a new release. Release: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[]byte(string(b))produces a non-aliasing copy ofb(the pre-slices.Cloneclone idiom); dropping it on the advice of "redundant" introduces an aliasing bug. Onlystring([]byte(s))is genuinely redundant — the result always equalss.Changes
stringbytesroundtrip.go): rewrites the[]byte(string(b))arm from "redundant round-trip" to "makes two copies to cloneb; useslices.Clone(b)orbytes.Clone(b)". Thestring([]byte(s))arm is unchanged.wantregexps updated to match the new message.47375-add-stringbytesroundtrip-linter.md): context and consequences sections corrected to reflect the semantic distinction.Before/after for the flagged message: