Skip to content
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

Ignore errors in closures passed to specific functions #51

Open
SOF3 opened this issue Jun 27, 2024 · 4 comments
Open

Ignore errors in closures passed to specific functions #51

SOF3 opened this issue Jun 27, 2024 · 4 comments

Comments

@SOF3
Copy link

SOF3 commented Jun 27, 2024

I have a utility function like this:

type Slice[T any] []T

func (slice Slice[T]) TryForEach(message string, fn func(T) error) error {
    for _, item := range slice {
        if err := fn(item); err != nil {
            return errors.Wrap(err, message)
        }
    }
    return nil
}

However, when I call this function:

slice.TryForEach("message", func(value T) error {
    return otherpkg.DoSomething(value)
}

I get an error for not wrapping DoSomething.

TryForEach actually already wraps the error. It would be useful to have an ignore directive like this:

wrapcheck:
  ignoreClosureInArgList:
    - func: "TryForEach"
      param: "fn"

such that the lint is disabled for top-level return statements (and not including nested closures) in closures if the closure is directly passed as the fn parameter for a function matching TryForEach.

@SOF3
Copy link
Author

SOF3 commented Jun 27, 2024

I'm happy to contribute this feature myself, would just like to hear some opinions on the design first.

@tomarrell
Copy link
Owner

Hmm, that is an interesting use case. Quite specific, it seems.

I'm not opposed to it, but I'm currently thinking of the value versus adding a line level ignore. Is this something that happens enough to you that it's worth the implementation effort for you?

@SOF3
Copy link
Author

SOF3 commented Jun 27, 2024

Well, I use the TryForEach function a lot in the implementation of a certain interface in my project, so I have to add //nolint:wrapcheck above the whole function for every implementation of this interface. I agree this is a bit niche; would be great if anyone could propose a more reusable ignore rule.

@tomarrell
Copy link
Owner

Fair enough, alright, if you want to open a PR with a proposed implementation I'd be happy to review it.

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

No branches or pull requests

2 participants