fix if var exits for incr
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user