Suppress clippy autofix for certain rules

Is there a way to prevent clippy from auto-fixing certain lints?

Generally, I find autofix very reasonable, however, it will automatically prefix unused variables with _ which is actively dangerous and not convenient. This makes it harder to use clippy as part of the dev/refactor process when you just want to clear out unused imports/implement other simple fixes.

If you stop clippy warning about a rule, it will also stop it from suggesting auto fixes. So add #![allow(…rule name…)] to your code to suppress rules you don't want. It prints the directive next to the first warning about it.

Technically speaking, this is rustc making this fix, rather than clippy. Just the language lint suggestions applied with cargo fix; cargo clippy --fix will do both clippy and language lints.

If you remember it when running fix, you can use -Aunused_variables to disable the lint (and its fix). I don't think there's a way to automatically disable it only for fixes but leave it on otherwise, though.

If you're running fix automatically on save in your IDE, I'd suggest against it. Format on save is reasonable, and running clippy to show its warnings inline, but I would suggest against automatically applying those fixes. (Especially for clippy, since clippy is by design opinionated and will have some false positives where its suggestions don't improve the code.) Instead, when the warnings show up in your IDE, you should be able to auto-apply them with the intent action (CTRL+. by default in VSCode, IIRC[1]) like you would with other languages.

  1. I've rebound it to ALT+ENTER because of IntelliJ muscle memory, so I don't know the default binding :upside_down_face: ↩︎


Thanks a bunch for that, I was seeing that rustc was raising the issue but had not expected rustc had fix capabilities. Your suggestion is pretty good, because I can create an alias for this. I don't run fix on save but do format on save as you mentioned. In a perfect world I would define this in config so others engineers would benefit but I'm satisfied with a shell alias, thanks!