Skip to content

Commit 2eab703

Browse files
authored
Have redundant-existence-check flag subset terms (#1514)
Fixes #1512 Signed-off-by: Anders Eknert <anders@styra.com>
1 parent 5d69c12 commit 2eab703

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

bundle/regal/ast/ast.rego

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ _exclude_arg("assign", 0, _)
227227

228228
# METADATA
229229
# description: |
230-
# true if both ref values (or "paths") are equal in type and value for each path component, ignoring locations
230+
# true if both ref values (or "paths") are equal in type
231+
# and value for each path component, ignoring locations
231232
ref_value_equal(v1, v2) if {
232233
count(v1) == count(v2)
233234

@@ -237,6 +238,19 @@ ref_value_equal(v1, v2) if {
237238
}
238239
}
239240

241+
# METADATA
242+
# description: |
243+
# true if all terms in `terms1` are also present in `terms2`
244+
# regardless of length, and ignoring locations
245+
is_terms_subset(terms1, terms2) if {
246+
count(terms1) <= count(terms2)
247+
248+
every i, term in terms1 {
249+
term.type == terms2[i].type
250+
term.value == terms2[i].value
251+
}
252+
}
253+
240254
# METADATA
241255
# description: returns the "path" string of any given ref value
242256
ref_to_string(ref) := ref[0].value if {

bundle/regal/rules/bugs/redundant-existence-check/redundant_existence_check.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ report contains violation if {
1919

2020
some term in rule.body[expr_index + 1].terms
2121

22-
ast.ref_value_equal(expr.terms.value, term.value)
22+
ast.is_terms_subset(expr.terms.value, term.value)
2323

2424
violation := result.fail(rego.metadata.chain(), result.ranged_from_ref(expr.terms.value))
2525
}
@@ -55,7 +55,7 @@ report contains violation if {
5555
some expr in _exprs[rule_index]
5656

5757
expr.terms.type == "ref"
58-
ast.ref_value_equal(expr.terms.value, rule.head.value.value)
58+
ast.is_terms_subset(expr.terms.value, rule.head.value.value)
5959

6060
violation := result.fail(rego.metadata.chain(), result.ranged_from_ref(expr.terms.value))
6161
}

bundle/regal/rules/bugs/redundant-existence-check/redundant_existence_check_test.rego

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ test_fail_redundant_existence_check if {
1212
startswith(input.foo, "bar")
1313
}`)
1414
r := rule.report with input as module
15+
16+
r == {{
17+
"category": "bugs",
18+
"description": "Redundant existence check",
19+
"level": "error",
20+
"location": {"col": 3, "file": "policy.rego", "row": 7, "text": "\t\tinput.foo", "end": {"col": 12, "row": 7}},
21+
"related_resources": [{
22+
"description": "documentation",
23+
"ref": config.docs.resolve_url("$baseUrl/$category/redundant-existence-check", "bugs"),
24+
}],
25+
"title": "redundant-existence-check",
26+
}}
27+
}
28+
29+
test_fail_redundant_existence_check_subset if {
30+
module := ast.with_rego_v1(`
31+
redundant if {
32+
input.foo
33+
startswith(input.foo.bar, "bar")
34+
}`)
35+
r := rule.report with input as module
36+
1537
r == {{
1638
"category": "bugs",
1739
"description": "Redundant existence check",
@@ -43,6 +65,7 @@ test_success_not_redundant_existence_check_with_cancels if {
4365
rule.foo == 1
4466
}`)
4567
r := rule.report with input as module
68+
4669
r == set()
4770
}
4871

@@ -52,6 +75,7 @@ test_fail_redundant_existence_check_head_assignment_of_ref if {
5275
input.foo
5376
}`)
5477
r := rule.report with input as module
78+
5579
r == {{
5680
"category": "bugs",
5781
"description": "Redundant existence check",
@@ -71,6 +95,7 @@ test_fail_redundant_existence_check_function_arg if {
7195
foo
7296
}`)
7397
r := rule.report with input as module
98+
7499
r == {{
75100
"category": "bugs",
76101
"description": "Redundant existence check",

0 commit comments

Comments
 (0)