Skip to content

Commit

Permalink
Fix false positive in unused-output-variable (#1398)
Browse files Browse the repository at this point in the history
Comprehension term vars are never unused, and should not be
flagged.

Fixes #1353

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored Feb 6, 2025
1 parent 48c9e5e commit a4e3592
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ report contains violation if {
not ast.var_in_head(input.rules[to_number(rule_index)].head, var.value)
not ast.var_in_call(ast.function_calls, rule_index, var.value)
not _ref_base_vars[rule_index][var.value]
not _comprehension_term_vars[rule_index][var.value]

# this is by far the most expensive condition to check, so only do
# so when all other conditions apply
Expand All @@ -52,3 +53,12 @@ _ref_base_vars[rule_index][term.value] contains term if {

not ast.is_wildcard(term)
}

_comprehension_term_vars[rule_index] contains var.value if {
some rule_index, comprehensions in ast.found.comprehensions
some comprehension in comprehensions

only_head := object.remove(comprehension.value, ["body"])

some var in ast.find_term_vars(only_head)
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,24 @@ test_success_not_output_variable_argument if {
r := rule.report with input as module
r == set()
}

test_success_not_unused_comprehension_term if {
module := ast.with_rego_v1(`success if {x | input[x]}`)

r := rule.report with input as module
r == set()
}

test_success_not_unused_comprehension_term_complex if {
module := ast.with_rego_v1(`success if {[x, y] | input[x][y]}`)

r := rule.report with input as module
r == set()
}

test_success_not_unused_comprehension_term_key_value if {
module := ast.with_rego_v1(`success if {x: y | input[x][y]}`)

r := rule.report with input as module
r == set()
}

0 comments on commit a4e3592

Please # to comment.