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

Rule: Add rule to avoid using implicit true when building an object (require explicit value) #1396

Open
rplnt opened this issue Feb 5, 2025 · 1 comment

Comments

@rplnt
Copy link

rplnt commented Feb 5, 2025

Summary

Add rule requiring explicit value (instead of relying on implicit true) when creating an object using the syntax foo[key] if {...}

Reasoning

In v1, this creates an object hello={"world":true}:

hello[input.message] if {
	input.message == "world"
}

While almost identical code in v0 created a set hello={"world"}:

hello[input.message] {
	input.message == "world"
}

Considering the amount of documentation/discussions available for v0 syntax it's easy to mistake these two. And even when ignoring that aspect, it's better for readability to have it explicitly written out as such:

hello[input.message] := true if {
	input.message == "world"
}

Notes

  • There might be other instances where this rule (avoid implicit true) may apply, this was the one I stumbled upon
  • Automatic fix should be possible see comment
@anderseknert
Copy link
Member

Thank you for filing this!

I believe that's the only case where the implict assignment is confusing, and especially when moving between v0 and v1. We'll look into adding a rule to check for that before next release.

Automating a fix here is a little trickier, I think. Because we can't know if the user was trying to define a set the "old way" or the intention was to define an object. So perhaps best to just report it and let the user decide how to fix it? Obviously we'll cover both cases in the rule's documentation.

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

No branches or pull requests

2 participants