From dfad4721de25d50c6dc2a9e65d072aecc05ff7f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Hirsz?= Date: Thu, 21 Mar 2024 16:46:03 +0100 Subject: [PATCH] fix: unused-variable with variables in [Setup], [Teardown] and [Timeout] (#1063) --- docs/releasenotes/unreleased/fixes.1.rst | 5 +++++ robocop/checkers/misc.py | 8 +++++++- .../expected_output_after_var.txt | 20 +++++++++---------- .../expected_output_pre_var.txt | 16 +++++++-------- .../rules/misc/unused_variable/test.robot | 5 +++++ 5 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 docs/releasenotes/unreleased/fixes.1.rst diff --git a/docs/releasenotes/unreleased/fixes.1.rst b/docs/releasenotes/unreleased/fixes.1.rst new file mode 100644 index 000000000..be2a28df6 --- /dev/null +++ b/docs/releasenotes/unreleased/fixes.1.rst @@ -0,0 +1,5 @@ +unused-variable when the variable is used in a Test Setup (#1062) +------------------------------------------------------------------ + +I0920 ``unused-variable`` was incorrectly reported if the variable was declared in the ``*** Variables ***`` section +and used in ``[Setup]``, ``[Teardown]`` or ``[Timeout]``. diff --git a/robocop/checkers/misc.py b/robocop/checkers/misc.py index ad674584b..8056d0475 100644 --- a/robocop/checkers/misc.py +++ b/robocop/checkers/misc.py @@ -1204,7 +1204,13 @@ def visit_LibraryImport(self, node): # noqa visit_SuiteSetup = ( visit_SuiteTeardown - ) = visit_TestSetup = visit_TestTeardown = visit_ResourceImport = visit_VariablesImport = visit_LibraryImport + ) = ( + visit_TestSetup + ) = ( + visit_TestTeardown + ) = ( + visit_Setup + ) = visit_Teardown = visit_Timeout = visit_ResourceImport = visit_VariablesImport = visit_LibraryImport def visit_DefaultTags(self, node): # noqa for token in node.get_tokens(Token.ARGUMENT): diff --git a/tests/atest/rules/misc/unused_variable/expected_output_after_var.txt b/tests/atest/rules/misc/unused_variable/expected_output_after_var.txt index a6ea226b0..9c360f399 100644 --- a/tests/atest/rules/misc/unused_variable/expected_output_after_var.txt +++ b/tests/atest/rules/misc/unused_variable/expected_output_after_var.txt @@ -1,11 +1,11 @@ -test.robot:14:5:14:11 [I] 0920 Variable '${var}' is assigned but not used -test.robot:18:15:18:22 [I] 0920 Variable '${var2}' is assigned but not used -test.robot:22:5:22:11 [I] 0920 Variable '${var}' is assigned but not used -test.robot:30:5:30:14 [I] 0920 Variable '${assign}' is assigned but not used -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 -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 +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 unused_section_vars.robot:21:1:21:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' is assigned but not used \ No newline at end of file diff --git a/tests/atest/rules/misc/unused_variable/expected_output_pre_var.txt b/tests/atest/rules/misc/unused_variable/expected_output_pre_var.txt index 57a75cd4c..9a08f1576 100644 --- a/tests/atest/rules/misc/unused_variable/expected_output_pre_var.txt +++ b/tests/atest/rules/misc/unused_variable/expected_output_pre_var.txt @@ -1,9 +1,9 @@ -test.robot:14:5:14:11 [I] 0920 Variable '${var}' is assigned but not used -test.robot:18:15:18:22 [I] 0920 Variable '${var2}' is assigned but not used -test.robot:22:5:22:11 [I] 0920 Variable '${var}' is assigned but not used -test.robot:30:5:30:14 [I] 0920 Variable '${assign}' is assigned but not used -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 +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 unused_section_vars.robot:21:1:21:19 [I] 0920 Variable '${GLOBAL_NOT_USED}' is assigned but not used \ No newline at end of file diff --git a/tests/atest/rules/misc/unused_variable/test.robot b/tests/atest/rules/misc/unused_variable/test.robot index 28cd3a2fe..cb1b1165f 100644 --- a/tests/atest/rules/misc/unused_variable/test.robot +++ b/tests/atest/rules/misc/unused_variable/test.robot @@ -1,5 +1,6 @@ *** Variables *** ${VARIABLE} value +${USED_IN_SETUP} value *** Keywords *** @@ -128,3 +129,7 @@ Unused With VAR Keyword Call ${used_without_sign} VAR ${variable} # missing value VAR $variable # ignored for invalid variable name + +Unused In Setup + [Setup] Keyword Setup ${used_in_setup} + Step