Skip to content

cmd/gofmt: comment in return params is misplaced  #28433

@benesch

Description

@benesch

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

go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

Ran gofmt on this program (https://play.golang.org/p/1rASHggr4wx):

package foo

func bar() (
	// This return value is extremely subtle! Please make sure you understand
	// what it does.
	string,
) {
	return "42"
}

What did you expect to see?

The input, unchanged.

What did you see instead?

The program reformatted with the comment in a very strange place (https://play.golang.org/p/NujRVxVN6N5):

package foo

func bar() string {// This return value is extremely subtle! Please make sure you understand
	// what it does.

	return "42"
}

Arguably not that common to stick a comment there, but I find the resulting code extremely surprising. Note that this nearly identical program works just fine (https://play.golang.org/p/Hpfcc5HX00l) because the presence of bazzle makes the parens required and so gofmt doesn't attempt a reformatting:

package foo

func bar() (
	// This return value is extremely subtle! Please make sure you understand
	// what it does.
	bazzle string,
) {
	return "42"
}

/cc @griesemer since you seem to get pinged on all the gofmt issues eventually

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions