Skip to content

Report [for k, v in set: k] as anti-pattern diagnostic #1609

Open
@radeksimko

Description

@radeksimko

Language Server Version

v0.32.6

Problem Statement

Users can today use the following syntax to loop over a set:

variable "set" {
  type    = set(string)
  default = ["aaa", "bbb"]
}

output "test" {
  value = [for key, val in var.set : key]
}

This is functionally equivalent to

variable "set" {
  type    = set(string)
  default = ["aaa", "bbb"]
}

output "test" {
  value = [for key, val in var.set : val]
}

In other words, looping over "set keys" is allowed but sets are not sorted (hence don't have indexes like lists or tuples) and don't really have their own keys either (like maps or objects).

Therefore, out of the two examples, only the bottom one should be considered best practice.

Attempted Solutions

No response

Proposal

  • Report looping over set keys as warning diagnostic
  • Provide quick fix code action to replace key with value reference

Related LSP methods

No response

References

No response

Help Wanted

  • I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions