Skip to content

Formatting can change code semantics for literal strings #9

@NiklasRosenstein

Description

@NiklasRosenstein

The following code:

  dynamic "rule" {
    for_each = concat(local.default_firewall_rules.open_tcp_ports, var.extra_firewall_rules.open_tcp_ports)
    content {
      direction  = "in"
      protocol   = "tcp"
      port       = "${rule.value}"
      source_ips = ["0.0.0.0/0"]
    }
  }

Is formatted to the following by the plugin:

  dynamic "rule" {
    for_each = concat(local.default_firewall_rules.open_tcp_ports, var.extra_firewall_rules.open_tcp_ports)
    content {
      direction  = "in"
      protocol   = "tcp"
      port       = rule.value
      source_ips = ["0.0.0.0/0"]
    }
  }

Note that "${rule.value}" got unpacked into just rule.value. The resulting code however has different semantics as here it is intended to be used as an alternative for tostring().

Arguments made that one should use tostrings() instead, but it is fundamentally important property of a formatter to not change the semantics of the code.

If one would want such a change to be done automatically in some fashion, it would be a linter's job, behind an --unsafe-fixes (or vice-versa) option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions