Skip to content
This repository was archived by the owner on May 9, 2021. It is now read-only.
This repository was archived by the owner on May 9, 2021. It is now read-only.

Warn when calling reflect.DeepEqual with errors #232

@zombiezen

Description

@zombiezen

A common antipattern I see in production code is using reflect.DeepEqual on errors in tests, which results in brittle tests. Even just adding more diagnostic information to the error can break it. The more proper thing to do is either:

  1. Check whether a substring is present in the error message (not great, but more correct).
  2. Use something like Dave Cheney's errors package to make the system-under-test provide more useful errors (great! because it's better API design anyway).

It would be helpful to have a lint check that discourages this by warning when either parameter to reflect.DeepEqual is of error type.

One additional point, quoting @dsnet:

If they are structs that contain a field of an error type, no warning is given. While I would consider using deep equal in that case a code smell, there are legitimate uses of it when it is used to compare sentinel errors.

(This was originally reported as golang/go#16748.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions