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
Assignees

Comments

@mvdan
Copy link
Member

@mvdan 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 mvdan commented Oct 20, 2017

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

@gopherbot
Copy link

@gopherbot gopherbot commented Oct 20, 2017

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

@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 20, 2017

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 label Oct 29, 2017
@gopherbot
Copy link

@gopherbot gopherbot commented Oct 29, 2017

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

@gopherbot gopherbot closed this in a265f2e Nov 15, 2017
@golang golang locked and limited conversation to collaborators Nov 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.