Support ToStruct variable lookup in enclosing function and package scope#8
Merged
Conversation
Previously, resolveToStructTags only searched the callback body for variable declarations. This caused false positives when the variable was declared in the enclosing function or at package level — a common pattern with anonymous structs. Extend the lookup chain: callback body → enclosing function → file-level. Extract shared spec-checking logic into findVarTypeInSpecs to avoid duplication between findVarType and findVarTypeInFile. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
row.ToStruct(&v)の変数宣言をコールバック外(親関数スコープ、パッケージスコープ)でも解決できるように拡張findVarTypeとfindVarTypeInFileの共通ロジックをfindVarTypeInSpecsに抽出設計の背景
従来、
resolveToStructTags内のfindVarTypeはコールバック body のみを検索していた。一方、型定義の解決(
extractStructSpannerTags系)は既にコールバック → 親関数 → ファイルトップレベルの順に検索していた。この非対称性により、親関数で匿名構造体を宣言してコールバック内で
ToStructに渡すという一般的なパターンが誤検知されていた。変数宣言の検索にも同じフォールバック戦略を適用することで解消した。
検討した代替案
findVarTypeにenclosingBodyを直接渡して一つの関数内で両方検索する案: 関数のシグネチャ変更が大きく、呼び出し元のresolveToStructTagsで段階的にフォールバックする現在のパターンの方が型定義検索と一貫性があるTest plan
go test ./...全テスト通過go run . lint-scan -no-star testdata/sample.goでエラーが出ないことを確認TestScan_ToStructUnresolvableVar)🤖 Generated with Claude Code