I found several cases in my codebase (kopia/kopia#747) that follow the following buggy pattern:
err := doSomething()
if err != nil {
return result, errors.Wrap(err, "something failed")
}
if somethingElse {
return result, errors.Wrap(err, "something else failed")
}
The second errors.Wrap() is incorrect, because it's not wrapping any error (but it's easy to copy/paste it as such).
This is quite hard to spot in code reviews because lines that return errors will always have errors.Wrap() and it looks ok at first glance, until you notice that in this case is err is always nil. Because errors.Wrap(nil, "something") always returns nil this one returns success, which is unintended.
Based on flow analysis, it should be possible to determine that err == nil in this case, and it would be really amazing if the linter could flag this pattern is as misuse.
I found several cases in my codebase (kopia/kopia#747) that follow the following buggy pattern:
The second
errors.Wrap()is incorrect, because it's not wrapping any error (but it's easy to copy/paste it as such).This is quite hard to spot in code reviews because lines that return errors will always have
errors.Wrap()and it looks ok at first glance, until you notice that in this case iserris alwaysnil. Becauseerrors.Wrap(nil, "something")always returnsnilthis one returns success, which is unintended.Based on flow analysis, it should be possible to determine that
err == nilin this case, and it would be really amazing if the linter could flag this pattern is as misuse.