cmd/gofmt: Slice literal condensed to one line and comment moved outside if comment is last line #18599

Open
btracey opened this Issue Jan 10, 2017 · 1 comment

Projects

None yet

3 participants

@btracey
Contributor
btracey commented Jan 10, 2017

Perhaps a duplicate of #9460, but the issue discussion says it's restricted to for statements, and at the very least this is another symptom of the same problem.

What version of Go are you using (go version)?

go 1.7.3

What operating system and processor architecture are you using (go env)?

go playground

In my work, it's common to have a list of cases I want to run, and comments are a good way to remove them selectively. All of the following conditions format as is

// All on one line
cases := []int{1, 2, 3}
// Cases each on own line
cases := []int{
	1,
	2,
	3,
}
// Trailing comment in the list
cases := []int{
	1,
	2,
	//3,
}
// Trailing comment with newline at the start
cases := []int{
	1, 2, 3,
	//4,
}
// All items on the same line, with first commented
cases := []int{ // 1
	2, 3, 4}

But, in the specific case where there is no newline between the start of the slice and the slice items, and the comment is on a newline between the items and the trailing brace, the comment is moved outside of the slice literal.

Specifically:

cases := []int{1, 2, 3,
	//4,
}

Formats to:

cases := []int{1, 2, 3}//4,

Furthermore, newlines are kept if there are multiple trailing comments

cases := []int{1, 2, 3,
	//4,
	//5,
}

is formatted to

cases := []int{1, 2, 3}//4,
//5,

This is undesirable formatting, particularly this last case since the newline comment feels uncoupled from the literal.

@rakyll
Member
rakyll commented Jan 10, 2017
@griesemer griesemer self-assigned this Jan 10, 2017
@griesemer griesemer added this to the Go1.9Maybe milestone Jan 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment