Skip to content

cmd/vet: warn when errors.As target has type *error #47528

@andig

Description

@andig

What version of Go are you using (go version)?

$ go version
go version go1.16.6 darwin/arm64

Does this issue reproduce with the latest release?

yes

What did you do?

Wrote this code https://play.golang.org/p/16cU0kc8Lku and wondered why the errors matched.

var Err = errors.New("sentinel")
err := errors.New("foo")
if errors.As(err, &Err) {
  fmt.Println("why?")
}

Discussion in https://groups.google.com/g/golang-nuts/c/MaYJy_IRbYA/m/uCHV6P87EQAJ?utm_medium=email&utm_source=footer

What did you expect to see?

Of course the code above is wrong, yet I didn't spot it. It would be nice if this kind of programming error could be detected:

  • add a govet check that flags using plain errors with errors.As or
  • @Merovius suggested errors.errorString should implement As() and return false, unless the pointers match.

I'm unsure if the latter is possible. It would strictly break BC (and the function's description). On the other hand ist should only change behaviour in cases that seem invalid from the start?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions