go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

Detect useless anonymous functions

Open dsnet opened this issue 3 months ago • 1 comments

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).

dsnet avatar Oct 07 '25 04:10 dsnet

Related: #123

dominikh avatar Oct 07 '25 18:10 dominikh