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

NilAway reports guard missing for errors checked by switch #12

Open
yuxincs opened this issue Jul 28, 2023 · 0 comments
Open

NilAway reports guard missing for errors checked by switch #12

yuxincs opened this issue Jul 28, 2023 · 0 comments
Labels
false positive Requires more analysis and support good first issue Good for newcomers

Comments

@yuxincs
Copy link
Contributor

yuxincs commented Jul 28, 2023

For error-returning functions, NilAway will check if the error is properly checked, otherwise the pointer result will be treated as nilable. However, NilAway currently only supports checking the errors via an if statement, but not a type switch. Consider the following example:

func aa() (*int, error) {
	return new(int), nil
}

func bb() {
	ptr, err := aa()
	if err == nil {
		print(*ptr) // OK, no NilAway errors.
	}
	switch err.(type) {
	case nil:
		print(*ptr) // Also OK, but NilAway does report an error.
                // analysistest.go:459: go.uber.org/errorreturn/inference/errorreturn-with-inference.go:191:10: unexpected diagnostic:  Value returned as result 0 from the function `aa` lacking guarding (definitely nilable) and is dereferenced at "inference/errorreturn-with-inference.go:191:10" (must be nonnil)
	}
}

The case nil in the type switch has the same effect as a normal if statement check if err == nil, but NilAway doesn't seem to understand this fact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false positive Requires more analysis and support good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants