Skip to content

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

@btracey

Description

@btracey

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions