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

Detect never-nil interface returns #1615

Open
FiloSottile opened this issue Nov 3, 2024 · 1 comment
Open

Detect never-nil interface returns #1615

FiloSottile opened this issue Nov 3, 2024 · 1 comment
Labels
waiting-for-feedback Waiting for the user to get back to us

Comments

@FiloSottile
Copy link

The following program prints false because the foo() return value is always a non-nil interface wrapping a nil bar() return value.

package main

import "errors"

func main() {
	x, _ := foo()
	println(x == nil)
}

func foo() (interface{}, error) {
	return bar()
}

func bar() (*struct{}, error) {
	return nil, errors.New("error")
}

Today I was about to ship something similar to an exposed standard library interface when a test stopped me.

Not sure it can be done with few enough false positives, but I would have loved for staticcheck to point out that foo() always returns non-nil, which given the error return is almost certainly not intended.

@FiloSottile FiloSottile added the needs-triage Newly filed issue that needs triage label Nov 3, 2024
@dominikh
Copy link
Owner

dominikh commented Nov 3, 2024

ISTM that we already catch this?

bar.go:7:10: this comparison is never true (SA4023)
	bar.go:6:2: the lhs of the comparison is the 1st return value of this function call
	bar.go:10:6: command-line-arguments.foo never returns a nil interface value

@dominikh dominikh added waiting-for-feedback Waiting for the user to get back to us and removed needs-triage Newly filed issue that needs triage labels Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-for-feedback Waiting for the user to get back to us
Projects
None yet
Development

No branches or pull requests

2 participants