Skip to content

Detect useless anonymous functions #1681

@dsnet

Description

@dsnet

Consider this rewrite:

- slices.EqualFunc(c.Peers, o.Peers, func(a, b Peer) bool { return a.Equal(b) })
+ slices.EqualFunc(c.Peers, o.Peers, Peer.Equal)

The former used an unnecessary anonymous function that could instead directly used the method expression.

This is rewrite is relatively trivial to statically detect where you have function of N input arguments and M output arguments and the body includes a single return expression calling a method on the first argument with N-1 input arguments.

I considered filing this in the golang/go issue tracker as a go/vet finding, but it's not a correctness bug, but rather a cleanliness "bug" that might be suited for staticcheck (where "simplicity" is listed one of the goals).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions