Skip to content

Commit

Permalink
rule: Fix pointless issue when using with (#907)
Browse files Browse the repository at this point in the history
Fixes #904

I find the times to be comparable for the regal bundle +
a simple violating file.

```sh
regal $ go build && time ./regal lint .  --disable-all --enable=pointless-r
eassignment
Rule:           pointless-reassignment
Description:    Pointless reassignment of variable
Category:       style
Location:       thing.rego:10:6
Text:           foo := e with foo as 2
Documentation:  https://docs.styra.com/regal/rules/style/pointless-reassignment

201 files linted. 1 violation found in 1 file.

real    0m1.906s
user    0m6.344s
sys     0m0.244s
[3]regal $ bran
Current:  main
Switched to branch 'pointless'
Your branch is ahead of 'main' by 1 commit.
  (use "git push" to publish your local commits)
regal $ go build && time ./regal lint .  --disable-all --enable=pointless-reassignment
201 files linted. No violations found.

real    0m1.580s
user    0m2.930s
sys     0m0.109s
```

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Jul 9, 2024
1 parent 35d6703 commit 288d9c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 7 additions & 12 deletions bundle/regal/rules/style/pointless_reassignment.rego
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ report contains violation if {

# pointless reassignment in rule body
report contains violation if {
some call in ast.all_refs
some rule in input.rules
some expr in rule.body

call[0].value[0].type == "var"
call[0].value[0].value == "assign"
not expr["with"]

call[2].type == "var"
expr.terms[0].value[0].type == "var"
expr.terms[0].value[0].value == "assign"
expr.terms[2].type == "var"

violation := result.fail(rego.metadata.chain(), result.location(call))
}

assign_calls contains call if {
some call in ast.all_refs

call[0].value[0].type == "var"
call[0].value[0].value == "assign"
violation := result.fail(rego.metadata.chain(), result.location(expr.terms))
}
15 changes: 15 additions & 0 deletions bundle/regal/rules/style/pointless_reassignment_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ test_pointless_reassignment_in_rule_body if {
"title": "pointless-reassignment",
}}
}

test_pointless_reassignment_in_rule_body_using_with if {
module := ast.with_rego_v1(`
foo := input
rule if {
bar := foo with input as "wow"
bar == true
}
`)

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

0 comments on commit 288d9c3

Please sign in to comment.