Cargo-check for test code?


cargo check command ignores code with #[cfg(test)] block, so to check whether my test code compiles,

  • Comment #[cfg(test)] and #[test] to let cargo check checks test codes, which is error-prune
  • Run cargo build, which is slower than cargo check

Is there any way to let cargo check to check codes with #[cfg(test)]?

There is an open issue #3431 on cargo for this. Not sure of the current status of the issue though since related #3112 has been merged already.

Just tried with latest nightly cargo 0.19.0-nightly (fa7584c14 2017-04-26), cargo check --tests is accepted but it does not fail on a type checking error inside #[test] fn foobar() { ... }.

EDIT: Commented on the 3431 to ask if this is a bug or not.

IIRC cargo clippy has similar issues as well, like not running on build scripts. It's why I'm still using clippy as a compiler plugin even though plugins aren't stable (yet).