Skip to content

fix(seenmapbool): eliminate duplicate diagnostics for set-maps in closures#40741

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/seenmapbool-fix-duplicate-diagnostics
Open

fix(seenmapbool): eliminate duplicate diagnostics for set-maps in closures#40741
Copilot wants to merge 3 commits into
mainfrom
copilot/seenmapbool-fix-duplicate-diagnostics

Conversation

Copilot AI commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

seenmapbool emitted the same map[string]bool diagnostic N+1 times for a set-map declared N levels deep in closures. The first ast.Inspect pass in inspectBody recursed into nested *ast.FuncLit bodies, so each closure's maps were collected by both the enclosing function's pass and the closure's own Preorder-triggered pass.

Changes

  • pkg/linters/seenmapbool/seenmapbool.go — first ast.Inspect pass now returns false on *ast.FuncLit, stopping recursion at closure boundaries. The second (write-disqualification) pass still recurses into closures so that a non-true write inside a closure correctly disqualifies an outer-scoped map candidate.

  • testdata/src/seenmapbool/seenmapbool.go — three new cases:

    • BadSetBoolInClosure / BadSetBoolInNestedClosure — assert exactly one // want at 1- and 2-level nesting
    • GoodOuterMapWrittenFalseInClosure — outer map written false in closure, must not be reported (guards the second-pass recursion)
func Dedup(xs []string) []string {
    unique := func(in []string) []string {
        seen := make(map[string]bool) // reported once, not twice
        // ...
    }
    return unique(xs)
}

Copilot AI and others added 2 commits June 22, 2026 06:41
…licate diagnostics

The first `ast.Inspect` pass in `inspectBody` now returns `false` when it
encounters a nested `*ast.FuncLit`, so maps declared inside closures are
collected only by the closure's own Preorder visit — not re-collected by the
enclosing function's pass. The second (write-disqualification) pass still
recurses into closures so that a non-true write made inside a closure
correctly prevents reporting the outer function's map as a pure set.

Test cases added:
- BadSetBoolInClosure: single // want in a 1-level closure
- BadSetBoolInNestedClosure: single // want in a 2-level closure
- GoodOuterMapWrittenFalseInClosure: outer map disqualified by closure write

Closes #40733

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Address code review: check n == nil before the *ast.FuncLit type assertion
for clearer control flow, since ast.Inspect calls the callback with nil
after visiting each node's children.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate diagnostics for set-maps in function literals fix(seenmapbool): eliminate duplicate diagnostics for set-maps in closures Jun 22, 2026
Copilot AI requested a review from pelikhan June 22, 2026 06:42
@pelikhan pelikhan marked this pull request as ready for review June 22, 2026 14:16
Copilot AI review requested due to automatic review settings June 22, 2026 14:16

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

Note

Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

Fixes duplicate map[string]bool “used as a set” diagnostics when a set-map is declared inside nested closures by preventing the initial candidate-collection traversal from descending into *ast.FuncLit bodies.

Changes:

  • Stop the first ast.Inspect pass at *ast.FuncLit boundaries to avoid re-collecting closure-local candidates from outer traversals.
  • Add test cases covering 1- and 2-level closure nesting (should report once) and verifying that a false write inside a closure disqualifies an outer-scoped map.
Show a summary per file
File Description
pkg/linters/seenmapbool/seenmapbool.go Prevents duplicate diagnostics by halting the first traversal at closure boundaries.
pkg/linters/seenmapbool/testdata/src/seenmapbool/seenmapbool.go Adds regression tests for closure nesting and disqualification via writes inside closures.

Copilot's findings

Tip

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

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

Comment on lines 72 to +78
ast.Inspect(body, func(n ast.Node) bool {
if n == nil {
return false
}
if _, ok := n.(*ast.FuncLit); ok {
return false // do not descend into nested closures
}
Comment on lines +59 to +60
func BadSetBoolInNestedClosure() {
// Two levels of nesting: each set-map is reported exactly once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants