mvdan/goreduce

Try to replace all exprs by zero values of their types

Closed this issue · 1 comments

mvdan commented

Right now we do this with basic literals only. For example, we try to replace "foo" with "" and 123 with 0.

But we could do this with basically all expressions, being more aggressive:

  • getInt() could be replaced with 0
  • someStruct could be replaced with Struct{}
  • 1.0 * 10.0 - 2 could be replaced with 0.0

I need to think about this, however. It might not always be a good idea. And sometimes the result might be awkward, since we might need parentheses or type conversions to make it compile properly.

Also worth noting that we could instead go for doing these step-by-step. The third example should be doable by replacing each literal and removing BinaryExpr sides. The second should be done by inlining a variable and zeroing the composite literal.

The first one will also be done, once we're able to inline somewhat complex func calls.

So need to also think whether or not this would make the tool able to reduce more programs. If not, we have to be careful if this is for performance because it may well actually make the tool slower.

mvdan commented

I struggle to imagine a scenario where this would help us reduce more programs. And as said, also hard to tell if this would make the tool faster overall.

And since this adds complexity, deciding against it for now.