Skip to content

proposal: cmd/vet: reject uses of reflect.DeepEqual on imported types with unexported fields #71732

@ianlancetaylor

Description

@ianlancetaylor

Proposal Details

The reflect.DeepEqual function is simple, and people often use it in tests in preference to more complex packages like github.com/google/go-cmp/cmp. But this can be misleading, and leads to issues like #43986 and #71702.

I propose that we add a vet check warning about uses of reflect.DeepEqual with values of types that are 1) imported; 2) contain references to unexported fields. Those are cases where reflect.DeepEqual is testing the internal state of a type defined in a different package, which means that the call is subject to breaking if the other package changes the type definition.

I believe that this meets vet's requirements for correctness and precision. However, I don't know how frequent the problem is.

I note that there is already a check in vet that warns about uses of reflect.DeepEqual specifically for the type reflect.Value (though I don't know how to actually run that check; it's not part of standard vet). This more general check could perhaps subsume that one.

CC @golang/tools-team

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions