Cargo Clippy pre-commit hook not aborting on unfixable warnings

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:

  1. If clippy identifies an issue, it attempts to fix it automatically.
  2. If the fix is successful, the changes are applied, and the pre-commit hook aborts the push.
  3. 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!

Thanks in advance!

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:

cargo clippy --all-features --fix
cargo clippy --all-features -- -D warnings

Thanks for the suggestion @kpreid. I was thinking about it, but at the same time I do not want to double the runtime of this hook.

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.

3 Likes

Thank you, I haven't tried it before. The second one is indeed much faster than the first.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.