Skip to content

Commit d3a77ce

Browse files
authoredJul 18, 2024··
Ensure unused-output-variable actually is output variable (#925)
Found when testing this against some policies in the wild Signed-off-by: Anders Eknert <anders@styra.com>
1 parent 3abd5c0 commit d3a77ce

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ in the near future:
703703

704704
- [ ] Make "Check on save" unnecessary by allowing diagnostics to include
705705
[compilation errors](https://github.com/StyraInc/regal/issues/745)
706-
- [ ] Add Code Lens to "Evaluate" any rule or package
706+
- [ ] Add Code Lens to "Evaluate" any rule or package (VS Code only, initially)
707707
- [ ] Implement [Signature Help](https://github.com/StyraInc/regal/issues/695) feature
708708

709709
The roadmap is updated when all the current items have been completed.

‎bundle/regal/rules/bugs/unused_output_variable.rego

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ report contains violation if {
3333
not _var_in_head(input.rules[to_number(rule_index)].head, var.value)
3434
not _var_in_call(ast.function_calls, rule_index, var.value)
3535

36+
# this is by far the most expensive condition to check, so only do
37+
# so when all other conditions apply
38+
ast.is_output_var(input.rules[to_number(rule_index)], var, var.location)
39+
3640
violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(var))
3741
}
3842

‎bundle/regal/rules/bugs/unused_output_variable_test.rego

+24
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,27 @@ test_success_not_unused_output_variable_other_ref if {
107107
r := rule.report with input as module
108108
r == set()
109109
}
110+
111+
test_success_not_output_variable_rule if {
112+
module := ast.with_rego_v1(`
113+
x := 1
114+
115+
success := x if {
116+
input[x]
117+
}
118+
`)
119+
120+
r := rule.report with input as module
121+
r == set()
122+
}
123+
124+
test_success_not_output_variable_argument if {
125+
module := ast.with_rego_v1(`
126+
success(x) if {
127+
input[x]
128+
}
129+
`)
130+
131+
r := rule.report with input as module
132+
r == set()
133+
}

0 commit comments

Comments
 (0)
Please sign in to comment.