mvdan / gofumpt

A stricter gofmt
https://pkg.go.dev/mvdan.cc/gofumpt
BSD 3-Clause "New" or "Revised" License
3.15k stars 110 forks source link

Clothe returns #287

Open flimzy opened 8 months ago

flimzy commented 8 months ago

Fixes #285


At present, this transformation ignores naked returns in functions with blank-named return values (i.e. func() (_ int, err error)) because rewriting them requires a literal zero value, which seemed unnecessary to evaluate the usefulness of this rule. It can be updated if we decide to move forward with this.

There's probably also room to improve the algorithm used to determine the nearest function parent to a given return statement. I didn't see any prior art within this project to copy, so I chose an easy option to implement, which I expect is enough to evaluate the usefulness. And we can optimize if deemed necessary.

flimzy commented 8 months ago

This is failing to properly adjust the location of comments after inserting the return values. I'll continue working on that.

flimzy commented 8 months ago

The comments issue should be resolved with a727581fef44aa7abc4968d658d8fef63ebbe071 (Thanks @mvdan for the help on Slack!)

flimzy commented 8 months ago

See https://github.com/flimzy/gofumpt/commit/64e649574265f1d53456df83257cee690b5a36c7 for what this new transform does on the gofumpt repo itself. I'll do further testing on a larger collection of repos when time permits.

flimzy commented 8 months ago

I've run this change against the stdlib, as well as the repos in this list. I found two panics, which already existed in gofumpt prior to this change. Otherwise, the output appears as expected and, in my subjective opinion, much easier to read.

I could share some diffs, but they're bulky and boring to read, honestly.

If you'd like me to do any additional investigation, please let me know.

flimzy commented 2 months ago

Anything else before merging this one, @mvdan ?