Repository navigation
[linter-miner] Add JSON unmarshal byte conversion linter - #75919
Conversation
Co-authored-by: gh-aw-bot <[email protected]>
|
✅ PR Specification Reviewer completed successfully! PR Specification ReviewerOutcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
|
❌ Design Decision Gate 🏗️ failed to deliver outputs during design decision gate check.
|
|
✅ PR Code Quality Reviewer completed the code quality review.
|
|
✅ Test Quality Sentinel completed test quality analysis. Test Quality SentinelOutcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
|
✅ Ponytail Reviewer completed successfully! Ponytail ReviewerOutcome: The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible. Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.
|
|
🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅
|
Comment MemoryPeek at saved memory (pr-code-quality-reviewer)Note This comment is managed by comment memory.Expand the saved memory block to view or edit the persistent context for this thread.
|
There was a problem hiding this comment.
Review summary
Request changes: the analyzer's prescribed replacement can silently accept trailing non-whitespace input that json.Unmarshal rejects. The inline comment describes the semantic regression and required guard.
The specialist pass was unavailable because its file-read tool access was denied; this review is based on the pre-fetched diff and independent analysis.
🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~2 AIC · ⌖ ~2 AIC · ⊞ 1.6K
Comment /review to run again
| !coverage.ShouldApply(pass, call.Pos(), *hotThreshold) { | ||
| return | ||
| } | ||
| pass.ReportRangef(call, "json.Unmarshal([]byte(s), ...) copies the string; use json.NewDecoder(strings.NewReader(s)).Decode(...) to avoid the conversion copy") |
There was a problem hiding this comment.
This diagnostic recommends a rewrite that changes json.Unmarshal semantics by accepting a valid first value followed by non-whitespace trailing data, which json.Unmarshal rejects.
💡 Preserve Unmarshal's complete-input validation
For example, json.NewDecoder(strings.NewReader(s)).Decode(&v) succeeds on {} garbage, whereas json.Unmarshal([]byte(s), &v) returns an error. Either avoid prescribing this rewrite generally or require an explicit EOF/second-decode check (and add a regression fixture) before suggesting it.
|
Warning The 🏗️ Design Decision Gate — ADR RequiredThis PR makes significant changes to core business logic (>100 new lines in business-logic directories; the pre-fetched gate summary counted 164 new lines) but does not have a linked Architecture Decision Record (ADR). 📄 Draft ADR committed:
📋 What to do next
The PR adds and registers Once the ADR is linked in the PR body, this gate will re-run and verify that the implementation matches the decision. ❓ Why ADRs MatterADRs create a searchable, permanent record of why the codebase looks the way it does and make significant trade-offs explicit for future contributors. 📋 Michael Nygard ADR Format ReferenceAn ADR must contain these four sections to be considered complete:
All records are stored in
|
There was a problem hiding this comment.
🟡 Changes recommended
The linter recommends a single Decode that can accept trailing input rejected by Unmarshal, potentially weakening whole-document validation unless the advice is explicitly limited to stream semantics.
1 open issue
1 file not reviewed
pkg/linters/jsonunmarshalbytesconversion/testdata/src/jsonunmarshalbytesconversion/generated.go: Generated file
What changed in this PR
Adds jsonunmarshalbytesconversion to gh-aw’s custom Go analyzers to flag string-to-byte conversions passed to json.Unmarshal, addressing #75205.
Changes:
- Detects conversions, including parenthesized forms, with generated-file skipping,
nolintsupport, and coverage-based gating. - Adds analyzer tests and fixtures.
- Registers and documents the analyzer while deferring CI enforcement pending review of existing findings.
| File | Description |
|---|---|
pkg/linters/spec_test.go |
Adds the analyzer to documentation synchronization checks. |
pkg/linters/registry.go |
Registers the analyzer with the shared runner. |
pkg/linters/README.md |
Documents detection and recommended alternatives. |
pkg/linters/jsonunmarshalbytesconversion/testdata/src/jsonunmarshalbytesconversion/jsonunmarshalbytesconversion.go |
Covers detection, suppression, and non-matching calls. |
pkg/linters/jsonunmarshalbytesconversion/testdata/src/jsonunmarshalbytesconversion/generated.go |
Provides a generated-file exclusion fixture. |
pkg/linters/jsonunmarshalbytesconversion/jsonunmarshalbytesconversion.go |
Implements detection, filtering, and diagnostics. |
pkg/linters/jsonunmarshalbytesconversion/jsonunmarshalbytesconversion_test.go |
Runs the analyzer against its fixtures. |
pkg/linters/doc.go |
Updates the analyzer inventory and count. |
pkg/linters/doc_sync_test.go |
Records deferred CI enforcement. |
Review effort: Balanced (auto)
| !coverage.ShouldApply(pass, call.Pos(), *hotThreshold) { | ||
| return | ||
| } | ||
| pass.ReportRangef(call, "json.Unmarshal([]byte(s), ...) copies the string; use json.NewDecoder(strings.NewReader(s)).Decode(...) to avoid the conversion copy") |
There was a problem hiding this comment.
Skills-Based Review 🧠
Applied /grill-with-docs and /tdd. Requesting changes: the analyzer’s replacement advice is not behaviour-preserving, and the new analyzer has no test that would catch that.
📋 Key Themes & Highlights
Key Themes
- Incorrect replacement advice:
json.NewDecoder(strings.NewReader(s)).Decode(...)accepts trailing data (for example{"a":1} xyz) thatjson.Unmarshalrejects. It also allocates more thanjson.Unmarshalin a 64 KiB benchmark (326 KB/16 allocs vs 139 KB/7 allocs), so it does not avoid the copy. A behaviour-preserving pattern is in the inline comment on the analyzer. - Docs drift: the README bullet and analyzer message repeat the same advice, and the README Dependencies list is missing the new analyzer.
- Test gap:
analysistestchecks diagnostic text only, so no test covers the semantics that the advice depends on.
Positive Highlights
- ✅ Clean analyzer shape: it reuses the shared
astutil,nolint,filecheck, andcoveragehelpers, and skips generated files. - ✅ Handles parenthesised conversions and named string types, and has a
nolintsuppression case. - ✅ Registry,
doc.go,spec_test.go, and the README Subpackages table are updated consistently. - ✅ Local checks pass:
go test ./pkg/linters/...exits 0 (99 packages), including the README/doc sync tests.
Uncertainty
I did not review the wider repository for other json.Unmarshal call sites, so the impact of enabling this in CI is not assessed. The CI enforcement deferral in notYetEnforced is left as-is.
@copilot please address the review comments above.
Comments that could not be inline-anchored
pkg/linters/jsonunmarshalbytesconversion/jsonunmarshalbytesconversion.go:57
**[/grill-with-docs]** The suggested replacement is not behaviour-preserving, and it does not avoid the copy this message claims to avoid. `Decode` silently accepts trailing data, for example `{"a":1} xyz`, which `json.Unmarshal` rejects. Do not recommend it as written. <details> <summary>💡 Behaviour-preserving alternative and measurements</summary> This pattern matches `json.Unmarshal` error behaviour on every input I tried, including trailing data, a second value, and an extra closing brace...
pkg/linters/README.md:45
**[/grill-with-docs]** This bullet repeats the replacement advice from the analyzer message, which is not behaviour-preserving and does not avoid the copy (see the comment on jsonunmarshalbytesconversion.go line 57). Update it in the same commit as the message. `doc_sync_test.go` only checks analyzer names, so it will not catch wording drift. @copilot please address this.
pkg/linters/jsonunmarshalbytesconversion/jsonunmarshalbytesconversion_test.go:13
**[/tdd]** Nothing pins the behaviour that the advice depends on. `analysistest` only checks the diagnostic text, so the wrong rewrite passed CI. <details> <summary>💡 Suggested test</summary> If the advice stays, add a table-driven runtime test next to `TestAnalyzer`. It should check that the recommended rewrite returns an error for trailing data such as `{"a":1} xyz`, and that its error behaviour matches `json.Unmarshal` on the same inputs. A future rewrite that changes semantics would then ...
pkg/linters/README.md:380
**[/grill-with-docs]** The **Dependencies** list has no entry for `jsonunmarshalbytesconversion`, but every sibling analyzer has one (for example `jsonmarshalignoredeerror` on this line). The doc-sync tests only parse the Subpackages table, so this gap is not caught. Add the matching bullet here. @copilot please address this.
🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~7 AIC · ⌖ ~2 AIC · ⊞ 5.4K
Comment /matt to run again
|
Copilot assignment requested for the following outstanding work:
Once you start work, post this work summary as a PR comment without the leading Copilot mention, including the Sous-chef head, work, and state lines below, to record that the assignment is running. Apply the necessary fixes on this PR's existing branch, reply to each listed review thread and resolve it when addressed. Ignore feedback already answered or resolved; stop when only human review or CI remains. Do not merge or trigger CI. Sous-chef requested-head: b021c25
|
Assignment started. I’m merging the latest main, correcting the diagnostic so decoder advice preserves whole-document validation, and investigating the reported safe_outputs failure. Sous-chef requested-head: b021c25 |
…son-unmarshal-bytes-conversion Co-authored-by: gh-aw-bot <[email protected]>
Co-authored-by: gh-aw-bot <[email protected]>
|
🎉 This pull request is included in a new release. Release: |

json.Unmarshal([]byte(s), ...)copies string data into a byte slice. Add a custom analyzer to flag this conversion and suggest streaming from a string reader.[]byteconversions passed toencoding/json.Unmarshal, including parenthesized conversions. Skips generated files, honorsnolint, and supports hot-path coverage gating.