Skip to content

mapdeletecheck: suppress autofix when comments overlap the replaced span#46159

Merged
pelikhan merged 8 commits into
mainfrom
copilot/fix-mapdeletecheck-comment-guard
Jul 18, 2026
Merged

mapdeletecheck: suppress autofix when comments overlap the replaced span#46159
pelikhan merged 8 commits into
mainfrom
copilot/fix-mapdeletecheck-comment-guard

Conversation

Copilot AI commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

mapdeletecheck emitted SuggestedFix unconditionally, so any comment inside the if body was silently dropped when the autofix was applied. mapclearloop already guards against this; mapdeletecheck was inconsistent.

Changes

  • pkg/linters/internal/astutil — Added HasOverlappingComment(files, start, end), lifting the previously private helper from mapclearloop into the shared utility package.
  • pkg/linters/mapclearloop — Replaced local hasOverlappingComment with astutil.HasOverlappingComment.
  • pkg/linters/mapdeletecheck — Guards SuggestedFixes with astutil.HasOverlappingComment; diagnostic always fires, fix is suppressed when comments overlap the ifStmt span.
  • testdata — Added badWithComments() cases (leading and trailing comment) to assert diagnostic fires but no autofix is emitted.

Behavior

// Before: autofix would silently drop this comment
if _, ok := cache[key]; ok {
    // evict the stale entry before refetching
    delete(cache, key)
}

// After: diagnostic fires, fix is suppressed — developer applies it manually

Copilot AI and others added 3 commits July 17, 2026 07:12
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
…ix against comment loss

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix mapdeletecheck to guard comments during autofix mapdeletecheck: suppress autofix when comments overlap the replaced span Jul 17, 2026
Copilot AI requested a review from pelikhan July 17, 2026 07:21
@pelikhan
pelikhan marked this pull request as ready for review July 17, 2026 08:46
Copilot AI review requested due to automatic review settings July 17, 2026 08:46

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

Prevents mapdeletecheck autofixes from discarding comments by sharing comment-overlap detection with mapclearloop.

Changes:

  • Extracts HasOverlappingComment into shared AST utilities.
  • Suppresses unsafe fixes while retaining diagnostics.
  • Adds comment-related fixtures and refreshes workflow lock metadata.
Show a summary per file
File Description
pkg/linters/internal/astutil/astutil.go Adds shared overlap detection.
pkg/linters/mapclearloop/mapclearloop.go Uses shared helper.
pkg/linters/mapdeletecheck/mapdeletecheck.go Guards suggested fixes.
pkg/linters/mapdeletecheck/testdata/src/mapdeletecheck/mapdeletecheck.go Adds commented test cases.
pkg/linters/mapdeletecheck/testdata/src/mapdeletecheck/mapdeletecheck.go.golden Adds expected unchanged cases.
.github/workflows/skillet.lock.yml Normalizes action version metadata.
.github/workflows/release.lock.yml Normalizes action version metadata.
.github/workflows/hourly-ci-cleaner.lock.yml Normalizes action version metadata.
.github/workflows/avenger.lock.yml Normalizes action version metadata.

Review details

Tip

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

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

Message: "redundant existence check before delete: delete(" + mText + ", " + kText + ") is already a no-op when the key is absent; remove the if statement",
SuggestedFixes: []analysis.SuggestedFix{{
}
if !astutil.HasOverlappingComment(pass.Files, ifStmt.Pos(), ifStmt.End()) {
@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Design Decision Gate 🏗️ completed the design decision gate check.

No ADR enforcement needed: PR #46159 does not have the 'implementation' label and has only 58 new lines of code in business logic directories (threshold: 100).

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

PR Code Quality Reviewer completed the code quality review.

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Test Quality Sentinel completed test quality analysis.

No test files were added or modified in this PR. The PR (#46159) only modifies production code in pkg/linters/ (astutil, mapclearloop, mapdeletecheck) and test data files (.golden and testdata), but no *_test.go or *.test.js files. Test Quality Sentinel analysis skipped.

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot mentioned this pull request Jul 17, 2026

@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.

Clean refactor: extracts hasOverlappingComment into shared astutil.HasOverlappingComment, applies it consistently in both mapclearloop and mapdeletecheck, and adds test cases that confirm the diagnostic still fires while autofix is correctly suppressed. No issues found.

🧵 Reviewed using Impeccable skills by Impeccable Skills Reviewer · 18.8 AIC · ⌖ 5.35 AIC · ⊞ 5K

@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.

One existing unresolved bug blocks merge

The open review comment at line 124 of mapdeletecheck.go identifies a real correctness break: the // want directives on the bad() if-statements are trailing comments on the if line, falling inside [ifStmt.Pos(), ifStmt.End()). HasOverlappingComment therefore returns true for those cases too, suppressing their SuggestedFix — but the golden file expects the rewritten delete(...) form. The analysistest run will fail.

Lines 7 and 12 of the test fixture both carry // want ... as trailing comments inside the if-statement span. No fix is emitted. Golden expects the fix applied. Must be resolved before merge.

One low-severity nil-guard observation left inline on astutil.go.

🔎 Code quality review by PR Code Quality Reviewer · 49.2 AIC · ⌖ 4.41 AIC · ⊞ 5.6K
Comment /review to run again

if end <= file.Pos() || start >= file.End() {
continue
}
for _, group := range file.Comments {

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.

Nil file entry will panic: if files contains a nil *ast.File (e.g. a buggy or test-injected pass), file.Pos(), file.End(), and file.Comments will all panic.

💡 Suggested fix
for _, file := range files {
    if file == nil {
        continue
    }
    if end <= file.Pos() || start >= file.End() {

This won't happen in normal go/analysis usage since pass.Files is always populated by the framework, but the function is exported and could be called by future linters that build a synthetic file slice. The nil check is one line and removes the footgun.

@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 on two actionable issues.

📋 Key Themes & Highlights

Issues Found

  • Test fixture interference (critical): analysistest // want directives fall within ifStmt.Pos()..ifStmt.End(), meaning HasOverlappingComment may suppress the suggested fix even for the plain bad() cases during testing. The golden file needs verification that it reflects real auto-fix behaviour (see inline comment).
  • Missing unit test for extracted utility: HasOverlappingComment is a new shared function in astutil with no direct unit tests, leaving boundary conditions (adjacent comments, zero-width ranges) unexercised.

Positive Highlights

  • ✅ Clean extraction of the private helper into the shared astutil package.
  • ✅ Diagnostic always fires; only the autofix is suppressed — correct user experience.
  • badWithComments() test cases are cleanly separated and well-commented.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • proxy.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "proxy.golang.org"

See Network Configuration for more information.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · 45 AIC · ⌖ 4.49 AIC · ⊞ 6.7K
Comment /matt to run again

Comments that could not be inline-anchored

pkg/linters/internal/astutil/astutil.go:136

[/tdd] HasOverlappingComment has no unit test in astutil_test.go — correctness is only exercised indirectly through linter integration tests, leaving boundary conditions (e.g. comment.End == start, zero-width range) untested.

<details>
<summary>💡 Suggested unit test sketch</summary>

func TestHasOverlappingComment(t *testing.T) {
    src := &quot;package p\n// comment\nvar x = 1\n&quot;
    fset := token.NewFileSet()
    f, _ := parser.ParseFile(fset, &quot;x.go&quot;, src, parser.ParseComments)
  …

</details>

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot run pr-finisher skill

@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot please run the pr-finisher skill, investigate the failed checks listed below, address any remaining review feedback, and rerun checks once the branch is up to date.

Failed checks:

Generated by PR Sous Chef: https://github.com/github/gh-aw/actions/runs/29576656963

Generated by 👨‍🍳 PR Sous Chef · 6.7 AIC · ⌖ 7.14 AIC · ⊞ 7.5K ·
Comment /souschef to run again

…rectives from suppressing autofixes

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
… autofixes

The // want directives on the if line fell within [ifStmt.Pos(), ifStmt.End()),
causing HasOverlappingComment to suppress ALL fixes including bad() cases that
should have a fix. RunWithSuggestedFixes never populated allFilenames, so the
golden file comparison never ran.

Move // want to // want +1 on the line before each if statement so they are
outside the replaced span. Update the golden file to include the // want +1
comment lines that remain after fixes are applied.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

Copilot AI commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot run pr-finisher skill

Addressed in the latest commit. Moved all // want directives to // want +1 on the line before each if statement. This places the test directive before ifStmt.Pos(), keeping it outside the [ifStmt.Pos(), ifStmt.End()) span that HasOverlappingComment checks — so the bad() cases now correctly receive their suggested fix and the golden file comparison runs. Verified by deliberately corrupting the golden file: the test now fails as expected.

The function is exported and could be called with a synthetic file slice;
skip nil entries to avoid a panic on file.Pos()/file.End()/file.Comments.

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>

Copilot AI commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please run the pr-finisher skill, investigate the failed checks listed below, address any remaining review feedback, and rerun checks once the branch is up to date.

Failed checks:...

Investigated the CI failure. The Smoke CI job failed due to a runner infrastructure error — EACCES: permission denied, open '/tmp/awf-chroot-mf4XYK/hosts' — which is unrelated to the code changes in this PR. Also addressed the github-actions[bot] review thread: added a nil guard to HasOverlappingComment in commit fix(astutil): guard HasOverlappingComment against nil *ast.File entries. All local tests pass (make fmt, make test for affected packages).

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.82.13

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.

mapdeletecheck: autofix silently deletes comments inside the if body — no comment guard, unlike its sibling mapclearloop

4 participants