fix foreach var

This commit is contained in:
Christoph Brandau
2025-08-13 09:26:22 +02:00
parent db8f849a06
commit 5b2bf39cba
+16
View File
@@ -120,6 +120,22 @@ class UndeclaredVariableCheck(Visitor):
base = text.split("(", 1)[0] # Handle array syntax
if not base.startswith("::"):
locals_set.add(base)
elif name == "foreach" and len(node.args) >= 3:
# foreach varlist1 list1 ?varlist2 list2 ...? body
# Treat loop variables as locals within the proc
# Iterate pairs (varlist, list) over all but the last arg (body)
pair_args = node.args[:-1]
i = 0
while i + 1 < len(pair_args):
varnode = pair_args[i]
text = _word_text(varnode) or ""
# Split var list by whitespace if braced list, else single name
names = text.split()
for nm in names:
base = nm.split("(", 1)[0]
if base and not base.startswith("::"):
locals_set.add(base)
i += 2
# Recurse
for ch in getattr(node, "children", []):
_collect(ch)