Check for dead code when building and testing


I’m writing a text editor in rust. It’s a binary and a lib that is only used by this one binary.

When compiling, I would like a way to see dead code (IE lib code that might be public, but is not used by the bin).

When running tests, I would like to see all dead code. This would work as a poor’s man code coverage to check what code is being exercised by the tests.

Is there a way to do one, or both?


You mention it could act as a poor man’s code coverage… what about using an actual code coverage tool to see what’s being run? e.g. cargo-cov, cargo-kcov, or cargo-tarpaulin.


Thanks, these are very useful links.

But I’m still uncomfortable with the bin/lib thing. I’m making it a lib just for the sake of testability. I don’t really want to have a public interface. I want to strip off everything that is not used.


You’ll find that most projects use this bin/lib way of breaking up their application. The library will contain all your application logic and is usually 90% of the project in terms of code. The binary’s job is purely to set things up beforehand and make sure all inputs are in the right shape before calling into the library to do everything else.

Don’t think of it as unnecessary code bloat or boilerplate, think of it more as a way to do separation of concerns. Especially for something a bit larger like a text editor, it’ll really help to keep things modular and manageable in the long run.

Also, if it’s just for testability then why not just use normal unit tests which are right next to the code they’re testing?


I get all of that. But the whole reason we do that is because there is no easy way to do separation of concerns in a binary without using a lib. Now I have a lot of “public” code in my lib which is not even used. The only way I currently have to get rid of it is to mark as private and see if something breaks. Function by function.