112 points · by rbanffy · 2y ago · 58 comments
[1]: https://www.micahcantor.com/blog/js-of-ocaml-dead-code/
golangci-lint rolls up lot of linters and checkers into a single binary.
There is a config file too.
if 1==2 { ...some code to temporarily disable ... }
"Just comment it out" is not always enough because when I want to quickly toggle the code on and off multiple times in a single session, it's not possible to do by only commenting/uncommenting that single block.Sometimes the code that's commented out contains the only reference to an imported package, and by commenting it out the autoformatter helpfully removes the import from the top of the file, so then uncommenting the line is not enough -- I need to go back and re-add the import.
Or I may want to comment out this line
x = f(a, b)
but that makes a and b in turn become unused as well, and I need to travel up to wherever they're defined and comment them out too, and so on with anything else that became thus unused in that chain.And this is all suboptimal because the warning is also important when debugging. I do want to get warned about the unused code! Just let me compile.
The same tool that removes your imports also adds them so there usually isn’t an extra step.
I would do the first-time compile on this template and then add things iteratively.
Go would be a nonstarter for this experimental/artistic code, because I would usually have dead code in my "toolbox" to iterate and experiment with until I liked the end product.
Does anyone know how to work around this?
deadcode ./cmd/...TL;DR: if your project is a library and an exported function looks like dead code, that means it isn't covered by any tests, and you should probably fix that.
Doesn't seem far-fetched to ask for the compiler to warn on unused types/functions too.
So there are two choices here:
1. The Go compiler complains about code which is unused by the current main package.
2. The Go compiler tries to find/compile all main packages every-time.
Both of these options suck.Tree shaking shouldn't be necessary in 2024 anyway.
Most have it on the standard tools, but I don't think any language was born with it.
Is this not the Greeter interface example from the OP? The example finds an unused variant of an interface and all code only invoked by said unused implementations and marks all of it as deadcode.