Repository: MarketSquare/robotframework-robocop
Pull request #1038 (2023-12-30 16:39:39)

Add support for VAR in empty-variable rule
Relates #973 

One syntax but requires so many updates to rules :)

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/MarketSquare/robotframework-robocop/pull/1038?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare) Report
Attention: `8 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`1a2b3be`)](https://app.codecov.io/gh/MarketSquare/robotframework-robocop/commit/1a2b3be4a091fa91498b800c7f11d3faa67be37d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare) 96.97% compared to head [(`1e1079c`)](https://app.codecov.io/gh/MarketSquare/robotframework-robocop/pull/1038?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare) 95.85%.

| [Files](https://app.codecov.io/gh/MarketSquare/robotframework-robocop/pull/1038?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare) | Patch % | Lines |
|---|---|---|
| [robocop/checkers/misc.py](https://app.codecov.io/gh/MarketSquare/robotframework-robocop/pull/1038?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare#diff-cm9ib2NvcC9jaGVja2Vycy9taXNjLnB5) | 11.11% | [8 Missing :warning: ](https://app.codecov.io/gh/MarketSquare/robotframework-robocop/pull/1038?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #1038      +/-   ##
==========================================
- Coverage   96.97%   95.85%   -1.12%     
==========================================
  Files          37       37              
  Lines        4626     4635       +9     
==========================================
- Hits         4486     4443      -43     
- Misses        140      192      +52     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/MarketSquare/robotframework-robocop/pull/1038?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=MarketSquare).


Patch:
diff --git a/docs/releasenotes/unreleased/rules.1.rst b/docs/releasenotes/unreleased/rules.1.rst
index 91a92ed..d763245 100644
--- a/docs/releasenotes/unreleased/rules.1.rst
+++ b/docs/releasenotes/unreleased/rules.1.rst
@@ -13,6 +13,7 @@ Following rules now support ``VAR`` syntax:
 - W0503 ``too-many-calls-in-keyword``
 - W0504 ``too-long-test-case``
 - W0505 ``too-many-calls-in-test-case``
+- I0912 ``empty-variable``
 - W0919 ``unused-argument``
 - I0920 ``unused-variable``
 - W0921 ``argument-overwritten-before-usage``
diff --git a/robocop/checkers/misc.py b/robocop/checkers/misc.py
index 9b25ee0..0bbc23c 100644
--- a/robocop/checkers/misc.py
+++ b/robocop/checkers/misc.py
@@ -849,6 +849,29 @@ class EmptyVariableChecker(VisitorChecker):
                     end_col=token.end_col_offset + 1,
                 )
 
+    def visit_Var(self, node):  # noqa
+        if node.errors:
+            return
+        if not node.value:  # catch variable declaration without any value
+            first_data = node.data_tokens[0]
+            self.report(
+                "empty-variable",
+                var_type=node.name[0],
+                node=first_data,
+                col=first_data.col_offset + 1,
+                end_col=first_data.end_col_offset + 1,
+            )
+        for token in node.get_tokens(Token.ARGUMENT):
+            if not token.value or token.value == "\\":
+                self.report(
+                    "empty-variable",
+                    var_type="$",
+                    node=token,
+                    lineno=token.lineno,
+                    col=token.col_offset + 1,
+                    end_col=token.end_col_offset + 1,
+                )
+
 
 class ResourceFileChecker(VisitorChecker):
     """Checker for resource files."""


Test patch:
diff --git a/tests/atest/rules/misc/empty_variable/expected_output.txt b/tests/atest/rules/misc/empty_variable/expected_output.txt
index 9315d07..e62c4be 100644
--- a/tests/atest/rules/misc/empty_variable/expected_output.txt
+++ b/tests/atest/rules/misc/empty_variable/expected_output.txt
@@ -12,3 +12,7 @@ test.robot:36:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving varia
 test.robot:37:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
 test.robot:38:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
 test.robot:39:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:51:5 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:53:9 [I] 0912 Use built-in variable @{EMPTY} instead of leaving variable without value or using backslash
+test.robot:54:9 [I] 0912 Use built-in variable &{EMPTY} instead of leaving variable without value or using backslash
+test.robot:62:8 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
\ No newline at end of file
diff --git a/tests/atest/rules/misc/empty_variable/expected_output_pre_var.txt b/tests/atest/rules/misc/empty_variable/expected_output_pre_var.txt
new file mode 100644
index 0000000..9315d07
--- /dev/null
+++ b/tests/atest/rules/misc/empty_variable/expected_output_pre_var.txt
@@ -0,0 +1,14 @@
+test.robot:3:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:4:1 [I] 0912 Use built-in variable @{EMPTY} instead of leaving variable without value or using backslash
+test.robot:5:1 [I] 0912 Use built-in variable &{EMPTY} instead of leaving variable without value or using backslash
+test.robot:7:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:20:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:22:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:25:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:30:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:33:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:35:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:36:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:37:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:38:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
+test.robot:39:1 [I] 0912 Use built-in variable ${EMPTY} instead of leaving variable without value or using backslash
diff --git a/tests/atest/rules/misc/empty_variable/test.robot b/tests/atest/rules/misc/empty_variable/test.robot
index 851e8ea..5ce0160 100644
--- a/tests/atest/rules/misc/empty_variable/test.robot
+++ b/tests/atest/rules/misc/empty_variable/test.robot
@@ -43,3 +43,23 @@ ${EMPTY_WITH_BACKSLASH}  \
 ... invalid
 {also_invalid}  2
 ...
+
+
+*** Keywords ***
+VAR Syntax
+    VAR    ${variable}    value
+    VAR    ${variable}
+    IF    ${variable}
+        VAR    @{variable}
+        VAR    &{variable}    scope=GLOBAL
+    END
+    VAR    $variable  # error
+    VAR
+    # VAR FIXME - uncomment after RF fix error (reported at https://github.com/robotframework/robotframework/issues/4995)
+    # ...
+    VAR    @{list}
+    ...    value
+    ...
+    ...    scope=SUITE
+    VAR
+    ...    scope=local
diff --git a/tests/atest/rules/misc/empty_variable/test_rule.py b/tests/atest/rules/misc/empty_variable/test_rule.py
index 6f32193..d08cc26 100644
--- a/tests/atest/rules/misc/empty_variable/test_rule.py
+++ b/tests/atest/rules/misc/empty_variable/test_rule.py
@@ -3,4 +3,7 @@ from tests.atest.utils import RuleAcceptance
 
 class TestRuleAcceptance(RuleAcceptance):
     def test_rule(self):
-        self.check_rule(src_files=["test.robot"], expected_file="expected_output.txt")
+        self.check_rule(src_files=["test.robot"], expected_file="expected_output.txt", target_version=">=7")
+
+    def test_rule_pre_var(self):
+        self.check_rule(src_files=["test.robot"], expected_file="expected_output_pre_var.txt", target_version="<7")