4 ms·
This is already a thing, I do this right now. You configure the linter to forbid panics, unwraps, and even arithmetic side effects at compile time. You can con
by bionhoward 1y ago
This is already a thing, I do this right now. You configure the linter to forbid panics, unwraps, and even arithmetic side effects at compile time.
You can configure your lints in your workspace-level Cargo.toml (the folder of crates)
“””
[workspace.lints.clippy]
pedantic = { level = "warn", priority = -1 }
# arithmetic_side_effects = "deny"
unwrap_used = "deny"
expect_used = "deny"
panic = "deny"
“””
then in your crate Cargo.toml
“””
[lints]
workspace = true
“””
Then you can’t even compile the code without proper error handling. Combine that with thiserror or anyhow with the backtrace feature and you can yeet errors with “?” operators or match on em, map_err, map_or_else, ignore them, etc
[1] https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used https://rust-lang.github.io/rust-clippy/master/index.html#un...
- aaronmdjones 1y agoIndent by 4 spaces to get code blocks on HN. Like this
- craftkiller 1y agoYou only need 2 spaces: https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc
- steveklabnik 1y agoYou only need 2. https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc > Text after a blank line that is indented by two or more spaces is reproduced verbatim. (This is intended for code.)
- bionhoward 1y agoThank you
- J_Shelby_J 1y agoBut can deny the use of all operations that might panic like indexing an array?
- bionhoward 1y agoYes, looks like you can, try indexing_slicing https://rust-lang.github.io/rust-clippy/master/#indexing_slicing https://rust-lang.github.io/rust-clippy/master/#indexing_sli...
- DreadY2K 1y agoThere's a lint for indexing an array, but not for all maybe-panicking operations. For example, the `copy_from_slice` method on slices (https://doc.rust-lang.org/std/primitive.slice.html#method.copy_from_slice https://doc.rust-lang.org/std/primitive.slice.html#method.co...) doesn't have a clippy lint for it, even though it will panic if given the wrong length.
- weegee101 1y agoThe issue with this in practice is that there are always cases where panics are absolutely the correct course of action. When program state is bad enough that you can't safely continue, you need to panic (and core dump in dev). Otherwise you are likely just creating an integrity minefield for you to debug later. Not saying there aren't applications where using these lints couldn't be alright (web servers maybe), but at least in my experiences (mostly doing CLI, graphics, and embedded stuff) trying to keep the program alive leads to more problems than less.
- 0x457 1y agoThe comment you're replying to specifically wanted "no panics" version of rust. It's totally normal practice for a library to have this as a standard.