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: comprehension-term-assignment #1073

Closed
anderseknert opened this issue Sep 7, 2024 · 1 comment · Fixed by #1098
Closed

Rule: comprehension-term-assignment #1073

anderseknert opened this issue Sep 7, 2024 · 1 comment · Fixed by #1098

Comments

@anderseknert
Copy link
Member

Somewhat similar to the unconditional-assignment rule, and also in the style category. I think it would be good to have Regal help simplify comprehensions where the last expression is a simple assignment to a term variable, and suggest moving the value directly to the comprehension head instead.

Avoid

to_string(ref) := concat(".", [path |
    some part in ref
    path := part.value
])

Prefer

to_string(ref) := concat(".", [part.value | some part in ref])

Note that while it's often possible to move a lot of code in that direction, like function calls — I don't think that helps readability at all, and so we should stick to recommend this for simple assignment like the example shown above.

@anderseknert anderseknert changed the title Rule: comprehension-tail-assignment Rule: comprehension-term-assignment Sep 8, 2024
@anderseknert
Copy link
Member Author

Renamed from "tail" assignment to "term" assignment, as detecting simple assignment of a term var elsewhere involves the same check more or less, just peformed over all the expressions. So something like this should be flagged just the same:

Avoid

to_string(ref) := concat(".", [path |
    some part in ref
    path := part.value
    part.type == "string"
])

Prefer

to_string(ref) := concat(".", [part.value |
    some part in ref
    part.type == "string"
])

anderseknert added a commit that referenced this issue Sep 10, 2024
Flag redundant assignment of simple values that could be used
directly as the comprehension term (or key/value).

Fixes #1073

Signed-off-by: Anders Eknert <anders@styra.com>
srenatus pushed a commit to srenatus/regal that referenced this issue Oct 1, 2024
Flag redundant assignment of simple values that could be used
directly as the comprehension term (or key/value).

Fixes StyraInc#1073

Signed-off-by: Anders Eknert <anders@styra.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant