fix(seenmapbool): eliminate duplicate diagnostics for set-maps in closures#40741
Open
Copilot wants to merge 3 commits into
Open
fix(seenmapbool): eliminate duplicate diagnostics for set-maps in closures#40741Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
…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
This was referenced Jun 22, 2026
Contributor
There was a problem hiding this comment.
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.Inspectpass at*ast.FuncLitboundaries 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
falsewrite 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. |
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.
seenmapboolemitted the samemap[string]booldiagnostic N+1 times for a set-map declared N levels deep in closures. The firstast.Inspectpass ininspectBodyrecursed into nested*ast.FuncLitbodies, so each closure's maps were collected by both the enclosing function's pass and the closure's ownPreorder-triggered pass.Changes
pkg/linters/seenmapbool/seenmapbool.go— firstast.Inspectpass now returnsfalseon*ast.FuncLit, stopping recursion at closure boundaries. The second (write-disqualification) pass still recurses into closures so that a non-truewrite inside a closure correctly disqualifies an outer-scoped map candidate.testdata/src/seenmapbool/seenmapbool.go— three new cases:BadSetBoolInClosure/BadSetBoolInNestedClosure— assert exactly one// wantat 1- and 2-level nestingGoodOuterMapWrittenFalseInClosure— outer map writtenfalsein closure, must not be reported (guards the second-pass recursion)