Repository navigation
fix(linters): safely resolve reflect.DeepEqual function aliases - #67636
Conversation
Reject mutable and escaped aliases using type-object identity, and cover alias chains, import forms, reassignment, and unrelated functions. Keep reflectdeepequalusage CI enforcement unchanged. Co-authored-by: Copilot App <[email protected]>
|
🔎 PR Code Quality Reviewer is reviewing code quality for this pull request... |
|
✅ Design Decision Gate 🏗️ completed the design decision gate check. See the comment below for the result and any generated ADR draft.
|
|
✅ Ponytail Reviewer completed successfully! Lean already. Ship.
|
|
✅ Test Quality Sentinel completed test quality analysis. No test files were added or modified in this PR. Test Quality Sentinel skipped.
|
|
🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅
|
There was a problem hiding this comment.
🟢 Approval recommended
The implementation is conservative, type-aware, and supported by comprehensive positive and negative regression fixtures.
0 open findings
What changed in this PR
Safely extends package-call resolution to detect immutable local aliases of reflect.DeepEqual without false positives from mutable aliases.
Changes:
- Resolves function identities through aliases, renamed imports, and dot imports.
- Rejects reassigned, escaped, uninitialized, and package-level aliases.
- Adds regression coverage and documents resolution boundaries.
| File | Description |
|---|---|
pkg/linters/internal/astutil/astutil.go |
Adds conservative, type-aware alias resolution. |
pkg/linters/reflect-deepequal-usage/testdata/src/a/a.go |
Tests valid local alias forms. |
pkg/linters/reflect-deepequal-usage/testdata/src/a/import_alias.go |
Tests renamed imports. |
pkg/linters/reflect-deepequal-usage/testdata/src/a/dot_import.go |
Tests dot imports. |
pkg/linters/reflect-deepequal-usage/testdata/src/b/b.go |
Tests unsupported and mutable aliases. |
pkg/linters/README.md |
Documents alias-resolution boundaries. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
🏗️ Design Decision Gate — ADR RequiredThis PR makes significant changes to core business logic (197 new lines in business-logic directories, threshold 100) but does not have a linked Architecture Decision Record (ADR). 📄 Draft ADR committed:
🔎 Decision inferred from the diff
📋 What to do next
Once an ADR is linked in the PR body, this gate will re-run and verify the implementation matches the decision. 📋 Michael Nygard ADR Format ReferenceAn ADR must contain these four sections to be considered complete:
All ADRs are stored in
|
There was a problem hiding this comment.
Skills-Based Review 🧠
Applied /diagnosing-bugs and /codebase-design.
The core alias-resolution fix is solid: it resolves types.Func objects (not just types.Object names), tracks mutation via assignment/short-decl/range/address-escape, and correctly restricts resolution to local (non-package-scope) variables. I traced through all 13 new negative fixtures in b.go (reassignment, conditional reassignment, later reassignment before a later write, uninitialized alias, closure reassignment, address escape, range reassignment, alias chains through a mutated base, shadowing, package-scope alias, method values, unrelated parameter) and the 4 positive fixtures (declared alias, parenthesized alias chain, multiple aliases, closure capture) plus the new dot_import.go and import_alias.go cases — all match the described conservative semantics.
📋 Key Themes & Highlights
Key Themes
- Correctness: root cause of the original bug (alias detection breaking on reassignment) is properly addressed via type-identity tracking and conservative mutation detection, not a narrow symptom patch.
- Test coverage: regression fixtures are unusually thorough for an AST-matching change — this is exactly the kind of coverage
/diagnosing-bugsand/tddlook for. - Minor performance note (left as inline comment):
packageFunctionAliaswalks every file in the package per alias lookup, and alias chains compound that walk per indirection level. Not a correctness issue, just worth considering before this pattern is reused in more linters.
Positive Highlights
- ✅ Switched from string/name-based matching to
types.Func/types.Objectidentity, eliminating a class of false positives/negatives from shadowing or renamed imports. - ✅ Explicit, well-named negative tests for each unsafe case (reassignment, address escape, uninitialized var, package scope) rather than relying on absence of a positive match.
- ✅ Documentation (
README.md, doc comments) updated to match the new, more conservative semantics.
@copilot please address the review comments above.
🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · sonnet50 · 60.8 AIC · ⌖ 14.6 AIC · ⊞ 10.3K
Comment /matt to run again
| seen[obj] = true | ||
| var initializer ast.Expr | ||
| mutable := false | ||
| for _, file := range pass.Files { |
There was a problem hiding this comment.
[/codebase-design] packageFunctionAlias re-walks every file in the package on each call, and resolving an alias chain recurses back through packageFunction into packageFunctionAlias, repeating the full per-package AST walk at each indirection level. This is O(aliases × package size) rather than a single pass, and this helper is shared by multiple linters (jsonmarshalignoredeerror, reflectdeepequalusage), so the cost multiplies across call sites.
💡 Possible mitigation
Consider memoizing per-object alias resolution results (e.g. keyed on pass + types.Object), or scoping the walk to the enclosing function/file instead of all pass.Files, so repeated alias lookups in a hot package don't re-walk the whole file set.
Not a correctness issue — the regression fixtures (reassignment, range, address escape, closures, package scope) look thorough and the detection logic is sound. Just worth noting before this pattern gets copied into more linters.
@copilot please address this.
|
🎉 This pull request is included in a new release. Release: |
Summary
Closes #66436.
Current main already follows function-value initializers and asserts the issue's original alias fixture. That implementation can incorrectly flag aliases after reassignment. This change completes the detection safely:
types.Funcobjects and track aliases using type-object identity, including alias chains, parentheses, renamed imports, and dot imports.Validation
go test ./pkg/linters/reflect-deepequal-usage/... ./pkg/linters/internal/astutil/... ./pkg/linters/jsonmarshalignoredeerror/... ./pkg/linters -count=1passed.make agent-report-progressran impacted tests successfully; its lint job initially encountered another session's golangci-lint lock. Retried the remaining gate withmake agent-report-progress-no-testand a session-localTMPDIR; passed without rerunningtest-unit.make fmtcompleted Go formatting but its repository-wide JavaScript formatter failed. Reverted only its unrelated formatting side effects; scoped validation passed and no JavaScript changes are included.