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

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

Closed
mvdan opened this issue Oct 20, 2017 · 4 comments
Closed

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

mvdan opened this issue Oct 20, 2017 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.

Comments

@mvdan
Copy link
Member

mvdan commented Oct 20, 2017

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

@mvdan mvdan changed the title cmd/gofmt: cmd/gofmt: comments in empty composite literals cannot be indented Oct 20, 2017
@mvdan
Copy link
Member Author

mvdan commented Oct 20, 2017

Also, happy to work on a patch if such a change would be desirable.

@gopherbot
Copy link

Change https://golang.org/cl/72190 mentions this issue: cmd/compile: test inlineability of math/bits funcs

@griesemer
Copy link
Contributor

I agree this a bit odd. @mvdan Feel free to give this a shot but I suspect this is going to be rather tricky to get right. Happy to be proven wrong, too.

@mvdan mvdan self-assigned this Oct 20, 2017
@mvdan mvdan added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 29, 2017
@gopherbot
Copy link

Change https://golang.org/cl/74232 mentions this issue: go/printer: indent lone comments in composite lits

@golang golang locked and limited conversation to collaborators Nov 15, 2018
@rsc rsc unassigned mvdan Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants