I'm trying to integrate cargo clippy as part of my pre-commit hooks to enforce code quality in my project. The command I'm using is: cargo clippy --all-features --fix -- -D warnings
Here's the behavior I'm aiming for:
If clippy identifies an issue, it attempts to fix it automatically.
If the fix is successful, the changes are applied, and the pre-commit hook aborts the push.
If clippy is unable to fix the warnings, I expect to receive an error, and the push should be aborted.
The first scenario works as expected—when clippy successfully fixes issues, the push is aborted due to new changes in the codebase, which is great. However, in the second scenario, if clippy can't fix the warnings, I don't receive any errors, and the push goes through as if everything is fine, even though it isn't. I do see these warnings in the log, but the check finishes successfully without any errors, so the push isn’t aborted as it should be.
Has anyone encountered this issue before, or does anyone have suggestions on how to ensure that the pre-commit hook catches when clippy can't automatically fix warnings? I'd appreciate any insights or solutions!
I'm not familiar with the behavior of combining --fix with -D warnings, but it sounds like you might be able to get the behavior you want with two commands in sequence:
It won't double the runtime. I just ran cargo clean && time cargo clippy --all-features --fix ; time cargo clippy --all-features -- -D warnings on a project, and got the following times out:
Checking mandelbrot v0.1.0 (/home/sfarnsworth/Personal/mandelbrot)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 11.81s
real 0m11.902s
user 1m11.934s
sys 0m10.660s
Checking mandelbrot v0.1.0 (/home/sfarnsworth/Personal/mandelbrot)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
real 0m0.395s
user 0m0.258s
sys 0m0.133s
You can see that the second set of checks largely reuses work from the first set of checks.