Skip to content

[linter-miner] Add mapcopyloop analyzer - #78958

Closed
gh-aw-bot wants to merge 6 commits into
mainfrom
copilot/linter-minermap-copy-loop-e5c786f5623b6782
Closed

gh-aw-bot wants to merge 6 commits into
mainfrom
copilot/linter-minermap-copy-loop-e5c786f5623b6782

Conversation

@gh-aw-bot

@gh-aw-bot gh-aw-bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Add a readability linter for range-over-map loops that copy each entry into another map. It reports only single-statement dst[k] = v loops and can suggest replacing them with maps.Copy.

  • Matching: Skips self-copies, blank range variables, compound assignments, extra statements, and nolint:mapcopyloop.
  • Suggested fix: Offered only when maps is visible and both maps have the same type.
  • Integration: Registers and documents the analyzer; leaves it advisory until existing findings are addressed.
for k, v := range src {
    dst[k] = v
}

// Suggested replacement
maps.Copy(dst, src)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@gh-aw-bot gh-aw-bot changed the title [WIP] Add mapcopyloop analyzer for range-copy loops [linter-miner] Add mapcopyloop analyzer Nov 18, 2026
@pelikhan
pelikhan marked this pull request as ready for review November 18, 2026 07:15
Copilot AI adaptive review requested due to automatic review settings November 18, 2026 07:15
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Code Quality Reviewer completed the code quality review.

PR Code Quality Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt56 · ~3 AIC · ⌖ ~1 AIC · ⊞ 1.5K · ◷

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

✅ Test Quality Sentinel completed test quality analysis.

Test Quality Sentinel

Outcome: reported_no_action

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.

🧪 Test quality analysis by Test Quality Sentinel · copilot · routed: haiku55 xhigh · ~1 AIC · ⌖ ~2 AIC · ⊞ 7.2K · ◷

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Specification Reviewer completed successfully!

PR Specification Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

Agent messages are omitted from this comment. The category uses structured run status, not interpretation of agent prose.

Generated by PR Specification Reviewer for #78958 · copilot · gpt56 · ~2 AIC · ⌖ ~1 AIC · ⊞ 1.7K · ◷

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

✅ Ponytail Reviewer completed successfully!

Generated by Ponytail Reviewer for #78958 · codex · routed: gpt61 medium · ~6 AIC · ⌖ ~1 AIC · ⊞ 185 · ◷

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~7 AIC · ⌖ ~2 AIC · ⊞ 6.6K · ◷

@github-actions github-actions Bot 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.

What this change does: Adds an analyzer that finds map-copy loops and suggests maps.Copy when it is available. It registers the analyzer as advisory.

One simplification is noted inline. Verdict: Ship.

Lean: -11 lines possible.

Generated by ✂️ Ponytail Reviewer for #78958 · codex · routed: gpt61 medium · ~6 AIC · ⌖ ~1 AIC · ⊞ 185
Comment /ponytail to run again

Comment thread pkg/linters/mapcopyloop/mapcopyloop.go Outdated
if len(rangeStmt.Body.List) != 1 {
return nil, false
}
var bodyStmt ast.Stmt

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.

Should fix: The body, left side, and right side are each checked to have exactly one item, so use [0] directly in all three places; the three loops add 11 lines without handling any additional case.

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.

Replaced the slice traversals with guarded [0] accesses; the exact-length checks are documented for the custom linter. Included in 0dd7ca8.

@github-actions

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (spec-review-history)
{"id":"ldg-eec7041a-6dd8-4503-a552-9c146c9ac726","parents":[],"payload":{"id":"ldg-434b659c-a4d8-4449-a960-0728e2bcec05","operation":"append","value":{"finding_count":0,"head_sha":"31ab839be54b5e1142329a3c5c72052ba1883e6c","outcome":"aligned"}},"sha":"sha256:671bc1af64ab1c176b5d697bf155eb20e37bd3a111feec38fe080c54ad54051d","timestamp":"2026-11-18T07:23:04.969Z","type":"ledger_append","version":1}
History

@github-actions

Copy link
Copy Markdown
Contributor

🏗️ Design Decision Gate — ADR Required

This PR makes significant changes to core business logic (>100 new lines) but does not have a linked Architecture Decision Record (ADR).

📄 Draft ADR committed: docs/adr/78958-add-mapcopyloop-linter.md — review and complete it before merging.

🔒 This PR cannot merge until an ADR is linked in the PR body.

📋 What to do next
  1. Review the draft ADR committed to your branch. It was generated from the PR diff.

  2. Complete the missing sections. Add context the draft could not infer, refine the decision rationale, and replace the [TODO: verify] markers with real alternatives and measured data.

  3. Set the status from Draft to Proposed or Accepted once reviewed.

  4. Reference the ADR in this PR body by adding a line such as:

    ADR: ADR-78958: Add mapcopyloop Linter to Flag Range-Copy Loops Replaceable with maps.Copy (link unavailable: invalid HTTPS URL)

Once an ADR is linked in the PR body, this gate will re-run and verify the implementation matches the decision.

🔎 Evidence used
  • Business-logic additions: 372 lines under pkg/ (default config; no .design-gate.yml). No implementation label.
  • No ADR found in the PR body, on the branch, or in linked issue [linter-miner] linters: add mapcopyloop analyzer for range-copy loops #78801. The issue body is a linter-miner proposal, not an ADR.
  • Precedent: sibling linter ADRs such as docs/adr/46060-add-mapclearloop-linter.md.
📋 Michael Nygard ADR format reference

An ADR must contain Context, Decision, Alternatives Considered, and Consequences to be considered complete. ADRs live in docs/adr/ and are numbered by PR number.

References: §44257509648

🏗️ ADR gate enforced by Design Decision Gate 🏗️ · pi · routed: haiku55 xhigh · ~3 AIC · ⌖ ~2 AIC · ⊞ 8.6K · ◷
Comment /review to run again

@github-actions

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (pr-code-quality-reviewer)
reviewed_at: 2026-11-18T07:20:00Z
review_event: REQUEST_CHANGES
top_themes:
  - unsafe suggested fixes for assignment-form ranges
  - side-effecting or loop-variable-dependent destination expressions
files_reviewed:
  - pkg/linters/mapcopyloop/mapcopyloop.go
  - pkg/linters/mapcopyloop/mapcopyloop_test.go
  - pkg/linters/mapcopyloop/testdata/src/mapcopyloop/mapcopyloop.go
  - pkg/linters/mapcopyloop/testdata/src/mapcopyloop/mapcopyloop.go.golden
  - pkg/linters/mapcopyloop/registry.go
comment_count: 2
History

Note

This comment is managed by comment memory.

Expand the saved memory block to view or edit the persistent context for this thread.
Edit only the text inside the backtick fences; workflow metadata and the footer are regenerated automatically.

Learn more about comment memory

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~3 AIC · ⌖ ~1 AIC · ⊞ 1.5K · ◷
Comment /review to run again

@github-actions github-actions Bot 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.

Review verdict

Please address the unsafe suggested-fix transformations before merging. The current analyzer can remove observable assignments to outer range variables and can generate behavior-changing or uncompilable replacements for non-stable destination expressions.

See the inline comments for targeted fixes and regression coverage.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~3 AIC · ⌖ ~1 AIC · ⊞ 1.5K
Comment /review to run again

bodyStmt = stmt
}
assignStmt, ok := bodyStmt.(*ast.AssignStmt)
if !ok || assignStmt.Tok != token.ASSIGN || len(assignStmt.Lhs) != 1 || len(assignStmt.Rhs) != 1 {

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.

The suggested rewrite is also offered for for k, v = range src, but this form assigns the final key and value into outer variables; replacing the loop with maps.Copy removes those assignments and changes observable behavior after the loop.

💡 Preserve outer-variable semantics

Restrict this transformation to rangeStmt.Tok == token.DEFINE, or otherwise prove the range variables are not used after the loop, and add a regression test for the assignment form.

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.

Assignment-form ranges are now excluded, and the fixture/golden preserve the = loop unchanged. Included in 0dd7ca8.

TextEdits: []analysis.TextEdit{{
Pos: rangeStmt.Pos(),
End: rangeStmt.End(),
NewText: []byte("maps.Copy(" + dstText + ", " + srcText + ")"),

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.

This fix is unsafe for destination expressions that are not stable map references, such as pick()[k] = v or groups[k][k] = v: it moves the destination evaluation outside the loop, changing call counts or producing code that references the loop variable after its scope (and can fail to compile).

💡 Limit suggested fixes to stable destinations

Only offer the rewrite when the indexed map expression is an identifier or a side-effect-free selector chain that does not reference the range variables; otherwise keep the diagnostic but omit the suggested fix. Add regression cases for a function-call destination and a destination indexed by k.

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.

Suggested fixes now require stable operands and reject destination references to range variables; nested-index and call cases cover the exclusions. Included in 0dd7ca8.

@github-actions

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (review-history)
{"id":"ldg-277d34fd-18ff-4bff-8da1-3d1a737a3c46","parents":[],"payload":{"id":"ldg-0f0a9683-9e18-47f2-9169-f0da81109c61","operation":"append","value":{"finding_count":2,"head_sha":"31ab839be54b5e1142329a3c5c72052ba1883e6c","outcome":"findings"}},"sha":"sha256:896cf0f7c12b6ea3477923acd5ad42031ae7485291ffde478419bf6a22ff528f","timestamp":"2026-11-18T07:24:39.934Z","type":"ledger_append","version":1}
History

@github-actions github-actions Bot 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.

Skills-Based Review 🧠

Applied /diagnosing-bugs and /tdd. Requesting changes: the suggested fix for the for k, v = range form produces code that does not compile, and the golden test enshrines that output.

📋 Key Themes & Highlights

Key Themes

  • Broken autofix for = loops: go vet rejects the golden file with declared and not used: k. The test passes only because the golden was generated from the bad rewrite.
  • Destination evaluated once: a call in the destination (for example getMap()[k] = v) would run once per entry in the loop but only once after maps.Copy. The fix should be limited to plain identifiers and selectors.

Positive Highlights

  • ✅ Clear, bounded matching rules: self-copies, blank variables, compound assignment, extra statements, and nolint are all skipped.
  • ✅ Shadowed maps package and comment-bearing loops are handled conservatively.
  • ✅ Advisory rollout via notYetEnforced is consistent with the existing linters.

Verification

  • go test ./pkg/linters/mapcopyloop/... passes, but only because the golden encodes the broken output.
  • go test ./pkg/linters/ passes.

@copilot please address the review comments above.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~7 AIC · ⌖ ~2 AIC · ⊞ 6.6K
Comment /matt to run again

Comment thread pkg/linters/mapcopyloop/mapcopyloop.go Outdated
End: rangeStmt.End(),
Message: "range-copy loop can be replaced with maps.Copy(" + dstText + ", " + srcText + ")",
}
if mapsPackageVisible(pass.Pkg, rangeStmt.Pos()) &&

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.

[/diagnosing-bugs] The suggested fix ignores the range form. For for k, v = range src (rangeStmt.Tok == token.ASSIGN) the rewrite leaves the outer k and v unused, so the fixed file does not compile. It also drops their final values after the loop, which changes behavior.

Offer the fix only for := loops and report the = form without a fix.

💡 Suggested guard
if rangeStmt.Tok == token.DEFINE &&
    mapsPackageVisible(pass.Pkg, rangeStmt.Pos()) &&
    !astutil.HasOverlappingComment(pass.Files, rangeStmt.Pos(), rangeStmt.End()) {

@copilot please address this.

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.

The analyzer now skips = range loops; their fixture remains unchanged. Included in 0dd7ca8.

return
}
srcText := astutil.NodeText(pass.Fset, rangeStmt.X)
dstText := astutil.NodeText(pass.Fset, indexExpr.X)

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.

[/diagnosing-bugs] dstText is evaluated once per iteration in the original loop but only once in maps.Copy. If the destination contains a call, such as getMap()[k] = v, the fix changes how often getMap runs. Restrict the fix to destinations made of identifiers and selectors, and add a call-destination case that expects a diagnostic with no suggested fix.

@copilot please address this.

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.

Function-call destinations and sources now retain diagnostics without suggested fixes; regression fixtures cover both. Included in 0dd7ca8.


var k string
var v int
for k, v = range src { // want `range-copy loop can be replaced with maps.Copy\(dst, src\)`

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.

[/tdd] This = case should keep its diagnostic but expect no suggested fix. The golden output for this loop must remain the original loop, not maps.Copy.

@copilot please address this.

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.

The assignment-form fixture now expects no suggested fix and remains unchanged in the golden output. Included in 0dd7ca8.


maps.Copy(dst, src)

var k string

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.

[/tdd] This golden output is not valid Go: go vet reports declared and not used: k. The test passes only because the golden captured the broken rewrite. Regenerate the golden after the guard fix so the = loop stays unchanged.

@copilot please address this.

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.

The golden preserves the assignment-form loop, and the test type-checks the rewritten source. Included in 0dd7ca8.

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.

🟡 Changes recommended

The suggested maps.Copy rewrites can change program behavior or produce invalid code, while current fixtures never exercise the fix path.

3 open issues
What changed in this PR

Adds mapcopyloop, a readability analyzer for gh-aw’s custom Go linter suite that recommends maps.Copy for map-copy loops.

Changes:

  • Implements loop matching, suppression handling, and conditional suggested fixes.
  • Adds diagnostic fixtures and golden replacement expectations.
  • Registers and documents the analyzer while deferring CI enforcement until existing findings are addressed.
File Description
pkg/​linters/​spec_test.go Adds the analyzer to documentation checks.
pkg/​linters/​registry.go Registers the analyzer.
pkg/​linters/​README.md Documents the rule and subpackage.
pkg/​linters/​mapcopyloop/​testdata/​src/​mapcopyloopnoimport/​mapcopyloopnoimport.go Adds a diagnostic case without importing maps.
pkg/​linters/​mapcopyloop/​testdata/​src/​mapcopyloop/​mapcopyloop.go.golden Records expected replacement output.
pkg/​linters/​mapcopyloop/​testdata/​src/​mapcopyloop/​mapcopyloop.go Covers matching, exclusions, and suppression.
pkg/​linters/​mapcopyloop/​mapcopyloop.go Implements detection and suggested fixes.
pkg/​linters/​mapcopyloop/​mapcopyloop_test.go Runs diagnostic and suggested-fix checks.
pkg/​linters/​doc.go Adds the analyzer to package documentation.
pkg/​linters/​doc_sync_test.go Records deferred CI enforcement.

Review effort: Balanced (adaptive)

Comment on lines +61 to +64
dstType := pass.TypesInfo.TypeOf(indexExpr.X)
if !isMap(dstType) || !types.Identical(dstType, srcType) {
return
}

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.

Fixes now require side-effect-free identifier/selector operands and a destination independent of range variables; nested indexing and calls are covered. Included in 0dd7ca8.

func rangeObjects(pass *analysis.Pass, rangeStmt *ast.RangeStmt) (types.Object, types.Object, bool) {
keyIdent, keyOK := rangeStmt.Key.(*ast.Ident)
valueIdent, valueOK := rangeStmt.Value.(*ast.Ident)
if !keyOK || !valueOK || keyIdent.Name == "_" || valueIdent.Name == "_" {

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.

The analyzer now skips assignment-form ranges, keeping their outer-variable behavior and source intact. Included in 0dd7ca8.

func TestAnalyzer(t *testing.T) {
t.Parallel()
testdata := analysistest.TestData()
analysistest.RunWithSuggestedFixes(t, testdata, mapcopyloop.Analyzer, "mapcopyloop")

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.

Added a comment-free positive fixture that emits a fix, and a test type-checks the rewritten golden file. Included in 0dd7ca8.

@gh-aw-bot

Copy link
Copy Markdown
Collaborator Author

@copilot

🍳 A little prep to help this PR along! Copilot assignment requested for the following outstanding work:

  1. Update this branch with the latest main using make merge-main, resolving any conflicts and preserving the intended changes.
  2. Review (pkg/linters/mapcopyloop/mapcopyloop.go:117): address unresolved feedback - Review comment
  3. Review (pkg/linters/mapcopyloop/mapcopyloop.go:122): address unresolved feedback - Review comment
  4. Review (pkg/linters/mapcopyloop/mapcopyloop.go:83): address unresolved feedback - Review comment
  5. Review (pkg/linters/mapcopyloop/mapcopyloop.go:76): address unresolved feedback - Review comment
  6. Review (pkg/linters/mapcopyloop/mapcopyloop.go:66): address unresolved feedback - Review comment
  7. Review (pkg/linters/mapcopyloop/testdata/src/mapcopyloop/mapcopyloop.go:19): address unresolved feedback - Review comment
  8. Review (pkg/linters/mapcopyloop/testdata/src/mapcopyloop/mapcopyloop.go.golden:15): address unresolved feedback - Review comment
  9. Review (pkg/linters/mapcopyloop/mapcopyloop.go:64): address unresolved feedback - Review comment
  10. Review (pkg/linters/mapcopyloop/mapcopyloop.go:101): address unresolved feedback - Review comment
  11. Review (pkg/linters/mapcopyloop/mapcopyloop_test.go:16): address unresolved feedback - Review comment

Keeping the changes focused; human review and CI get the final taste test. No merging or CI triggering.

Structured data
{
  "completed_task": 0,
  "head": "25924b6d2bdb1ef2fbc9f55e67d9a73da445e0c2",
  "kind": "requested",
  "schema_version": "1",
  "state": "e307b3121bd2e5e866b5e6ea8e61c191e9f3ea9332af4a7c7112f07ca272c745",
  "work": [
    "025d8c73a512c851a34fb3206ffeb52616720dcc6b820472e7c789cea24483bf",
    "074f874c085acada191a64c87bbe11dda281b6dd6574a5649dc0b94430c37779",
    "1b6acb702e4908f394d8597f78eac49a338f88a129069623dcb04aac535297d6",
    "219aa7fb6e77d6475b28342ea6d5413b7fe5d84c742471efb5ec981a9836cc26",
    "31d3efc53f09b7a7eae3a47e52d681dbca2375f81afebbae53d32987f5727791",
    "95e3ad84de73d5d8326e40af51426c5e7bb77c8a05971f21553b3e7db23388bb",
    "9cef6c9d15bda6048fba74e41b251d1c2afb8dc70a4a29334a6155c1be4f2409",
    "a2714d789b51f2265e8a4c5b704f31a921ac3cc64382b822ec4edf2c01153a1b",
    "af9bb3f18d91437b8ce3a32cfec08557feb0b12255a0a4e8a514bee9154633c4",
    "d7daeb44e0d54603607d7ad9c5292548b14452676bd5277b5ddd56729fb7340b"
  ],
  "workflow": "pr-sous-chef"
}

Generated by 👨🍳 PR Sous Chef · pi · routed: haiku55 xhigh · ~2 AIC · ⌖ ~2 AIC · ⊞ 7K · ◷
Comment /souschef to run again

…ap-copy-loop-e5c786f5623b6782

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
gh-aw-bot and others added 2 commits November 18, 2026 08:18
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Category: feature | Risk: medium | Score 62/100 (impact 28 + urgency 17 + quality 17) | Action: auto_merge | Reason: new linter analyzer is scoped and CI validated; keep it moving after a standard review.

Generated by 🔧 PR Triage Agent · copilot · mai11 · ~6 AIC · ⌖ ~1 AIC · ⊞ 13K · ◷

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[linter-miner] linters: add mapcopyloop analyzer for range-copy loops

4 participants