tl;dr: Is it currently possible to get a warning-free build, including cargo test
, without introducing potentially sketchy annotations? Who's doing it?
I love compiler warnings. I love static analysis and anything I can do to catch errors quicker. So, when working in seriously foot-shooty languages like C++, I tend to crank the warnings really high, set the equivalent of -Werror
, and eliminate all the warnings. (I even do this in Haskell, which has some notoriously promiscuous warnings.)
Occasionally, I have to profane my source code with alterations to dodge such warnings, but I try to avoid disabling warnings outright using #pragma
and similar mechanisms, because they're rarely precise: they always feel like they open the door to future rot, undetected by the compiler.
So, that being said:
I'm learning Rust, and I'm surprised by the number of warnings I'm getting that seem... spurious, particularly in test
builds. These are almost always dead_code
. I wonder if there are tricks/idioms I'm missing that I could use to get a warning-free build?
main
is reported as dead code in a test build, as is everything only directly called by main
. (This is the subject of an open bug).
Imports used only by tests are reported as redundant in non-test builds, but I can work around this by by declaring a tests
module marked with #[cfg(test)]
and putting the imports inside it. (IMHO this could use a louder shout-out in the testing chapter of the book.)
Any functions not called from tests are also reported as dead, which I guess is the compiler suggesting I increase my test coverage. I don't want to slap them with
#[allow(dead_code)]
, because if they go dead in the non-test build I do want to hear about it. I was surprised that this does not seem to work:
#[cfg_attr(test, allow(dead_code))]
...which seems like the most precise way of saying to the compiler, "tolerate this being unused in a test build."
What else should I be doing?
Thanks in advance.