Skip to content

cmd/gofmt: comments in empty composite literals cannot be indented #22355

@mvdan

Description

@mvdan

See https://play.golang.org/p/L5cPyMPVl7. If you press "Format", you'll get:

var _ = map[string][]int{
	"foo": []string{
		1,
		2,
	},
	"bar": []string{
	// 3,
	// 4,
	},
}

However, add a non-commented out element like in https://play.golang.org/p/jJE6F2YXDQ, and it will be formatted with the proper indentation. Note that adding an element before the comments also fixes the issue.

Since gofmt seems to not care about the user's choice in this scenario, I would say that comments on their own lines in otherwise empty composite literals should be indented.

Happy to hear counter-arguments or an explanation for the current logic, if it is on purpose. Perhaps there are other fixes too, such as respecting the user's original style (which I think happens in switch cases).

/cc @griesemer

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions