Skip to content

Commit 7bc7868

Browse files
authoredJul 30, 2024··
Extend redundant-existence-check to fail redundant ref checks (#949)
Also add end location to this rule's report Fixes #935 Signed-off-by: Anders Eknert <anders@styra.com>
1 parent f2b1029 commit 7bc7868

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed
 

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import rego.v1
77
import data.regal.ast
88
import data.regal.result
99

10+
# METADATA
11+
# description: check rule bodies for redundant existence checks
1012
report contains violation if {
1113
some rule_index, rule in input.rules
1214
some expr_index, expr in ast.exprs[rule_index]
@@ -18,12 +20,28 @@ report contains violation if {
1820
ast.static_ref(expr.terms)
1921

2022
ref_str := ast.ref_to_string(expr.terms.value)
21-
2223
next_expr := rule.body[expr_index + 1]
2324

2425
some term in next_expr.terms
2526

2627
ast.ref_to_string(term.value) == ref_str
2728

28-
violation := result.fail(rego.metadata.chain(), result.location(expr))
29+
violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(expr))
30+
}
31+
32+
# METADATA
33+
# description: check for redundant existence checks in rule head assignment
34+
report contains violation if {
35+
some rule_index, rule in input.rules
36+
37+
rule.head.value.type == "ref"
38+
39+
ref_str := ast.ref_to_string(rule.head.value.value)
40+
41+
some expr in ast.exprs[rule_index]
42+
43+
expr.terms.type == "ref"
44+
ast.ref_to_string(expr.terms.value) == ref_str
45+
46+
violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(expr.terms))
2947
}

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test_fail_redundant_existence_check if {
1818
"category": "bugs",
1919
"description": "Redundant existence check",
2020
"level": "error",
21-
"location": {"col": 3, "file": "policy.rego", "row": 7, "text": "\t\tinput.foo"},
21+
"location": {"col": 3, "file": "policy.rego", "row": 7, "text": "\t\tinput.foo", "end": {"col": 12, "row": 7}},
2222
"related_resources": [{
2323
"description": "documentation",
2424
"ref": config.docs.resolve_url("$baseUrl/$category/redundant-existence-check", "bugs"),
@@ -47,3 +47,22 @@ test_success_not_redundant_existence_check_with_cancels if {
4747
r := rule.report with input as module
4848
r == set()
4949
}
50+
51+
test_fail_redundant_existence_check_head_assignment_of_ref if {
52+
module := ast.with_rego_v1(`
53+
redundant := input.foo if {
54+
input.foo
55+
}`)
56+
r := rule.report with input as module
57+
r == {{
58+
"category": "bugs",
59+
"description": "Redundant existence check",
60+
"level": "error",
61+
"location": {"col": 3, "file": "policy.rego", "row": 7, "text": "\t\tinput.foo", "end": {"col": 12, "row": 7}},
62+
"related_resources": [{
63+
"description": "documentation",
64+
"ref": config.docs.resolve_url("$baseUrl/$category/redundant-existence-check", "bugs"),
65+
}],
66+
"title": "redundant-existence-check",
67+
}}
68+
}

0 commit comments

Comments
 (0)
Please sign in to comment.