-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule: pointless-reassignment
#878
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# METADATA | ||
# description: Pointless reassignment of variable | ||
package regal.rules.style["pointless-reassignment"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
|
||
# pointless reassignment in rule head | ||
report contains violation if { | ||
some rule in ast.rules | ||
|
||
ast.generated_body(rule) | ||
|
||
rule.head.value.type == "var" | ||
count(rule.head.ref) == 1 | ||
|
||
violation := result.fail(rego.metadata.chain(), result.location(rule)) | ||
} | ||
|
||
# pointless reassignment in rule body | ||
report contains violation if { | ||
some call in ast.all_refs | ||
|
||
call[0].value[0].type == "var" | ||
call[0].value[0].value == "assign" | ||
|
||
call[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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package regal.rules.style["pointless-reassignment_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.config | ||
|
||
import data.regal.rules.style["pointless-reassignment"] as rule | ||
|
||
test_pointless_reassignment_in_rule_head if { | ||
module := ast.with_rego_v1(` | ||
foo := "foo" | ||
|
||
bar := foo | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "style", | ||
"description": "Pointless reassignment of variable", | ||
"level": "error", | ||
"location": {"col": 2, "file": "policy.rego", "row": 8, "text": "\tbar := foo"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/pointless-reassignment", "style"), | ||
}], | ||
"title": "pointless-reassignment", | ||
}} | ||
} | ||
|
||
test_pointless_reassignment_in_rule_body if { | ||
module := ast.with_rego_v1(` | ||
rule if { | ||
foo := "foo" | ||
|
||
bar := foo | ||
} | ||
`) | ||
|
||
r := rule.report with input as module | ||
r == {{ | ||
"category": "style", | ||
"description": "Pointless reassignment of variable", | ||
"level": "error", | ||
"location": {"col": 7, "file": "policy.rego", "row": 9, "text": "\t\tbar := foo"}, | ||
"related_resources": [{ | ||
"description": "documentation", | ||
"ref": config.docs.resolve_url("$baseUrl/$category/pointless-reassignment", "style"), | ||
}], | ||
"title": "pointless-reassignment", | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# pointless-reassignment | ||
|
||
**Summary**: Pointless reassignment of variable | ||
|
||
**Category**: Style | ||
|
||
**Avoid** | ||
```rego | ||
package policy | ||
|
||
allow if { | ||
users := all_users | ||
any_admin(users) | ||
} | ||
``` | ||
|
||
**Prefer** | ||
```rego | ||
package policy | ||
|
||
allow if { | ||
any_admin(all_users) | ||
} | ||
``` | ||
|
||
## Rationale | ||
|
||
Values and variables are immutable in Rego, so reassigning the value of one variable to another only adds noise. | ||
|
||
## Exceptions | ||
|
||
Reassigning the value of a long reference often helps readability, and especially so when it needs to be referenced | ||
multiple times: | ||
|
||
```rego | ||
package policy | ||
|
||
allow if { | ||
users := input.context.permissions.users | ||
any_admin(users) | ||
} | ||
``` | ||
|
||
This rule does not consider such assignments violations. | ||
|
||
## Configuration Options | ||
|
||
This linter rule provides the following configuration options: | ||
|
||
```yaml | ||
rules: | ||
style: | ||
pointless-reassignment: | ||
# one of "error", "warning", "ignore" | ||
level: error | ||
``` | ||
|
||
## Community | ||
|
||
If you think you've found a problem with this rule or its documentation, would like to suggest improvements, new rules, | ||
or just talk about Regal in general, please join us in the `#regal` channel in the Styra Community | ||
[Slack](https://communityinviter.com/apps/styracommunity/signup)! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my handiwork!