Skip to content

Commit

Permalink
Fix unused-variable with section variables not recognized as used (#1050
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bhirsz authored Jan 16, 2024
1 parent c31a136 commit c50b9ec
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 6 deletions.
39 changes: 39 additions & 0 deletions docs/releasenotes/5.0.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
:orphan:

=============
Robocop 5.0.2
=============

Fix for 0920 ``unused-variable`` not recognizing variables from ``*** Variables ***`` section used in
``*** Settings ***`` and keyword arguments default values (#1049).

You can install the latest available version by running

::

pip install --upgrade robotframework-robocop

or to install exactly this version

::

pip install robotframework-robocop==5.0.2

.. contents::
:depth: 2
:local:

Acknowledgements
================

Thanks to the whole community for submitting bug reports and feature requests.
Without you, Robocop wouldn't be in the place where it is now. All the feedback
is essential to drive the tool towards higher quality and better user
experience.

If you want to help us more, consider contributing to the project directly.
We can offer our constant support to make the work fun and effective. We do
our best to create a supportive and welcoming environment for everyone.
Feel free to ping us on our official `#robocop-linter Slack channel`_ anytime.

.. _#robocop-linter Slack channel: https://robotframework.slack.com/archives/C01AWSNKC2H
24 changes: 21 additions & 3 deletions robocop/checkers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,9 +1165,13 @@ def parse_arguments(self, node):
for arg in node.get_tokens(Token.ARGUMENT):
if arg.value[0] in ("@", "&"): # ignore *args and &kwargs
continue
name, *_ = arg.value.split("=", maxsplit=1)
normalized_name = normalize_robot_var_name(name)
self.add_argument(name, normalized_name, token=arg)
if "=" in arg.value:
arg_name, default_value = arg.value.split("=", maxsplit=1)
self.find_not_nested_variable(default_value, is_var=False)
else:
arg_name = arg.value
normalized_name = normalize_robot_var_name(arg_name)
self.add_argument(arg_name, normalized_name, token=arg)

def parse_embedded_arguments(self, name_token):
"""Store embedded arguments from keyword name. Ignore embedded variables patterns (${var:pattern})."""
Expand All @@ -1194,6 +1198,20 @@ def visit_If(self, node): # noqa
for token in node.header.get_tokens(Token.ASSIGN):
self.handle_assign_variable(token)

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)

visit_SuiteSetup = (
visit_SuiteTeardown
) = visit_TestSetup = visit_TestTeardown = visit_ResourceImport = visit_VariablesImport = visit_LibraryImport

def visit_DefaultTags(self, node): # noqa
for token in node.get_tokens(Token.ARGUMENT):
self.find_not_nested_variable(token.value, is_var=False)

visit_TestTags = visit_ForceTags = visit_Metadata = visit_DefaultTags

def clear_variables_after_loop(self):
"""Remove used variables after loop finishes."""
for index, scope in enumerate(self.variables):
Expand Down
2 changes: 1 addition & 1 deletion robocop/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "5.0.1"
__version__ = "5.0.2"
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ test.robot:115:5:115:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:118:5:118:14 [I] 0920 Variable '${assign}' is assigned but not used
test.robot:121:12:121:23 [I] 0920 Variable '${not_used}' is assigned but not used
test.robot:129:12:129:23 [I] 0920 Variable '${variable}' is assigned but not used
unused_section_vars.robot:7:1:7:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' 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 @@ -6,4 +6,4 @@ test.robot:41:12:41:18 [I] 0920 Variable '${var}' is assigned but not used
test.robot:67:12:67:23 [I] 0920 Variable '${category}' is assigned but not used
test.robot:115:5:115:11 [I] 0920 Variable '${var}' is assigned but not used
test.robot:118:5:118:14 [I] 0920 Variable '${assign}' is assigned but not used
unused_section_vars.robot:7:1:7:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' is assigned but not used
unused_section_vars.robot:21:1:21:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' is assigned but not used
36 changes: 36 additions & 0 deletions tests/atest/rules/misc/unused_variable/unused_section_vars.robot
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
*** Settings ***
Documentation Global variables in test/task files cannot be imported and should be used within file.
Library TestLibrary value with ${USED_IN_LIB_ARG} ${USED_IN_LIB_ARG2}
Library ${TEST_LIBRARY}
Resource path/to/${GLOBAL_PATH}/file.resource
Variables ${VARIABLES_FILE}
Variables variables.py @{GLOBAL_VARS_ARGS}

Suite Setup ${SETUP_KW}
Suite Teardown Suite Teardown ${SUITE_TEARDOWN_ARG}
Test Setup Test Setup value with ${TEST_SETUP_ARG}
Test Teardown Test Teardown value with ${TEST_SETUP_ARG}

Metadata Key ${METADATA_VALUE}

Default Tags @{DEFAULT_TAGS}


*** Variables ***
${GLOBAL_USED} value
${GLOBAL_NOT_USED} value
${GLOBAL_USED_IN_SECTION} value
${GLOBAL_USED2} value with ${GLOBAL_USED_IN_SECTION}
${USED_IN_ARG_DEFAULT} value
${USED_IN_ARG_DEFAULT2} value
${USED_IN_LIB_ARG} value
${USED_IN_LIB_ARG2} value
${TEST_LIBRARY} value
${GLOBAL_PATH} resources
${VARIABLES_FILE} variables.yaml
@{GLOBAL_VARS_ARGS} arg1 arg2
@{DEFAULT_TAGS} tag1 tag2
${SETUP_KW} Setup Keyword
${SUITE_TEARDOWN_ARG} value
${TEST_SETUP_ARG} value
${METADATA_VALUE} value
@{TEMPLATE_ARGS} arg1 arg2


*** Test Cases ****
Expand All @@ -15,8 +43,16 @@ Test
Log ${GLOBAL_USED}
Keyword

Test with template
[Template] Template
@{TEMPLATE_ARGS}


*** Keywords ***
Keyword
[Documentation] Use second global variable.
Log ${GLOBAL_USED2}

Keyword With Arguments
[Arguments] ${arg}=${USED_IN_ARG_DEFAULT} ${arg2}=value with ${USED_IN_ARG_DEFAULT2}
No Operation

0 comments on commit c50b9ec

Please sign in to comment.