Skip to content

Support ToStruct variable lookup in enclosing function and package scope#8

Merged
tomoemon merged 2 commits into
mainfrom
feat/tostruct-outer-scope-var
Apr 2, 2026
Merged

Support ToStruct variable lookup in enclosing function and package scope#8
tomoemon merged 2 commits into
mainfrom
feat/tostruct-outer-scope-var

Conversation

@tomoemon

@tomoemon tomoemon commented Apr 2, 2026

Copy link
Copy Markdown
Owner

Summary

  • row.ToStruct(&v) の変数宣言をコールバック外(親関数スコープ、パッケージスコープ)でも解決できるように拡張
  • 変数宣言の検索順序: コールバック → 親関数 → ファイルトップレベル(型定義の検索と同じ戦略)
  • findVarTypefindVarTypeInFile の共通ロジックを findVarTypeInSpecs に抽出
  • エラーメッセージを更新し、検索対象スコープを明示

設計の背景

従来、resolveToStructTags 内の findVarType はコールバック body のみを検索していた。
一方、型定義の解決(extractStructSpannerTags 系)は既にコールバック → 親関数 → ファイルトップレベルの順に検索していた。
この非対称性により、親関数で匿名構造体を宣言してコールバック内で ToStruct に渡すという一般的なパターンが誤検知されていた。

変数宣言の検索にも同じフォールバック戦略を適用することで解消した。

検討した代替案

  • findVarTypeenclosingBody を直接渡して一つの関数内で両方検索する案: 関数のシグネチャ変更が大きく、呼び出し元の resolveToStructTags で段階的にフォールバックする現在のパターンの方が型定義検索と一貫性がある

Test plan

  • go test ./... 全テスト通過
  • go run . lint-scan -no-star testdata/sample.go でエラーが出ないことを確認
  • 親関数スコープの anonymous struct / named type のテストケース追加
  • パッケージスコープの anonymous struct のテストケース追加
  • 型が未定義の変数は引き続きエラーになることを確認(既存テスト TestScan_ToStructUnresolvableVar

🤖 Generated with Claude Code

tomoemon and others added 2 commits April 2, 2026 18:30
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>
@tomoemon
tomoemon merged commit 9fb18b2 into main Apr 2, 2026
2 checks passed
@tomoemon
tomoemon deleted the feat/tostruct-outer-scope-var branch April 2, 2026 09:36
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.

1 participant