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

proposal: cmd/vet: Not using the return value of a function that only produces an error should be flagged #72926

Closed
rski opened this issue Mar 18, 2025 · 8 comments
Labels
Milestone

Comments

@rski
Copy link

rski commented Mar 18, 2025

Proposal Details

I ran into a piece of code today that did

if something {
   fmt.Errorf("something happened: %s", someString)
}

if somethingElse {
  return fmt.Errorf("something else happened: %s", someOtherString)
}

Clearly the first one was a mistake, the intention was to return. I think it would be nice if vet flagged such uses of Errorf, Wrapf, errors.New as they are most definitely not intentional.

There is an existing tool, https://github.com/kisielk/errcheck which would have pointed this out. However, in my experience integrating it in existing codebases can be quite tedious and requires fixing many calls that might be fishy but not necessarily buggy.

@rski rski added the Proposal label Mar 18, 2025
@gopherbot gopherbot added this to the Proposal milestone Mar 18, 2025
@seankhliao
Copy link
Member

As your experience points out, this wouldn't meet the accuracy bar for vet.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2025
@rski
Copy link
Author

rski commented Mar 18, 2025

sorry, I meant to contrast them. errcheck checks for every single possible function. I'm suggesting vet only check the ones from the standard library, which would be very accurate

@randall77
Copy link
Contributor

What about io.Closer? Do we have to handle the error returned by any Close call in the stdlib?

@rski
Copy link
Author

rski commented Mar 18, 2025

Close has side effects. Errorf doesn't, the whole point of it is to create an error, so doing nothing with it doesn't make sense. To me it's the same sort of check as not allowing unused variables.

@randall77
Copy link
Contributor

Sure, but how do you differentiate Close from Errorf? They both return just an error.

@rski
Copy link
Author

rski commented Mar 18, 2025

oh, by listing the relevant functions out I guess

@randall77
Copy link
Contributor

Right, just depending on the return type as proposed is probably insufficient.
So I think this issue is asking for something more specific. Maybe just fmt.Errorf? Some more expansive list? Would it be opt-in or opt-out for new functions?
Something similar to #62729 for fmt?

@ianlancetaylor
Copy link
Member

This is a subset of #65984.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants