2 minutes
Rust Clippy: Now you see me, now you don’t
I’m still fairly new to Rust, but one of the things I’ve greatly appreciated along the way is Clippy - Rust’s default code linter. The suggested fixes it provides are great for writing more idiomatic code or even discovering new language features (hello enumerate!).
There have been some oddities though - every now and again I was sure there was an extra warning or two that didn’t appear on the next run of cargo clippy
, but there were tests to write and then the code for those tests to write so I didn’t really take much notice…
…Until the day I did some fairly extensive code reorganisation and suddenly found myself with 2-3 times the number of Clippy warnings I was accustomed to, many in code that hadn’t seen changes for a few weeks or more. What was going on?
This time I did stop and take notice.
It turns out that due to the way cargo caches previous builds, just running cargo check
or cargo clippy
won’t be guaranteed to give you the full set of linting warnings across all of your files (see here or here).
There are a few current workarounds:
- Use the nightly Rust toolchain which contains a potential fix
- Do a clean before running clippy,
rust clean && rust clippy
- Update the modification times your files prior to running clippy,
find . | grep "\.rs$" | xargs touch ; cargo clippy
Option 1) isn’t particularly enticing; I’m writing Rust code for my job so I’d much rather stick with stable releases and hopefully avoid toolchain related surprises.
Options 2) and 3) both result in a full rebuild and an annoying wait while cargo runs a bunch a checks on dependencies, so don’t feel great either.
What I’ve ended up with is a variation on 3) but only updating files under src/
,
find ./src/ | grep "\.rs$" | xargs touch ; cargo clippy
This seems to avoid the extra overhead at the build stage but still gives me my full set of linter warnings.
Now to do something about my code to fix those extra Clippy warnings that have been hiding from me…