fix if var exits for incr

This commit is contained in:
Christoph Brandau
2025-08-13 12:45:46 +02:00
parent 781b240128
commit 4a94f1aea8
2 changed files with 26 additions and 3 deletions
+11 -2
View File
@@ -144,16 +144,25 @@ class UndeclaredVariableCheck(Visitor):
# Second pass: flag VarSub usages that are not accounted for
def _scan(node):
# 1) Variable substitutions like $var
if isinstance(node, VarSub):
var_name = node.value or ""
# Strip leading namespace separator for comparison; treat only leading :: as global
if var_name.startswith("::"):
return
base = var_name
# VarSub.value does not include index; indices are separate children
if base not in locals_set and base not in declared_globals:
msg = f"Variable '{base}' used in proc is not set locally and not declared global; use 'global {base}' or '$::{base}'"
self._violations.append(Violation(Rules.UNDECLARED_VARIABLE, msg, node.pos, node.end_pos))
# 2) Commands that take a variable name as first argument, e.g., 'incr var [amount]'
elif isinstance(node, Command) and hasattr(node.routine, "contents"):
if node.routine.contents == "incr" and node.args:
first_arg = node.args[0]
text = _word_text(first_arg) or ""
base = text.split("(", 1)[0]
if base and not base.startswith("::"):
if base not in locals_set and base not in declared_globals:
msg = f"Variable '{base}' used in proc is not set locally and not declared global; use 'global {base}'"
self._violations.append(Violation(Rules.UNDECLARED_VARIABLE, msg, first_arg.pos, first_arg.end_pos))
for ch in getattr(node, "children", []):
_scan(ch)