8000 Don't enforce inside returns with non-nil errors by abhinav · Pull Request #52 · abhinav/requiredfield · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Don't enforce inside returns with non-nil errors #52

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
May 11, 2025
Merged

Conversation

abhinav
Copy link
Owner
@abhinav abhinav commented May 11, 2025

If a function has a struct return type alongside an error,
it's pretty standard to do this:

if err != nil {
    return MyStruct{}, fmt.Errorf("do thing: %w", err)
}

There's no reason to enforce required fields on the struct
because the contract is that the struct value is to be ignored
if the error is non-nil.

This change adds support to detect these cases and ignore them.
It is a bit messier than "ignore if part of return with non-nil error"
because we still want to enforce required fields if the non-nil error
itself is the thing with required fields, e.g.

return nil, &MyError{...} // and MyError has required fields

Resolves #37

If a function has a struct return type alongside an error,
it's pretty standard to do this:

    if err != nil {
        return MyStruct{}, fmt.Errorf("do thing: %w", err)
    }

There's no reason to enforce required fields on the struct
because the contract is that the struct value is to be ignored
if the error is non-nil.

This change adds support to detect these cases and ignore them.
It is a bit messier than "ignore if part of return with non-nil error"
because we still want to enforce required fields if the non-nil error
itself is the thing with required fields, e.g.

    return nil, &MyError{...} // and MyError has required fields

Resolves #37
@abhinav
Copy link
Owner Author
abhinav commented May 11, 2025

This change is part of the following stack:

Change managed by git-spice.

@abhinav abhinav merged commit 6840cc4 into main May 11, 2025
4 checks passed
@abhinav abhinav deleted the err-return branch May 11, 2025 22:03
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.

Option for suppressing return MyStruct{}, err by default
1 participant
0