Skip to content

Commit

Permalink
Fix false positive in pointless-reassignment (#1286)
Browse files Browse the repository at this point in the history
That would happen despite the left hand side not being a var

Fixes #1285

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored Dec 4, 2024
1 parent 0a88ab3 commit c0ac18f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ report contains violation if {

not expr["with"]

ast.assignment_terms(expr)[1].type == "var"
[lhs, rhs] := ast.assignment_terms(expr)

lhs.type == "var"
rhs.type == "var"

violation := result.fail(rego.metadata.chain(), result.infix_expr_location(expr.terms))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import data.regal.config

import data.regal.rules.style["pointless-reassignment"] as rule

test_pointless_reassignment_in_rule_head if {
test_fail_pointless_reassignment_in_rule_head if {
module := ast.with_rego_v1(`
foo := "foo"
Expand Down Expand Up @@ -37,7 +37,7 @@ test_pointless_reassignment_in_rule_head if {
}}
}

test_pointless_reassignment_in_rule_body if {
test_fail_pointless_reassignment_in_rule_body if {
module := ast.with_rego_v1(`
rule if {
foo := "foo"
Expand Down Expand Up @@ -69,7 +69,7 @@ test_pointless_reassignment_in_rule_body if {
}}
}

test_pointless_reassignment_in_rule_body_using_with if {
test_success_pointless_reassignment_in_rule_body_using_with if {
module := ast.with_rego_v1(`
foo := input
Expand All @@ -83,3 +83,16 @@ test_pointless_reassignment_in_rule_body_using_with if {
r := rule.report with input as module
r == set()
}

test_success_not_pointless_reassignment_to_array if {
module := ast.with_rego_v1(`
parts := split(input.arr, ".")
rule := [b, a] if {
[a, b] := parts
}
`)

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

0 comments on commit c0ac18f

Please # to comment.