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

Double indirection on error that will never satisfy errors.As lint #71

Open
stefanvanburen opened this issue Mar 14, 2024 · 0 comments
Open

Comments

@stefanvanburen
Copy link

package main

import (
	"errors"
	"fmt"
)

type testError struct {
	msg string
}

func (te testError) Error() string { return te.msg }

func main() {
	te := testError{msg: "hello"}
	var terr *testError
	if errors.As(te, &terr) {
		fmt.Println(terr)
	} else {
		fmt.Println("errors.As failed")
	}
}

go.dev/play link

In this example, Error is defined on the concrete testError type (instead of the more idiomatic pointer receiver). Below, matching the error starting with a pointer and then indirecting that pointer in the errors.As call won't work, because the pointer given to errors.As is to a *testError (not satisfying the Error interface) instead of testError. It seems like a lint could be added that checks for this case.

go vet has the -errorsas analysis, but doesn't catch this case, and I'm not currently finding prior art on a lint of this type. (It could be not possible due to something I'm not thinking of.)

For a concrete example, I was recently bit by this while doing an error.As check on an http2.StreamError, which implements Error on the concrete type.

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

No branches or pull requests

1 participant