by evrimoztamur · 1y ago · 0 comments
[lints.clippy]
all = "deny"
unwrap_used = "deny"
expect_used = "deny"
panic = "deny"
indexing_slicing = "deny"
unhandled_errors = "deny"
unreachable = "deny"
undocumented_unsafe_blocks = "deny"
unwrap_in_result = "deny"
ok_expect = "deny"We get around it by using conditional compilation and putting the lints in our entrypoints (`main.rs` or `lib.rs`), which is done automatically for any new entrypoint in the codebase via a Make target and some awk magic.
As an example, the following forbids print and dbg statements in release builds (all output should go through logging), allows it with a warning in debug builds, and allows it unconditionally in tests:
#![cfg_attr(not(debug_assertions), deny(clippy::dbg_macro))]
#![cfg_attr(not(debug_assertions), deny(clippy::print_stdout))]
#![cfg_attr(not(debug_assertions), deny(clippy::print_stderr))]
#![cfg_attr(debug_assertions, warn(clippy::dbg_macro))]
#![cfg_attr(debug_assertions, warn(clippy::print_stdout))]
#![cfg_attr(debug_assertions, warn(clippy::print_stderr))]
#![cfg_attr(test, allow(clippy::dbg_macro))]
#![cfg_attr(test, allow(clippy::print_stdout))]
#![cfg_attr(test, allow(clippy::print_stderr))]
AFAIK there isn't currently a way to configure per-profile lints in the top-level Cargo configs. I wish there were.I try to remember to look at new restriction lints with every Rust release. For example, here's what new with 1.86.0[3]; the `return_and_then` lint looks pretty nice.
n.b. no one should enable all restrictions lints— some are mutually exclusive, some are appropriate for specialised circumstances like `#[no_std]`. But I find them helpful to keep a project away from the wild parts of Rust.
P.S. `unhandled_errors` doesn't exist[4].
[1]: https://rust-lang.github.io/rust-clippy/stable/?groups=restr... [2]: https://doc.rust-lang.org/stable/clippy/lints.html#restricti... [3]: https://rust-lang.github.io/rust-clippy/stable/?groups=restr... [4]: https://rust-lang.github.io/rust-clippy/stable/index.html#/u...
I think the only legitimate uses are for direct indexing for tile maps etc. where I do bounds checking on two axes and know that it will map correctly. to the underlying memory (but that's `clippy::indexing_slicing`, I have 0 `clippy::unwrap_used` in my codebase now).
If you begin a new project with these lints, you'll quickly train to write idiomatic Option/Result handling code by default.