Skip to content

Commit

Permalink
Fix not used variables not detected in IF (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz authored Jul 31, 2024
1 parent 50128e5 commit 59b6c73
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
11 changes: 11 additions & 0 deletions docs/releasenotes/unreleased/fixes.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
unused-variable not detected within IF block (#1093)
----------------------------------------------------

If the variable was defined in the IF block, I0920 ``unused-variable`` was not reported even if variable was not used
anywhere::

*** Test Cases ***
Useless variable definition
IF True
${not_used} Keyword Call
END
18 changes: 17 additions & 1 deletion robocop/checkers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,12 +1343,28 @@ def visit_If(self, node): # noqa
self.variables.append({})
for item in node.body:
self.visit(item)
self.variables.pop()
self.add_variables_from_if_to_scope()
if node.orelse:
self.visit(node.orelse)
for token in node.header.get_tokens(Token.ASSIGN):
self.handle_assign_variable(token)

def add_variables_from_if_to_scope(self):
"""
Add all variables in given IF branch to common scope. If variable is used already in the branch, if it will
also be mark as used.
"""
variables = self.variables.pop()
if not self.variables:
self.variables.append(variables)
return
for var_name, cached_var in variables.items():
if var_name in self.variables[-1]:
if cached_var.is_used:
self.variables[-1][var_name].is_used = True
else:
self.variables[-1][var_name] = cached_var

def visit_LibraryImport(self, node): # noqa
for token in node.get_tokens(Token.NAME, Token.ARGUMENT):
self.find_not_nested_variable(token.value, is_var=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ test.robot:15:5:15:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:19:15:19:22 [I] 0920 Variable '${var2}' is assigned but not used
test.robot:23:5:23:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:31:5:31:14 [I] 0920 Variable '${assign}' is assigned but not used
test.robot:42:12:42:18 [I] 0920 Variable '${var}' is assigned but not used
test.robot:68:12:68:23 [I] 0920 Variable '${category}' is assigned but not used
test.robot:116:5:116:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:119:5:119:14 [I] 0920 Variable '${assign}' is assigned but not used
test.robot:122:12:122:23 [I] 0920 Variable '${not_used}' is assigned but not used
test.robot:130:12:130:23 [I] 0920 Variable '${variable}' is assigned but not used
test.robot:39:9:39:20 [I] 0920 Variable '${not_used}' is assigned but not used
test.robot:44:9:44:32 [I] 0920 Variable '${not_used_from_branch}' is assigned but not used
test.robot:52:13:52:30 [I] 0920 Variable '${nested_define3}' is assigned but not used
test.robot:66:12:66:18 [I] 0920 Variable '${var}' is assigned but not used
test.robot:92:12:92:23 [I] 0920 Variable '${category}' is assigned but not used
test.robot:140:5:140:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:143:5:143:14 [I] 0920 Variable '${assign}' is assigned but not used
test.robot:146:12:146:23 [I] 0920 Variable '${not_used}' is assigned but not used
test.robot:154:12:154:23 [I] 0920 Variable '${variable}' is assigned but not used
unused_section_vars.robot:21:1:21:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' is assigned but not used
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ test.robot:15:5:15:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:19:15:19:22 [I] 0920 Variable '${var2}' is assigned but not used
test.robot:23:5:23:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:31:5:31:14 [I] 0920 Variable '${assign}' is assigned but not used
test.robot:42:12:42:18 [I] 0920 Variable '${var}' is assigned but not used
test.robot:68:12:68:23 [I] 0920 Variable '${category}' is assigned but not used
test.robot:116:5:116:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:119:5:119:14 [I] 0920 Variable '${assign}' is assigned but not used
test.robot:39:9:39:20 [I] 0920 Variable '${not_used}' is assigned but not used
test.robot:44:9:44:32 [I] 0920 Variable '${not_used_from_branch}' is assigned but not used
test.robot:52:13:52:30 [I] 0920 Variable '${nested_define3}' is assigned but not used
test.robot:66:12:66:18 [I] 0920 Variable '${var}' is assigned but not used
test.robot:92:12:92:23 [I] 0920 Variable '${category}' is assigned but not used
test.robot:140:5:140:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:143:5:143:14 [I] 0920 Variable '${assign}' is assigned but not used
unused_section_vars.robot:21:1:21:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' is assigned but not used
12 changes: 12 additions & 0 deletions tests/atest/rules/misc/unused_variable/expected_output_rf3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test.robot:15:5:15:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:19:15:19:22 [I] 0920 Variable '${var2}' is assigned but not used
test.robot:23:5:23:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:31:5:31:14 [I] 0920 Variable '${assign}' is assigned but not used
test.robot:39:9:39:20 [I] 0920 Variable '${not_used}' is assigned but not used
test.robot:44:9:44:32 [I] 0920 Variable '${not_used_from_branch}' is assigned but not used
test.robot:56:13:56:30 [I] 0920 Variable '${nested_define3}' is assigned but not used
test.robot:66:12:66:18 [I] 0920 Variable '${var}' is assigned but not used
test.robot:92:12:92:23 [I] 0920 Variable '${category}' is assigned but not used
test.robot:140:5:140:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:143:5:143:14 [I] 0920 Variable '${assign}' is assigned but not used
unused_section_vars.robot:21:1:21:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' is assigned but not used
24 changes: 24 additions & 0 deletions tests/atest/rules/misc/unused_variable/test.robot
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ Used In IF
IF $var
Keyword String with ${var2}
END
${used_in_if} Keyword
IF True
${not_used} Keyword
# TODO even if branch used it, it should be mark as unused. Could be achieved by making
# add_variables_from_if_to_scope temporarily saving popped variables
${used_in_branch} Keyword
ELSE IF False
${not_used_from_branch} Keyword ${used_in_branch}
ELSE
Keyword ${used_in_if}
END
IF nested
IF nested-loop
${nested_define} Keyword
${nested_define2} Keyword
${nested_define3} Keyword
ELSE
${nested_define} Keyword
${nested_define2} Keyword
${nested_define3} Keyword
END
Keyword ${nested_define}
END
Keyword ${nested_define2}

Not Used From FOR
FOR ${var} IN 1 2 3
Expand Down
10 changes: 9 additions & 1 deletion tests/atest/rules/misc/unused_variable/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ def test_rule_pre_var(self):
src_files=["test.robot", "unused_section_vars.robot"],
expected_file="expected_output_pre_var.txt",
issue_format="end_col",
target_version="<7",
target_version=">=4;<7",
)

def test_rule_rf3(self):
self.check_rule(
src_files=["test.robot", "unused_section_vars.robot"],
expected_file="expected_output_rf3.txt",
issue_format="end_col",
target_version="==3.*",
)

def test_rule_on_loops(self):
Expand Down

0 comments on commit 59b6c73

Please sign in to comment.