Skip to content
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

bundle: Add non-loop-expression rule #1401

Merged
merged 8 commits into from
Feb 11, 2025

Conversation

charlieegan3
Copy link
Member

@charlieegan3 charlieegan3 commented Feb 6, 2025

Fixes #93

TODO:

  • Docs for the rule
  • Optimizations on the rego / checking performance
  • ignore print statements

@charlieegan3 charlieegan3 marked this pull request as ready for review February 10, 2025 11:17
Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work here! Left a couple of comments but nothing blocking.

@@ -18,6 +18,8 @@ items contains item if {
position := location.to_position(input.regal.context.location)
line := input.regal.file.lines[position.line]

not endswith(trim_space(line), "=")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

There's nothing better than writing a Regal rule that finds issues in Regal.

`)

sps == {"0": {
5: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting how the first level variable can't be numeric (due to that type checker bug), but seems to work for this next level. That should be fixed of course, but certainly out of scope for this PR :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this was an ongoing pain when working on this one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hoping that @johanfylling can fix that in OPA soon :)


## Exceptions

This rule cannot yet detect the following cases.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

charlieegan3 and others added 8 commits February 11, 2025 12:27
Like we predicted, this rule costs a lot. 6 million allocations compared
to main is probably a record! But the value of the rule is also great, so
we'll have to make the best of it. Some perf tweaks help shave off at least
1 million allocations, and we're now down to 5.

Next up, I'll be taking a look at where most of the cost in OPA is while
evaluating this.

Funnily, I also found a missing case in the `use-object-keys` rule I added
just today, from seeing a set comprehension being used to get keys but not
getting flagged by the rule. Turned out `object.keys` was also noticeably
faster :)

```
1872039250 ns/op	3277562680 B/op	64585680 allocs/op // main
1990034458 ns/op	3577600880 B/op	70427856 allocs/op // non-loop-expression PR
1981020667 ns/op	3515957384 B/op	69337090 allocs/op // use built-ins to get _exprs row
1976604792 ns/op	3518227272 B/op	69422952 allocs/op // inline ast.is_wildcard
1957780375 ns/op	3518962424 B/op	69226825 allocs/op // uglier loops
1923554667 ns/op	3501324176 B/op	68954057 allocs/op // object.keys(sps)
```

Signed-off-by: Anders Eknert <[email protected]>
This will be detected by another rule.

Signed-off-by: Charlie Egan <[email protected]>
@charlieegan3 charlieegan3 merged commit 539ba39 into StyraInc:main Feb 11, 2025
5 checks passed
@charlieegan3 charlieegan3 deleted the non-loop-expression branch February 11, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule: Detect expressions that may be moved outside of loop
2 participants