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

Named return values not reported #13

Open
cornfeedhobo opened this issue Apr 12, 2021 · 3 comments
Open

Named return values not reported #13

cornfeedhobo opened this issue Apr 12, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@cornfeedhobo
Copy link

Try this out

package main

import (
	"errors"
)

func somethingDangerous() error {
	return errors.New("fake err")
}

func run() (err error) {
	err = somethingDangerous()
	return
}

func main() {
	if err := run(); err != nil {
		println(err)
	}
}

You'll see that wrap check does not detect that err is not being wrapped.

@tomarrell
Copy link
Owner

G'day, thanks a lot for the report. This particular example is actually not expected to be wrapped, as calls to errors.New() are explicitly ignored as it is usually called directly as part of the return.

Secondly, the call to somethingDangerous() is in the same package, therefore does not satisfy the invariant of being either 1. a func in a separate package or 2. a method call on an interface.

However, you did bring up a good point with regards to named return values. I have been able to make a similar example which in fact should be wrapped and is not reported. The example is below:

package main

import "encoding/json"

func main() {
	do()
}

func do() (err error) {
	err = json.Unmarshal([]byte(""), nil)
	return // TODO want `error returned from external package is unwrapped`
}

You can see that the error returned by a call to a separate package func json.Unmarshal is returned directly without being wrapped. You're correct that this case is a false negative.

I've written a test case that fails. If you're interesting in contributing a solution that would be massively appreciated, as my time at the moment is a bit on the short side.

Cheers

@tomarrell tomarrell changed the title False negative Named return values not reported Apr 16, 2021
@tomarrell tomarrell added the bug Something isn't working label Apr 16, 2021
@tomarrell
Copy link
Owner

tomarrell commented Apr 16, 2021

The test case was added in commit: e75cc2a behind a TODO.

@cornfeedhobo
Copy link
Author

@tomarrell Awesome! Thanks for taking a look. Indeed, if I find time, I'll take a crack at a PR, but my repos are demanding time too 😅. I guess we'll see who gets time first.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants