Skip to content

Commit 6c325b5

Browse files
authored
feat: Ignore unused variables starting with _ underscore (#1550)
1 parent b183775 commit 6c325b5

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

docs/releasenotes/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 7.0.0
44

5+
### Features
6+
7+
- Ignore unused variables starting with ``_`` (``${_variable}``) ([issue #1457](https://github.com/MarketSquare/robotframework-robocop/issues/1457)
8+
59
### Fixes
610

711
- Fix ``unused-variable`` and ``variable-overwritten-before-usage`` rules not reporting violations in ``TRY`` blocks ([issue #1548](https://github.com/MarketSquare/robotframework-robocop/issues/1548))

src/robocop/linter/rules/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ def visit_LibraryImport(self, node) -> None: # noqa: N802
13331333
)
13341334

13351335
def clear_variables_after_loop(self) -> None:
1336-
"""Remove used variables after loop finishes."""
1336+
"""Remove used variables after the loop finishes."""
13371337
for index, scope in enumerate(self.variables):
13381338
self.variables[index] = {name: variable for name, variable in scope.items() if not variable.is_used}
13391339

@@ -1474,7 +1474,7 @@ def handle_assign_variable(self, token, ignore_var_conversion: bool = True) -> N
14741474
if ignore_var_conversion:
14751475
name = utils.remove_variable_type_conversion(name)
14761476
normalized = utils.normalize_robot_name(name)
1477-
if not normalized: # i.e. "${_}" -> ""
1477+
if not normalized or name.startswith("_"): # i.e. "${_}" -> "", or ${_ignore}
14781478
return
14791479
arg = self.arguments.get(normalized, None)
14801480
if arg is not None:

src/robocop/linter/rules/variables.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,20 @@ class UnusedVariableRule(Rule):
8383
Log Triangle base points are: ${p1} and ${p2}.
8484
RETURN ${p1} ${p2} # ${p3} is never used
8585
86-
Use ``${_}`` variable name if you purposefully do not use variable:
86+
You can use ``${_}`` variable name or start variable name with ``_`` underscore if you purposefully do not
87+
use variable:
8788
8889
*** Keywords ***
8990
Process Value 10 Times
9091
[Arguments] ${value}
9192
FOR ${_} IN RANGE 10
9293
Process Value ${value}
9394
END
95+
${_first} ${second} Unpack List @{LIST}
9496
95-
Note that some keywords may use your local variables even if you don't pass them directly. For example
96-
BuiltIn ``Replace Variables`` or any custom keyword that retrieves variables from local scope. In such case
97-
Robocop will still raise ``unused-variable`` even if variable is used.
97+
Note that some keywords may use your local variables even if you don't pass them directly. For example,
98+
BuiltIn ``Replace Variables`` or any custom keyword that retrieves variables from a local scope. In this case,
99+
Robocop will still raise an ``unused-variable`` even if the variable is actually used.
98100
99101
"""
100102

tests/linter/rules/variables/unused_variable/test.robot

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,20 @@ Except with AS
255255
EXCEPT Error message* AS ${var2}
256256
Log ${var2}
257257
END
258+
259+
Not used starting with _
260+
[Documentation] Should be ignored.
261+
FOR ${_var} IN 1 2 3
262+
Keyword
263+
END
264+
TRY
265+
May Fail ${var1}
266+
EXCEPT Error message* AS ${_var2}
267+
No Operation
268+
${_branch_assign} Keyword
269+
END
270+
IF condition
271+
${_var3} Keyword
272+
END
273+
${var4} ${_var5} Keyword
274+
Log ${var4}

0 commit comments

Comments
 (0)