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

proposal: cmd/gofmt: A change of spacing within for...range <int> loops #65643

Open
adamroyjones opened this issue Feb 9, 2024 · 3 comments
Open
Labels
Milestone

Comments

@adamroyjones
Copy link

Proposal Details

I know this is dangerously close to bike-shedding but I thought it worth the suggestion, even if it ends up being closed.

Here's a quite tortured example that's derived from something slightly less tortured.

package main

func main() {
	xs := []int{0, 1, 2}
	n := 1
	ys := make([]int, 0, len(xs)-(n-1))
	for range len(xs)-(n-1) {
		ys = append(ys, len(xs)-(n-1))
	}
}

When passed through gofmt, this becomes

package main

func main() {
	xs := []int{0, 1, 2}
	n := 1
	ys := make([]int, 0, len(xs)-(n-1))
	for range len(xs) - (n - 1) {
		ys = append(ys, len(xs)-(n-1))
	}
}

which, to my eye, seems worse. The terms after the range keyword form a tight conceptual cluster, but the text is spread.

I didn't see any discussion on #61405 about associated gofmt rules, so this may be an unintended outcome.

I propose that the spacing be as with function calls. I'm happy to have a stab at an implementation.

@gopherbot gopherbot added this to the Proposal milestone Feb 9, 2024
@seankhliao
Copy link
Member

The formatting is consistent with when the expression is used outside of range:

_ = len(xs) - (n - 1)

@ianlancetaylor
Copy link
Contributor

gofmt works without type information, so I don't know that there is any reasonable way to make this change, even if we want it.

CC @griesemer

@griesemer
Copy link
Contributor

I will need to double-check but I believe this formatting solely depends on the expression and doesn't need type information. So it should be possible to do (and would be trivial to do), assuming there's agreement on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants