Skip to content

Clothe returns #287

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

Merged
merged 1 commit into from
Apr 21, 2025
Merged

Clothe returns #287

merged 1 commit into from
Apr 21, 2025

Conversation

flimzy
Copy link

@flimzy flimzy commented Oct 20, 2023

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
Copy link
Author

flimzy commented Oct 20, 2023

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

@flimzy
Copy link
Author

flimzy commented Oct 23, 2023

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

@flimzy
Copy link
Author

flimzy commented Oct 23, 2023

See flimzy@64e6495 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
Copy link
Author

flimzy commented Oct 24, 2023

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.

Copy link
Owner

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, just some thoughts, in particular I'm worried about PathEnclosingInterval :)

@flimzy flimzy force-pushed the clotheReturns branch 2 times, most recently from 71eb4a2 to 61a1113 Compare January 29, 2024 12:39
@flimzy
Copy link
Author

flimzy commented Apr 16, 2024

Anything else before merging this one, @mvdan ?

@mvdan
Copy link
Owner

mvdan commented Apr 13, 2025

Apologies for the slowness and thanks for bearing with me. I'm not sure why I didn't get to this sooner, I must admit.

I hope it's okay that I've pushed to your branch. First, to rebase your changes. Second, to simplify the logic to track parent func types; see my commit message for details.

Does my change look OK to you? If so, please squash all the commits and write a commit message summarizing the change. I'm happy to do this for you if you prefer, too. The change LGTM at this point, but I'd want to merge a single commit.

@flimzy
Copy link
Author

flimzy commented Apr 14, 2025

@mvdan Looks great to me. Glad you had a chance to look at it and simplify things. Feel free to squash if that's quick for you. Otherwise I'll get to it in the coming days.

@mvdan
Copy link
Owner

mvdan commented Apr 14, 2025

ack, I'll do that then :)

When a function has named results, one can use a "naked return"
to implicitly return those variables. That is, the code

    func Foo() (err error) {
        return
    }

is functionally equivalent to

    func Foo() (err error) {
        return err
    }

Naked returns exist primarily for the sake of avoiding repetition,
but even the Go tour recommends that their use should be limited:

    Naked return statements should be used only in short functions,
    as with the example shown here. They can harm readability in longer functions.

Most Go functions out in the wild aren't short, and in practice,
naked returns tend to be avoided in larger or collaborative codebases
as they confuse the reader about whether any values are being returned,
or which values are being returned if any.

Given that gofumpt is already opinionated, and its use tends to be on
Go modules on the larger side or with multiple contributors,
it seems best to rewrite naked returns to "clothe" them.

Fixes mvdan#285.
@mvdan
Copy link
Owner

mvdan commented Apr 21, 2025

Squashed, and commit message written. I also tweaked the README and comments a bit. Will merge on green.

@mvdan mvdan merged commit 72ae882 into mvdan:master Apr 21, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: Clothe naked returns
2 participants